org.apache.lucene.queries.function.FunctionQuery Java Examples
The following examples show how to use
org.apache.lucene.queries.function.FunctionQuery.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ExtendedDismaxQParser.java From lucene-solr with Apache License 2.0 | 6 votes |
/** * Parses all multiplicative boosts */ protected List<ValueSource> getMultiplicativeBoosts() throws SyntaxError { List<ValueSource> boosts = new ArrayList<>(); if (config.hasMultiplicativeBoosts()) { for (String boostStr : config.multBoosts) { if (boostStr==null || boostStr.length()==0) continue; Query boost = subQuery(boostStr, FunctionQParserPlugin.NAME).getQuery(); ValueSource vs; if (boost instanceof FunctionQuery) { vs = ((FunctionQuery)boost).getValueSource(); } else { vs = new QueryValueSource(boost, 1.0f); } boosts.add(vs); } } return boosts; }
Example #2
Source File: TestTaxonomyFacetSumValueSource.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testWithScore() throws Exception { Directory indexDir = newDirectory(); Directory taxoDir = newDirectory(); DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir); IndexWriter iw = new IndexWriter(indexDir, newIndexWriterConfig(new MockAnalyzer(random()))); FacetsConfig config = new FacetsConfig(); for (int i = 0; i < 4; i++) { Document doc = new Document(); doc.add(new NumericDocValuesField("price", (i+1))); doc.add(new FacetField("a", Integer.toString(i % 2))); iw.addDocument(config.build(taxoWriter, doc)); } DirectoryReader r = DirectoryReader.open(iw); DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter); FacetsCollector fc = new FacetsCollector(true); // score documents by their 'price' field - makes asserting the correct counts for the categories easier Query q = new FunctionQuery(new LongFieldSource("price")); FacetsCollector.search(newSearcher(r), q, 10, fc); Facets facets = new TaxonomyFacetSumValueSource(taxoReader, config, fc, DoubleValuesSource.SCORES); assertEquals("dim=a path=[] value=10.0 childCount=2\n 1 (6.0)\n 0 (4.0)\n", facets.getTopChildren(10, "a").toString()); iw.close(); IOUtils.close(taxoWriter, taxoReader, taxoDir, r, indexDir); }
Example #3
Source File: FunctionRangeQParserPlugin.java From lucene-solr with Apache License 2.0 | 5 votes |
@Override public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) { return new QParser(qstr, localParams, params, req) { ValueSource vs; String funcStr; @Override public Query parse() throws SyntaxError { funcStr = localParams.get(QueryParsing.V, null); QParser subParser = subQuery(funcStr, FunctionQParserPlugin.NAME); subParser.setIsFilter(false); // the range can be based on the relevancy score of embedded queries. Query funcQ = subParser.getQuery(); if (funcQ instanceof FunctionQuery) { vs = ((FunctionQuery)funcQ).getValueSource(); } else { vs = new QueryValueSource(funcQ, 0.0f); } String l = localParams.get("l"); String u = localParams.get("u"); boolean includeLower = localParams.getBool("incl",true); boolean includeUpper = localParams.getBool("incu",true); // TODO: add a score=val option to allow score to be the value ValueSourceRangeFilter rf = new ValueSourceRangeFilter(vs, l, u, includeLower, includeUpper); FunctionRangeQuery frq = new FunctionRangeQuery(rf); return frq; } }; }
Example #4
Source File: CollapsingQParserPlugin.java From lucene-solr with Apache License 2.0 | 5 votes |
public OrdValueSourceStrategy(int maxDoc, int nullPolicy, int valueCount, GroupHeadSelector groupHeadSelector, boolean needsScores4Collapsing, boolean needsScores, IntIntHashMap boostDocs, FunctionQuery funcQuery, IndexSearcher searcher, SortedDocValues values) throws IOException { super(maxDoc, valueCount, nullPolicy, needsScores, boostDocs, values); this.needsScores4Collapsing = needsScores4Collapsing; this.valueSource = funcQuery.getValueSource(); this.rcontext = ValueSource.newContext(searcher); assert GroupHeadSelectorType.MIN_MAX.contains(groupHeadSelector.type); if (GroupHeadSelectorType.MAX.equals(groupHeadSelector.type)) { comp = new MaxFloatComp(); this.ordVals = new IntFloatDynamicMap(valueCount, -Float.MAX_VALUE); } else { this.nullVal = Float.MAX_VALUE; comp = new MinFloatComp(); this.ordVals = new IntFloatDynamicMap(valueCount, Float.MAX_VALUE); } collapseScore.setupIfNeeded(groupHeadSelector, rcontext); }
Example #5
Source File: CollapsingQParserPlugin.java From lucene-solr with Apache License 2.0 | 5 votes |
public IntValueSourceStrategy(int maxDoc, int size, String collapseField, int nullValue, int nullPolicy, GroupHeadSelector groupHeadSelector, boolean needsScores4Collapsing, boolean needsScores, IntIntHashMap boostDocs, FunctionQuery funcQuery, IndexSearcher searcher) throws IOException { super(maxDoc, size, collapseField, nullValue, nullPolicy, needsScores, boostDocs); this.needsScores4Collapsing = needsScores4Collapsing; this.testValues = new IntFloatDynamicMap(size, 0.0f); this.valueSource = funcQuery.getValueSource(); this.rcontext = ValueSource.newContext(searcher); assert GroupHeadSelectorType.MIN_MAX.contains(groupHeadSelector.type); if (GroupHeadSelectorType.MAX.equals(groupHeadSelector.type)) { this.nullCompVal = -Float.MAX_VALUE; comp = new MaxFloatComp(); } else { this.nullCompVal = Float.MAX_VALUE; comp = new MinFloatComp(); } collapseScore.setupIfNeeded(groupHeadSelector, rcontext); }
Example #6
Source File: FunctionQParser.java From lucene-solr with Apache License 2.0 | 5 votes |
@Override public Query parse() throws SyntaxError { ValueSource vs = null; List<ValueSource> lst = null; for(;;) { ValueSource valsource = parseValueSource(FLAG_DEFAULT & ~FLAG_CONSUME_DELIMITER); sp.eatws(); if (!parseMultipleSources) { vs = valsource; break; } else { if (lst != null) { lst.add(valsource); } else { vs = valsource; } } // check if there is a "," separator if (sp.peek() != ',') break; consumeArgumentDelimiter(); if (lst == null) { lst = new ArrayList<>(2); lst.add(valsource); } } if (parseToEnd && sp.pos < sp.end) { throw new SyntaxError("Unexpected text after function: " + sp.val.substring(sp.pos, sp.end)); } if (lst != null) { vs = new VectorValueSource(lst); } return new FunctionQuery(vs); }
Example #7
Source File: StatsField.java From lucene-solr with Apache License 2.0 | 5 votes |
/** * Inspects a {@link Query} to see if it directly maps to a {@link ValueSource}, * and if so returns it -- otherwise wraps it as needed. * * @param q Query whose scores we have been asked to compute stats of * @returns a ValueSource to use for computing the stats */ private static ValueSource extractValueSource(Query q) { return (q instanceof FunctionQuery) ? // Common case: we're wrapping a func, so we can directly pull out ValueSource ((FunctionQuery) q).getValueSource() : // asked to compute stats over a query, wrap it up as a ValueSource new QueryValueSource(q, 0.0F); }
Example #8
Source File: TestOrdValues.java From lucene-solr with Apache License 2.0 | 5 votes |
private void doTestRank(String field, boolean inOrder) throws Exception { IndexReader r = DirectoryReader.open(dir); IndexSearcher s = newSearcher(r); ValueSource vs; if (inOrder) { vs = new OrdFieldSource(field); } else { vs = new ReverseOrdFieldSource(field); } Query q = new FunctionQuery(vs); log("test: " + q); QueryUtils.check(random(), q, s); ScoreDoc[] h = s.search(q, 1000).scoreDocs; assertEquals("All docs should be matched!", N_DOCS, h.length); String prevID = inOrder ? "IE" // greater than all ids of docs in this test ("ID0001", etc.) : "IC"; // smaller than all ids of docs in this test ("ID0001", etc.) for (int i = 0; i < h.length; i++) { String resID = s.doc(h[i].doc).get(ID_FIELD); log(i + ". score=" + h[i].score + " - " + resID); log(s.explain(q, h[i].doc)); if (inOrder) { assertTrue("res id " + resID + " should be < prev res id " + prevID, resID.compareTo(prevID) < 0); } else { assertTrue("res id " + resID + " should be > prev res id " + prevID, resID.compareTo(prevID) > 0); } prevID = resID; } r.close(); }
Example #9
Source File: TestOrdValues.java From lucene-solr with Apache License 2.0 | 5 votes |
private void doTestExactScore(String field, boolean inOrder) throws Exception { IndexReader r = DirectoryReader.open(dir); IndexSearcher s = newSearcher(r); ValueSource vs; if (inOrder) { vs = new OrdFieldSource(field); } else { vs = new ReverseOrdFieldSource(field); } Query q = new FunctionQuery(vs); TopDocs td = s.search(q, 1000); assertEquals("All docs should be matched!", N_DOCS, td.totalHits.value); ScoreDoc sd[] = td.scoreDocs; for (int i = 0; i < sd.length; i++) { float score = sd[i].score; String id = s.getIndexReader().document(sd[i].doc).get(ID_FIELD); log("-------- " + i + ". Explain doc " + id); log(s.explain(q, sd[i].doc)); float expectedScore = N_DOCS - i - 1; assertEquals("score of result " + i + " should be " + expectedScore + " != " + score, expectedScore, score, TEST_SCORE_TOLERANCE_DELTA); String expectedId = inOrder ? id2String(N_DOCS - i) // in-order ==> larger values first : id2String(i + 1); // reverse ==> smaller values first assertTrue("id of result " + i + " should be " + expectedId + " != " + score, expectedId.equals(id)); } r.close(); }
Example #10
Source File: BoostQParserPlugin.java From lucene-solr with Apache License 2.0 | 4 votes |
@Override public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) { return new QParser(qstr, localParams, params, req) { QParser baseParser; ValueSource vs; String b; @Override public Query parse() throws SyntaxError { b = localParams.get(BOOSTFUNC); baseParser = subQuery(localParams.get(QueryParsing.V), null); Query q = baseParser.getQuery(); if (b == null) return q; Query bq = subQuery(b, FunctionQParserPlugin.NAME).getQuery(); if (bq instanceof FunctionQuery) { vs = ((FunctionQuery)bq).getValueSource(); } else { vs = new QueryValueSource(bq, 0.0f); } return FunctionScoreQuery.boostByValue(q, vs.asDoubleValuesSource()); } @Override public String[] getDefaultHighlightFields() { return baseParser.getDefaultHighlightFields(); } @Override public Query getHighlightQuery() throws SyntaxError { return baseParser.getHighlightQuery(); } @Override public void addDebugInfo(NamedList<Object> debugInfo) { // encapsulate base debug info in a sub-list? baseParser.addDebugInfo(debugInfo); debugInfo.add("boost_str",b); debugInfo.add("boost_parsed",vs); } }; }
Example #11
Source File: CollapsingQParserPlugin.java From lucene-solr with Apache License 2.0 | 4 votes |
public OrdFieldValueCollector(int maxDoc, int segments, DocValuesProducer collapseValuesProducer, int nullPolicy, GroupHeadSelector groupHeadSelector, SortSpec sortSpec, boolean needsScores4Collapsing, boolean needsScores, FieldType fieldType, IntIntHashMap boostDocs, FunctionQuery funcQuery, IndexSearcher searcher) throws IOException{ assert ! GroupHeadSelectorType.SCORE.equals(groupHeadSelector.type); this.maxDoc = maxDoc; this.contexts = new LeafReaderContext[segments]; List<LeafReaderContext> con = searcher.getTopReaderContext().leaves(); for(int i=0; i<con.size(); i++) { contexts[i] = con.get(i); } this.collapseValuesProducer = collapseValuesProducer; this.collapseValues = collapseValuesProducer.getSorted(null); if(collapseValues instanceof MultiDocValues.MultiSortedDocValues) { this.multiSortedDocValues = (MultiDocValues.MultiSortedDocValues)collapseValues; this.ordinalMap = multiSortedDocValues.mapping; } int valueCount = collapseValues.getValueCount(); this.nullPolicy = nullPolicy; this.needsScores4Collapsing = needsScores4Collapsing; this.needsScores = needsScores; if (null != sortSpec) { this.collapseStrategy = new OrdSortSpecStrategy(maxDoc, nullPolicy, valueCount, groupHeadSelector, this.needsScores4Collapsing, this.needsScores, boostDocs, sortSpec, searcher, collapseValues); } else if (funcQuery != null) { this.collapseStrategy = new OrdValueSourceStrategy(maxDoc, nullPolicy, valueCount, groupHeadSelector, this.needsScores4Collapsing, this.needsScores, boostDocs, funcQuery, searcher, collapseValues); } else { NumberType numType = fieldType.getNumberType(); if (null == numType) { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "min/max must be either Int/Long/Float based field types"); } switch (numType) { case INTEGER: { this.collapseStrategy = new OrdIntStrategy(maxDoc, nullPolicy, valueCount, groupHeadSelector, this.needsScores, boostDocs, collapseValues); break; } case FLOAT: { this.collapseStrategy = new OrdFloatStrategy(maxDoc, nullPolicy, valueCount, groupHeadSelector, this.needsScores, boostDocs, collapseValues); break; } case LONG: { this.collapseStrategy = new OrdLongStrategy(maxDoc, nullPolicy, valueCount, groupHeadSelector, this.needsScores, boostDocs, collapseValues); break; } default: { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "min/max must be either Int/Long/Float field types"); } } } }
Example #12
Source File: CollapsingQParserPlugin.java From lucene-solr with Apache License 2.0 | 4 votes |
public IntFieldValueCollector(int maxDoc, int size, int segments, int nullValue, int nullPolicy, String collapseField, GroupHeadSelector groupHeadSelector, SortSpec sortSpec, boolean needsScores4Collapsing, boolean needsScores, FieldType fieldType, IntIntHashMap boostDocsMap, FunctionQuery funcQuery, IndexSearcher searcher) throws IOException{ assert ! GroupHeadSelectorType.SCORE.equals(groupHeadSelector.type); this.maxDoc = maxDoc; this.contexts = new LeafReaderContext[segments]; List<LeafReaderContext> con = searcher.getTopReaderContext().leaves(); for(int i=0; i<con.size(); i++) { contexts[i] = con.get(i); } this.collapseField = collapseField; this.nullValue = nullValue; this.nullPolicy = nullPolicy; this.needsScores4Collapsing = needsScores4Collapsing; this.needsScores = needsScores; if (null != sortSpec) { this.collapseStrategy = new IntSortSpecStrategy(maxDoc, size, collapseField, nullValue, nullPolicy, groupHeadSelector, this.needsScores4Collapsing, this.needsScores, boostDocsMap, sortSpec, searcher); } else if (funcQuery != null) { this.collapseStrategy = new IntValueSourceStrategy(maxDoc, size, collapseField, nullValue, nullPolicy, groupHeadSelector, this.needsScores4Collapsing, this.needsScores, boostDocsMap, funcQuery, searcher); } else { NumberType numType = fieldType.getNumberType(); assert null != numType; // shouldn't make it here for non-numeric types switch (numType) { case INTEGER: { this.collapseStrategy = new IntIntStrategy(maxDoc, size, collapseField, nullValue, nullPolicy, groupHeadSelector, this.needsScores, boostDocsMap); break; } case FLOAT: { this.collapseStrategy = new IntFloatStrategy(maxDoc, size, collapseField, nullValue, nullPolicy, groupHeadSelector, this.needsScores, boostDocsMap); break; } default: { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "min/max must be Int or Float field types when collapsing on numeric fields"); } } } }
Example #13
Source File: LuceneQueryUtil.java From querqy with Apache License 2.0 | 4 votes |
public static ValueSource queryToValueSource(final Query query) { return (query instanceof FunctionQuery) ? ((FunctionQuery)query).getValueSource() : new QueryValueSource(query, 1.0f); }
Example #14
Source File: MtasScoreQuery.java From mtas with Apache License 2.0 | 2 votes |
/** * Instantiates a new mtas score query. * * @param subQuery the sub query * @param scoringQuery the scoring query */ public MtasScoreQuery(Query subQuery, FunctionQuery scoringQuery) { super(subQuery, scoringQuery); }
Example #15
Source File: MtasScoreQuery.java From mtas with Apache License 2.0 | 2 votes |
/** * Instantiates a new mtas score query. * * @param subQuery the sub query * @param scoringQueries the scoring queries */ public MtasScoreQuery(Query subQuery, FunctionQuery... scoringQueries) { super(subQuery, scoringQueries); }