|
55 | 55 | import org.apache.lucene.search.IndexSearcher;
|
56 | 56 | import org.apache.lucene.search.MatchAllDocsQuery;
|
57 | 57 | import org.apache.lucene.search.MatchNoDocsQuery;
|
| 58 | +import org.apache.lucene.search.PhraseQuery; |
58 | 59 | import org.apache.lucene.search.PrefixQuery;
|
59 | 60 | import org.apache.lucene.search.Query;
|
60 | 61 | import org.apache.lucene.search.Scorer;
|
|
77 | 78 | import org.elasticsearch.common.bytes.BytesArray;
|
78 | 79 | import org.elasticsearch.common.bytes.BytesReference;
|
79 | 80 | import org.elasticsearch.common.compress.CompressedXContent;
|
| 81 | +import org.elasticsearch.common.lucene.search.function.FunctionScoreQuery; |
80 | 82 | import org.elasticsearch.common.geo.ShapeRelation;
|
81 | 83 | import org.elasticsearch.common.settings.Settings;
|
82 | 84 | import org.elasticsearch.common.xcontent.XContentFactory;
|
@@ -220,6 +222,16 @@ public void testDuel() throws Exception {
|
220 | 222 | }
|
221 | 223 | return new DisjunctionMaxQuery(clauses, 0.01f);
|
222 | 224 | });
|
| 225 | + queryFunctions.add(() -> { |
| 226 | + Float minScore = randomBoolean() ? null : (float) randomIntBetween(1, 1000); |
| 227 | + Query innerQuery; |
| 228 | + if (randomBoolean()) { |
| 229 | + innerQuery = new TermQuery(new Term(field1, randomFrom(stringContent.get(field1)))); |
| 230 | + } else { |
| 231 | + innerQuery = new PhraseQuery(field1, randomFrom(stringContent.get(field1)), randomFrom(stringContent.get(field1))); |
| 232 | + } |
| 233 | + return new FunctionScoreQuery(innerQuery, minScore, 1f); |
| 234 | + }); |
223 | 235 |
|
224 | 236 | List<ParseContext.Document> documents = new ArrayList<>();
|
225 | 237 | for (Supplier<Query> queryFunction : queryFunctions) {
|
@@ -679,6 +691,31 @@ public void testPercolateMatchAll() throws Exception {
|
679 | 691 | assertEquals(4, topDocs.scoreDocs[2].doc);
|
680 | 692 | }
|
681 | 693 |
|
| 694 | + public void testFunctionScoreQuery() throws Exception { |
| 695 | + List<ParseContext.Document> docs = new ArrayList<>(); |
| 696 | + addQuery(new FunctionScoreQuery(new TermQuery(new Term("field", "value")), null, 1f), docs); |
| 697 | + addQuery(new FunctionScoreQuery(new TermQuery(new Term("field", "value")), 10f, 1f), docs); |
| 698 | + addQuery(new FunctionScoreQuery(new MatchAllDocsQuery(), null, 1f), docs); |
| 699 | + addQuery(new FunctionScoreQuery(new MatchAllDocsQuery(), 10F, 1f), docs); |
| 700 | + |
| 701 | + indexWriter.addDocuments(docs); |
| 702 | + indexWriter.close(); |
| 703 | + directoryReader = DirectoryReader.open(directory); |
| 704 | + IndexSearcher shardSearcher = newSearcher(directoryReader); |
| 705 | + shardSearcher.setQueryCache(null); |
| 706 | + |
| 707 | + MemoryIndex memoryIndex = new MemoryIndex(); |
| 708 | + memoryIndex.addField("field", "value", new WhitespaceAnalyzer()); |
| 709 | + IndexSearcher percolateSearcher = memoryIndex.createSearcher(); |
| 710 | + PercolateQuery query = (PercolateQuery) fieldType.percolateQuery("_name", queryStore, |
| 711 | + Collections.singletonList(new BytesArray("{}")), percolateSearcher, Version.CURRENT); |
| 712 | + TopDocs topDocs = shardSearcher.search(query, 10, new Sort(SortField.FIELD_DOC), true, true); |
| 713 | + assertEquals(2L, topDocs.totalHits); |
| 714 | + assertEquals(2, topDocs.scoreDocs.length); |
| 715 | + assertEquals(0, topDocs.scoreDocs[0].doc); |
| 716 | + assertEquals(2, topDocs.scoreDocs[1].doc); |
| 717 | + } |
| 718 | + |
682 | 719 | public void testPercolateSmallAndLargeDocument() throws Exception {
|
683 | 720 | List<ParseContext.Document> docs = new ArrayList<>();
|
684 | 721 | BooleanQuery.Builder builder = new BooleanQuery.Builder();
|
|
0 commit comments