Search in sources :

Example 11 with FunctionScoreQuery

use of org.opensearch.common.lucene.search.function.FunctionScoreQuery in project OpenSearch by opensearch-project.

the class FunctionScoreQueryBuilderTests method testCustomWeightFactorQueryBuilderWithFunctionScoreWithoutQueryGiven.

public void testCustomWeightFactorQueryBuilderWithFunctionScoreWithoutQueryGiven() throws IOException {
    Query parsedQuery = parseQuery(functionScoreQuery(weightFactorFunction(1.3f))).toQuery(createShardContext());
    assertThat(parsedQuery, instanceOf(FunctionScoreQuery.class));
    FunctionScoreQuery functionScoreQuery = (FunctionScoreQuery) parsedQuery;
    assertThat(functionScoreQuery.getSubQuery() instanceof MatchAllDocsQuery, equalTo(true));
    assertThat((double) (functionScoreQuery.getFunctions()[0]).getWeight(), closeTo(1.3, 0.001));
}
Also used : Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) QueryBuilders.matchAllQuery(org.opensearch.index.query.QueryBuilders.matchAllQuery) QueryBuilders.termQuery(org.opensearch.index.query.QueryBuilders.termQuery) FunctionScoreQuery(org.opensearch.common.lucene.search.function.FunctionScoreQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) QueryBuilders.functionScoreQuery(org.opensearch.index.query.QueryBuilders.functionScoreQuery) TermQuery(org.apache.lucene.search.TermQuery) FunctionScoreQuery(org.opensearch.common.lucene.search.function.FunctionScoreQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Example 12 with FunctionScoreQuery

use of org.opensearch.common.lucene.search.function.FunctionScoreQuery in project OpenSearch by opensearch-project.

the class CandidateQueryTests method testFunctionScoreQuery.

public void testFunctionScoreQuery() throws Exception {
    List<ParseContext.Document> docs = new ArrayList<>();
    addQuery(new FunctionScoreQuery(new TermQuery(new Term("field", "value")), null, 1f), docs);
    addQuery(new FunctionScoreQuery(new TermQuery(new Term("field", "value")), 10f, 1f), docs);
    addQuery(new FunctionScoreQuery(new MatchAllDocsQuery(), null, 1f), docs);
    addQuery(new FunctionScoreQuery(new MatchAllDocsQuery(), 10F, 1f), docs);
    indexWriter.addDocuments(docs);
    indexWriter.close();
    directoryReader = DirectoryReader.open(directory);
    IndexSearcher shardSearcher = newSearcher(directoryReader);
    shardSearcher.setQueryCache(null);
    MemoryIndex memoryIndex = new MemoryIndex();
    memoryIndex.addField("field", "value", new WhitespaceAnalyzer());
    IndexSearcher percolateSearcher = memoryIndex.createSearcher();
    PercolateQuery query = (PercolateQuery) fieldType.percolateQuery("_name", queryStore, Collections.singletonList(new BytesArray("{}")), percolateSearcher, false, Version.CURRENT);
    TopDocs topDocs = shardSearcher.search(query, 10, new Sort(SortField.FIELD_DOC));
    assertEquals(2L, topDocs.totalHits.value);
    assertEquals(2, topDocs.scoreDocs.length);
    assertEquals(0, topDocs.scoreDocs[0].doc);
    assertEquals(2, topDocs.scoreDocs[1].doc);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) WhitespaceAnalyzer(org.apache.lucene.analysis.core.WhitespaceAnalyzer) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) BytesArray(org.opensearch.common.bytes.BytesArray) FunctionScoreQuery(org.opensearch.common.lucene.search.function.FunctionScoreQuery) ArrayList(java.util.ArrayList) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TopDocs(org.apache.lucene.search.TopDocs) MemoryIndex(org.apache.lucene.index.memory.MemoryIndex) Sort(org.apache.lucene.search.Sort)

Example 13 with FunctionScoreQuery

use of org.opensearch.common.lucene.search.function.FunctionScoreQuery in project OpenSearch by opensearch-project.

the class CandidateQueryTests method testDuel.

public void testDuel() throws Exception {
    int numFields = randomIntBetween(1, 3);
    Map<String, List<String>> stringContent = new HashMap<>();
    for (int i = 0; i < numFields; i++) {
        int numTokens = randomIntBetween(1, 64);
        List<String> values = new ArrayList<>();
        for (int j = 0; j < numTokens; j++) {
            values.add(randomAlphaOfLength(8));
        }
        stringContent.put("field" + i, values);
    }
    List<String> stringFields = new ArrayList<>(stringContent.keySet());
    int numValues = randomIntBetween(16, 64);
    List<Integer> intValues = new ArrayList<>(numValues);
    for (int j = 0; j < numValues; j++) {
        intValues.add(randomInt());
    }
    Collections.sort(intValues);
    QueryShardContext context = createSearchContext(indexService).getQueryShardContext();
    MappedFieldType intFieldType = mapperService.fieldType("int_field");
    List<Supplier<Query>> queryFunctions = new ArrayList<>();
    queryFunctions.add(MatchNoDocsQuery::new);
    queryFunctions.add(MatchAllDocsQuery::new);
    queryFunctions.add(() -> new TermQuery(new Term("unknown_field", "value")));
    String field1 = randomFrom(stringFields);
    queryFunctions.add(() -> new TermQuery(new Term(field1, randomFrom(stringContent.get(field1)))));
    String field2 = randomFrom(stringFields);
    queryFunctions.add(() -> new TermQuery(new Term(field2, randomFrom(stringContent.get(field2)))));
    queryFunctions.add(() -> intFieldType.termQuery(randomFrom(intValues), context));
    queryFunctions.add(() -> intFieldType.termsQuery(Arrays.asList(randomFrom(intValues), randomFrom(intValues)), context));
    queryFunctions.add(() -> intFieldType.rangeQuery(intValues.get(4), intValues.get(intValues.size() - 4), true, true, ShapeRelation.WITHIN, null, null, context));
    queryFunctions.add(() -> new TermInSetQuery(field1, new BytesRef(randomFrom(stringContent.get(field1))), new BytesRef(randomFrom(stringContent.get(field1)))));
    queryFunctions.add(() -> new TermInSetQuery(field2, new BytesRef(randomFrom(stringContent.get(field1))), new BytesRef(randomFrom(stringContent.get(field1)))));
    // many iterations with boolean queries, which are the most complex queries to deal with when nested
    int numRandomBoolQueries = 1000;
    for (int i = 0; i < numRandomBoolQueries; i++) {
        queryFunctions.add(() -> createRandomBooleanQuery(1, stringFields, stringContent, intFieldType, intValues));
    }
    queryFunctions.add(() -> {
        int numClauses = randomIntBetween(1, 1 << randomIntBetween(2, 4));
        List<Query> clauses = new ArrayList<>();
        for (int i = 0; i < numClauses; i++) {
            String field = randomFrom(stringFields);
            clauses.add(new TermQuery(new Term(field, randomFrom(stringContent.get(field)))));
        }
        return new DisjunctionMaxQuery(clauses, 0.01f);
    });
    queryFunctions.add(() -> {
        Float minScore = randomBoolean() ? null : (float) randomIntBetween(1, 1000);
        Query innerQuery;
        if (randomBoolean()) {
            innerQuery = new TermQuery(new Term(field1, randomFrom(stringContent.get(field1))));
        } else {
            innerQuery = new PhraseQuery(field1, randomFrom(stringContent.get(field1)), randomFrom(stringContent.get(field1)));
        }
        return new FunctionScoreQuery(innerQuery, minScore, 1f);
    });
    List<ParseContext.Document> documents = new ArrayList<>();
    for (Supplier<Query> queryFunction : queryFunctions) {
        Query query = queryFunction.get();
        addQuery(query, documents);
    }
    indexWriter.addDocuments(documents);
    indexWriter.close();
    directoryReader = DirectoryReader.open(directory);
    IndexSearcher shardSearcher = newSearcher(directoryReader);
    // Disable query cache, because ControlQuery cannot be cached...
    shardSearcher.setQueryCache(null);
    Document document = new Document();
    for (Map.Entry<String, List<String>> entry : stringContent.entrySet()) {
        String value = entry.getValue().stream().collect(Collectors.joining(" "));
        document.add(new TextField(entry.getKey(), value, Field.Store.NO));
    }
    for (Integer intValue : intValues) {
        List<Field> numberFields = NumberFieldMapper.NumberType.INTEGER.createFields("int_field", intValue, true, true, false);
        for (Field numberField : numberFields) {
            document.add(numberField);
        }
    }
    MemoryIndex memoryIndex = MemoryIndex.fromDocument(document, new WhitespaceAnalyzer());
    duelRun(queryStore, memoryIndex, shardSearcher);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) PhraseQuery(org.apache.lucene.search.PhraseQuery) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) CoveringQuery(org.apache.lucene.search.CoveringQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) SpanNotQuery(org.apache.lucene.search.spans.SpanNotQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) CommonTermsQuery(org.apache.lucene.queries.CommonTermsQuery) FunctionScoreQuery(org.opensearch.common.lucene.search.function.FunctionScoreQuery) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) HashMap(java.util.HashMap) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) ArrayList(java.util.ArrayList) InetAddresses.forString(org.opensearch.common.network.InetAddresses.forString) Document(org.apache.lucene.document.Document) SortField(org.apache.lucene.search.SortField) TextField(org.apache.lucene.document.TextField) IndexableField(org.apache.lucene.index.IndexableField) StoredField(org.apache.lucene.document.StoredField) StringField(org.apache.lucene.document.StringField) Field(org.apache.lucene.document.Field) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) QueryShardContext(org.opensearch.index.query.QueryShardContext) TextField(org.apache.lucene.document.TextField) ArrayList(java.util.ArrayList) List(java.util.List) Supplier(java.util.function.Supplier) BytesRef(org.apache.lucene.util.BytesRef) WhitespaceAnalyzer(org.apache.lucene.analysis.core.WhitespaceAnalyzer) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) FunctionScoreQuery(org.opensearch.common.lucene.search.function.FunctionScoreQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) Term(org.apache.lucene.index.Term) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) FloatPoint(org.apache.lucene.document.FloatPoint) DoublePoint(org.apache.lucene.document.DoublePoint) InetAddressPoint(org.apache.lucene.document.InetAddressPoint) LongPoint(org.apache.lucene.document.LongPoint) IntPoint(org.apache.lucene.document.IntPoint) HalfFloatPoint(org.apache.lucene.document.HalfFloatPoint) MemoryIndex(org.apache.lucene.index.memory.MemoryIndex) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) Map(java.util.Map) HashMap(java.util.HashMap)

Example 14 with FunctionScoreQuery

use of org.opensearch.common.lucene.search.function.FunctionScoreQuery in project OpenSearch by opensearch-project.

the class QueryAnalyzerTests method testFunctionScoreQuery_withMatchAll.

public void testFunctionScoreQuery_withMatchAll() {
    MatchAllDocsQuery innerQuery = new MatchAllDocsQuery();
    FunctionScoreQuery functionScoreQuery1 = new FunctionScoreQuery(innerQuery, new RandomScoreFunction(0, 0, null));
    Result result = analyze(functionScoreQuery1, Version.CURRENT);
    assertThat(result.verified, is(true));
    assertThat(result.minimumShouldMatch, equalTo(0));
    assertThat(result.matchAllDocs, is(true));
    assertThat(result.extractions.isEmpty(), is(true));
    FunctionScoreQuery functionScoreQuery2 = new FunctionScoreQuery(innerQuery, new RandomScoreFunction(0, 0, null), CombineFunction.MULTIPLY, 1f, 10f);
    result = analyze(functionScoreQuery2, Version.CURRENT);
    assertThat(result.verified, is(false));
    assertThat(result.minimumShouldMatch, equalTo(0));
    assertThat(result.matchAllDocs, is(true));
    assertThat(result.extractions.isEmpty(), is(true));
}
Also used : FunctionScoreQuery(org.opensearch.common.lucene.search.function.FunctionScoreQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) RandomScoreFunction(org.opensearch.common.lucene.search.function.RandomScoreFunction) Result(org.opensearch.percolator.QueryAnalyzer.Result) QueryAnalyzer.selectBestResult(org.opensearch.percolator.QueryAnalyzer.selectBestResult)

Example 15 with FunctionScoreQuery

use of org.opensearch.common.lucene.search.function.FunctionScoreQuery in project OpenSearch by opensearch-project.

the class PercolatorHighlightSubFetchPhaseTests method testLocatePercolatorQuery.

public void testLocatePercolatorQuery() {
    PercolateQuery percolateQuery = new PercolateQuery("_name", ctx -> null, Collections.singletonList(new BytesArray("{}")), new MatchAllDocsQuery(), Mockito.mock(IndexSearcher.class), null, new MatchAllDocsQuery());
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(new MatchAllDocsQuery()).size(), equalTo(0));
    BooleanQuery.Builder bq = new BooleanQuery.Builder();
    bq.add(new MatchAllDocsQuery(), BooleanClause.Occur.FILTER);
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(bq.build()).size(), equalTo(0));
    bq.add(percolateQuery, BooleanClause.Occur.FILTER);
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(bq.build()).size(), equalTo(1));
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(bq.build()).get(0), sameInstance(percolateQuery));
    ConstantScoreQuery constantScoreQuery = new ConstantScoreQuery(new MatchAllDocsQuery());
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(constantScoreQuery).size(), equalTo(0));
    constantScoreQuery = new ConstantScoreQuery(percolateQuery);
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(constantScoreQuery).size(), equalTo(1));
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(constantScoreQuery).get(0), sameInstance(percolateQuery));
    BoostQuery boostQuery = new BoostQuery(new MatchAllDocsQuery(), 1f);
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(boostQuery).size(), equalTo(0));
    boostQuery = new BoostQuery(percolateQuery, 1f);
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(boostQuery).size(), equalTo(1));
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(boostQuery).get(0), sameInstance(percolateQuery));
    FunctionScoreQuery functionScoreQuery = new FunctionScoreQuery(new MatchAllDocsQuery(), new RandomScoreFunction(0, 0, null));
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(functionScoreQuery).size(), equalTo(0));
    functionScoreQuery = new FunctionScoreQuery(percolateQuery, new RandomScoreFunction(0, 0, null));
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(functionScoreQuery).size(), equalTo(1));
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(functionScoreQuery).get(0), sameInstance(percolateQuery));
    DisjunctionMaxQuery disjunctionMaxQuery = new DisjunctionMaxQuery(Collections.singleton(new MatchAllDocsQuery()), 1f);
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(disjunctionMaxQuery).size(), equalTo(0));
    disjunctionMaxQuery = new DisjunctionMaxQuery(Arrays.asList(percolateQuery, new MatchAllDocsQuery()), 1f);
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(disjunctionMaxQuery).size(), equalTo(1));
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(disjunctionMaxQuery).get(0), sameInstance(percolateQuery));
    PercolateQuery percolateQuery2 = new PercolateQuery("_name", ctx -> null, Collections.singletonList(new BytesArray("{}")), new MatchAllDocsQuery(), Mockito.mock(IndexSearcher.class), null, new MatchAllDocsQuery());
    bq = new BooleanQuery.Builder();
    bq.add(new MatchAllDocsQuery(), BooleanClause.Occur.FILTER);
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(bq.build()).size(), equalTo(0));
    bq.add(percolateQuery, BooleanClause.Occur.FILTER);
    bq.add(percolateQuery2, BooleanClause.Occur.FILTER);
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(bq.build()).size(), equalTo(2));
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(bq.build()), containsInAnyOrder(sameInstance(percolateQuery), sameInstance(percolateQuery2)));
    assertNotNull(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(null));
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(null).size(), equalTo(0));
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) BooleanQuery(org.apache.lucene.search.BooleanQuery) BytesArray(org.opensearch.common.bytes.BytesArray) FunctionScoreQuery(org.opensearch.common.lucene.search.function.FunctionScoreQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) BoostQuery(org.apache.lucene.search.BoostQuery) RandomScoreFunction(org.opensearch.common.lucene.search.function.RandomScoreFunction)

Aggregations

FunctionScoreQuery (org.opensearch.common.lucene.search.function.FunctionScoreQuery)27 TermQuery (org.apache.lucene.search.TermQuery)21 Term (org.apache.lucene.index.Term)15 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)13 Query (org.apache.lucene.search.Query)12 IndexSearcher (org.apache.lucene.search.IndexSearcher)9 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)8 RandomScoreFunction (org.opensearch.common.lucene.search.function.RandomScoreFunction)8 ScoreFunction (org.opensearch.common.lucene.search.function.ScoreFunction)6 RandomApproximationQuery (org.apache.lucene.search.RandomApproximationQuery)5 FieldValueFactorFunction (org.opensearch.common.lucene.search.function.FieldValueFactorFunction)5 FilterScoreFunction (org.opensearch.common.lucene.search.function.FunctionScoreQuery.FilterScoreFunction)5 BlendedTermQuery (org.apache.lucene.queries.BlendedTermQuery)4 TopDocs (org.apache.lucene.search.TopDocs)4 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)4 CombineFunction (org.opensearch.common.lucene.search.function.CombineFunction)4 LeafScoreFunction (org.opensearch.common.lucene.search.function.LeafScoreFunction)4 WeightFactorFunction (org.opensearch.common.lucene.search.function.WeightFactorFunction)4 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)3 Explanation (org.apache.lucene.search.Explanation)3