Search in sources :

Example 6 with QueryExtraction

use of org.opensearch.percolator.QueryAnalyzer.QueryExtraction in project OpenSearch by opensearch-project.

the class QueryAnalyzerTests method testExtractQueryMetadata_phraseQuery.

public void testExtractQueryMetadata_phraseQuery() {
    PhraseQuery phraseQuery = new PhraseQuery("_field", "_term1", "term2");
    Result result = analyze(phraseQuery, Version.CURRENT);
    assertThat(result.verified, is(false));
    assertThat(result.minimumShouldMatch, equalTo(2));
    List<QueryExtraction> terms = new ArrayList<>(result.extractions);
    terms.sort(Comparator.comparing(qt -> qt.term));
    assertThat(terms.size(), equalTo(2));
    assertThat(terms.get(0).field(), equalTo(phraseQuery.getTerms()[0].field()));
    assertThat(terms.get(0).bytes(), equalTo(phraseQuery.getTerms()[0].bytes()));
    assertThat(terms.get(1).field(), equalTo(phraseQuery.getTerms()[1].field()));
    assertThat(terms.get(1).bytes(), equalTo(phraseQuery.getTerms()[1].bytes()));
}
Also used : Query(org.apache.lucene.search.Query) Arrays(java.util.Arrays) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) Term(org.apache.lucene.index.Term) CommonTermsQuery(org.apache.lucene.queries.CommonTermsQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) Version(org.opensearch.Version) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) DoublePoint(org.apache.lucene.document.DoublePoint) QueryAnalyzer.analyze(org.opensearch.percolator.QueryAnalyzer.analyze) IntervalsSource(org.apache.lucene.queries.intervals.IntervalsSource) LatLonPoint(org.apache.lucene.document.LatLonPoint) Occur(org.apache.lucene.search.BooleanClause.Occur) FunctionScoreQuery(org.opensearch.common.lucene.search.function.FunctionScoreQuery) InetAddressPoint(org.apache.lucene.document.InetAddressPoint) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) SpanFirstQuery(org.apache.lucene.search.spans.SpanFirstQuery) BytesRef(org.apache.lucene.util.BytesRef) Result(org.opensearch.percolator.QueryAnalyzer.Result) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Set(java.util.Set) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) ScoreMode(org.apache.lucene.search.join.ScoreMode) Collectors(java.util.stream.Collectors) IsCollectionWithSize.hasSize(org.hamcrest.collection.IsCollectionWithSize.hasSize) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) List(java.util.List) IntervalQuery(org.apache.lucene.queries.intervals.IntervalQuery) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.is(org.hamcrest.Matchers.is) RandomScoreFunction(org.opensearch.common.lucene.search.function.RandomScoreFunction) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) QueryExtraction(org.opensearch.percolator.QueryAnalyzer.QueryExtraction) LongPoint(org.apache.lucene.document.LongPoint) InetAddresses(org.opensearch.common.network.InetAddresses) SpanNotQuery(org.apache.lucene.search.spans.SpanNotQuery) QueryBitSetProducer(org.apache.lucene.search.join.QueryBitSetProducer) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) CombineFunction(org.opensearch.common.lucene.search.function.CombineFunction) IntPoint(org.apache.lucene.document.IntPoint) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) FloatPoint(org.apache.lucene.document.FloatPoint) HalfFloatPoint(org.apache.lucene.document.HalfFloatPoint) QueryAnalyzer.selectBestResult(org.opensearch.percolator.QueryAnalyzer.selectBestResult) BooleanClause(org.apache.lucene.search.BooleanClause) Consumer(java.util.function.Consumer) TermQuery(org.apache.lucene.search.TermQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) BoostQuery(org.apache.lucene.search.BoostQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) Intervals(org.apache.lucene.queries.intervals.Intervals) OpenSearchToParentBlockJoinQuery(org.opensearch.index.search.OpenSearchToParentBlockJoinQuery) Comparator(java.util.Comparator) Collections(java.util.Collections) QueryExtraction(org.opensearch.percolator.QueryAnalyzer.QueryExtraction) PhraseQuery(org.apache.lucene.search.PhraseQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) ArrayList(java.util.ArrayList) Result(org.opensearch.percolator.QueryAnalyzer.Result) QueryAnalyzer.selectBestResult(org.opensearch.percolator.QueryAnalyzer.selectBestResult)

Example 7 with QueryExtraction

use of org.opensearch.percolator.QueryAnalyzer.QueryExtraction in project OpenSearch by opensearch-project.

the class QueryAnalyzerTests method terms.

private static Set<QueryExtraction> terms(int[] intervals, String... values) {
    Set<QueryExtraction> queryExtractions = new HashSet<>();
    for (int interval : intervals) {
        byte[] encodedInterval = new byte[4];
        IntPoint.encodeDimension(interval, encodedInterval, 0);
        queryExtractions.add(new QueryAnalyzer.QueryExtraction(new QueryAnalyzer.Range("_field", null, null, encodedInterval)));
    }
    for (String value : values) {
        queryExtractions.add(new QueryExtraction(new Term("_field", value)));
    }
    return queryExtractions;
}
Also used : QueryExtraction(org.opensearch.percolator.QueryAnalyzer.QueryExtraction) QueryExtraction(org.opensearch.percolator.QueryAnalyzer.QueryExtraction) Term(org.apache.lucene.index.Term) DoublePoint(org.apache.lucene.document.DoublePoint) LatLonPoint(org.apache.lucene.document.LatLonPoint) InetAddressPoint(org.apache.lucene.document.InetAddressPoint) LongPoint(org.apache.lucene.document.LongPoint) IntPoint(org.apache.lucene.document.IntPoint) FloatPoint(org.apache.lucene.document.FloatPoint) HalfFloatPoint(org.apache.lucene.document.HalfFloatPoint) HashSet(java.util.HashSet)

Example 8 with QueryExtraction

use of org.opensearch.percolator.QueryAnalyzer.QueryExtraction in project OpenSearch by opensearch-project.

the class QueryAnalyzerTests method testExtractQueryMetadata_termsQuery.

public void testExtractQueryMetadata_termsQuery() {
    TermInSetQuery termsQuery = new TermInSetQuery("_field", new BytesRef("_term1"), new BytesRef("_term2"));
    Result result = analyze(termsQuery, Version.CURRENT);
    assertThat(result.verified, is(true));
    assertThat(result.minimumShouldMatch, equalTo(1));
    List<QueryExtraction> terms = new ArrayList<>(result.extractions);
    terms.sort(Comparator.comparing(qt -> qt.term));
    assertThat(terms.size(), equalTo(2));
    assertThat(terms.get(0).field(), equalTo("_field"));
    assertThat(terms.get(0).text(), equalTo("_term1"));
    assertThat(terms.get(1).field(), equalTo("_field"));
    assertThat(terms.get(1).text(), equalTo("_term2"));
}
Also used : Query(org.apache.lucene.search.Query) Arrays(java.util.Arrays) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) Term(org.apache.lucene.index.Term) CommonTermsQuery(org.apache.lucene.queries.CommonTermsQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) Version(org.opensearch.Version) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) DoublePoint(org.apache.lucene.document.DoublePoint) QueryAnalyzer.analyze(org.opensearch.percolator.QueryAnalyzer.analyze) IntervalsSource(org.apache.lucene.queries.intervals.IntervalsSource) LatLonPoint(org.apache.lucene.document.LatLonPoint) Occur(org.apache.lucene.search.BooleanClause.Occur) FunctionScoreQuery(org.opensearch.common.lucene.search.function.FunctionScoreQuery) InetAddressPoint(org.apache.lucene.document.InetAddressPoint) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) SpanFirstQuery(org.apache.lucene.search.spans.SpanFirstQuery) BytesRef(org.apache.lucene.util.BytesRef) Result(org.opensearch.percolator.QueryAnalyzer.Result) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Set(java.util.Set) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) ScoreMode(org.apache.lucene.search.join.ScoreMode) Collectors(java.util.stream.Collectors) IsCollectionWithSize.hasSize(org.hamcrest.collection.IsCollectionWithSize.hasSize) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) List(java.util.List) IntervalQuery(org.apache.lucene.queries.intervals.IntervalQuery) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.is(org.hamcrest.Matchers.is) RandomScoreFunction(org.opensearch.common.lucene.search.function.RandomScoreFunction) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) QueryExtraction(org.opensearch.percolator.QueryAnalyzer.QueryExtraction) LongPoint(org.apache.lucene.document.LongPoint) InetAddresses(org.opensearch.common.network.InetAddresses) SpanNotQuery(org.apache.lucene.search.spans.SpanNotQuery) QueryBitSetProducer(org.apache.lucene.search.join.QueryBitSetProducer) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) CombineFunction(org.opensearch.common.lucene.search.function.CombineFunction) IntPoint(org.apache.lucene.document.IntPoint) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) FloatPoint(org.apache.lucene.document.FloatPoint) HalfFloatPoint(org.apache.lucene.document.HalfFloatPoint) QueryAnalyzer.selectBestResult(org.opensearch.percolator.QueryAnalyzer.selectBestResult) BooleanClause(org.apache.lucene.search.BooleanClause) Consumer(java.util.function.Consumer) TermQuery(org.apache.lucene.search.TermQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) BoostQuery(org.apache.lucene.search.BoostQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) Intervals(org.apache.lucene.queries.intervals.Intervals) OpenSearchToParentBlockJoinQuery(org.opensearch.index.search.OpenSearchToParentBlockJoinQuery) Comparator(java.util.Comparator) Collections(java.util.Collections) QueryExtraction(org.opensearch.percolator.QueryAnalyzer.QueryExtraction) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) ArrayList(java.util.ArrayList) BytesRef(org.apache.lucene.util.BytesRef) Result(org.opensearch.percolator.QueryAnalyzer.Result) QueryAnalyzer.selectBestResult(org.opensearch.percolator.QueryAnalyzer.selectBestResult)

Example 9 with QueryExtraction

use of org.opensearch.percolator.QueryAnalyzer.QueryExtraction in project OpenSearch by opensearch-project.

the class QueryAnalyzerTests method testBooleanQueryWithMustAndShouldClauses.

public void testBooleanQueryWithMustAndShouldClauses() {
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    TermQuery termQuery1 = new TermQuery(new Term("_field", "_term1"));
    builder.add(termQuery1, BooleanClause.Occur.SHOULD);
    TermQuery termQuery2 = new TermQuery(new Term("_field", "_term2"));
    builder.add(termQuery2, BooleanClause.Occur.SHOULD);
    TermQuery termQuery3 = new TermQuery(new Term("_field", "_term3"));
    builder.add(termQuery3, BooleanClause.Occur.MUST);
    Result result = analyze(builder.build(), Version.CURRENT);
    assertThat("Must clause is exact, so this is a verified candidate match", result.verified, is(true));
    assertThat(result.minimumShouldMatch, equalTo(1));
    assertThat(result.extractions.size(), equalTo(1));
    List<QueryExtraction> extractions = new ArrayList<>(result.extractions);
    assertThat(extractions.get(0).term, equalTo(new Term("_field", "_term3")));
    builder.setMinimumNumberShouldMatch(1);
    result = analyze(builder.build(), Version.CURRENT);
    assertThat("Must clause is exact, but m_s_m is 1 so one should clause must match too", result.verified, is(false));
    assertThat(result.minimumShouldMatch, equalTo(2));
    assertTermsEqual(result.extractions, termQuery1.getTerm(), termQuery2.getTerm(), termQuery3.getTerm());
    builder = new BooleanQuery.Builder();
    BooleanQuery.Builder innerBuilder = new BooleanQuery.Builder();
    innerBuilder.setMinimumNumberShouldMatch(2);
    innerBuilder.add(termQuery1, BooleanClause.Occur.SHOULD);
    innerBuilder.add(termQuery2, BooleanClause.Occur.SHOULD);
    builder.add(innerBuilder.build(), BooleanClause.Occur.MUST);
    builder.add(termQuery3, BooleanClause.Occur.MUST);
    result = analyze(builder.build(), Version.CURRENT);
    assertThat("Verified, because m_s_m is specified in an inner clause and not top level clause", result.verified, is(true));
    assertThat(result.minimumShouldMatch, equalTo(3));
    assertThat(result.extractions.size(), equalTo(3));
    extractions = new ArrayList<>(result.extractions);
    extractions.sort(Comparator.comparing(key -> key.term));
    assertThat(extractions.get(0).term, equalTo(new Term("_field", "_term1")));
    assertThat(extractions.get(1).term, equalTo(new Term("_field", "_term2")));
    assertThat(extractions.get(2).term, equalTo(new Term("_field", "_term3")));
    builder = new BooleanQuery.Builder();
    builder.add(innerBuilder.build(), BooleanClause.Occur.SHOULD);
    builder.add(termQuery3, BooleanClause.Occur.MUST);
    result = analyze(builder.build(), Version.CURRENT);
    assertThat("Verified, because m_s_m is specified in an inner clause and not top level clause", result.verified, is(true));
    assertThat(result.minimumShouldMatch, equalTo(1));
    assertThat(result.extractions.size(), equalTo(1));
    extractions = new ArrayList<>(result.extractions);
    assertThat(extractions.get(0).term, equalTo(new Term("_field", "_term3")));
}
Also used : Query(org.apache.lucene.search.Query) Arrays(java.util.Arrays) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) Term(org.apache.lucene.index.Term) CommonTermsQuery(org.apache.lucene.queries.CommonTermsQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) Version(org.opensearch.Version) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) DoublePoint(org.apache.lucene.document.DoublePoint) QueryAnalyzer.analyze(org.opensearch.percolator.QueryAnalyzer.analyze) IntervalsSource(org.apache.lucene.queries.intervals.IntervalsSource) LatLonPoint(org.apache.lucene.document.LatLonPoint) Occur(org.apache.lucene.search.BooleanClause.Occur) FunctionScoreQuery(org.opensearch.common.lucene.search.function.FunctionScoreQuery) InetAddressPoint(org.apache.lucene.document.InetAddressPoint) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) SpanFirstQuery(org.apache.lucene.search.spans.SpanFirstQuery) BytesRef(org.apache.lucene.util.BytesRef) Result(org.opensearch.percolator.QueryAnalyzer.Result) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Set(java.util.Set) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) ScoreMode(org.apache.lucene.search.join.ScoreMode) Collectors(java.util.stream.Collectors) IsCollectionWithSize.hasSize(org.hamcrest.collection.IsCollectionWithSize.hasSize) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) List(java.util.List) IntervalQuery(org.apache.lucene.queries.intervals.IntervalQuery) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.is(org.hamcrest.Matchers.is) RandomScoreFunction(org.opensearch.common.lucene.search.function.RandomScoreFunction) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) QueryExtraction(org.opensearch.percolator.QueryAnalyzer.QueryExtraction) LongPoint(org.apache.lucene.document.LongPoint) InetAddresses(org.opensearch.common.network.InetAddresses) SpanNotQuery(org.apache.lucene.search.spans.SpanNotQuery) QueryBitSetProducer(org.apache.lucene.search.join.QueryBitSetProducer) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) CombineFunction(org.opensearch.common.lucene.search.function.CombineFunction) IntPoint(org.apache.lucene.document.IntPoint) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) FloatPoint(org.apache.lucene.document.FloatPoint) HalfFloatPoint(org.apache.lucene.document.HalfFloatPoint) QueryAnalyzer.selectBestResult(org.opensearch.percolator.QueryAnalyzer.selectBestResult) BooleanClause(org.apache.lucene.search.BooleanClause) Consumer(java.util.function.Consumer) TermQuery(org.apache.lucene.search.TermQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) BoostQuery(org.apache.lucene.search.BoostQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) Intervals(org.apache.lucene.queries.intervals.Intervals) OpenSearchToParentBlockJoinQuery(org.opensearch.index.search.OpenSearchToParentBlockJoinQuery) Comparator(java.util.Comparator) Collections(java.util.Collections) BooleanQuery(org.apache.lucene.search.BooleanQuery) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) QueryExtraction(org.opensearch.percolator.QueryAnalyzer.QueryExtraction) ArrayList(java.util.ArrayList) Term(org.apache.lucene.index.Term) Result(org.opensearch.percolator.QueryAnalyzer.Result) QueryAnalyzer.selectBestResult(org.opensearch.percolator.QueryAnalyzer.selectBestResult)

Example 10 with QueryExtraction

use of org.opensearch.percolator.QueryAnalyzer.QueryExtraction in project OpenSearch by opensearch-project.

the class QueryAnalyzerTests method testToParentBlockJoinQuery.

public void testToParentBlockJoinQuery() {
    TermQuery termQuery = new TermQuery(new Term("field", "value"));
    QueryBitSetProducer queryBitSetProducer = new QueryBitSetProducer(new TermQuery(new Term("_type", "child")));
    OpenSearchToParentBlockJoinQuery query = new OpenSearchToParentBlockJoinQuery(termQuery, queryBitSetProducer, ScoreMode.None, "child");
    Result result = analyze(query, Version.CURRENT);
    assertFalse(result.verified);
    assertThat(result.minimumShouldMatch, equalTo(1));
    assertEquals(1, result.extractions.size());
    assertNull(result.extractions.toArray(new QueryExtraction[0])[0].range);
    assertEquals(new Term("field", "value"), result.extractions.toArray(new QueryExtraction[0])[0].term);
}
Also used : BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) QueryExtraction(org.opensearch.percolator.QueryAnalyzer.QueryExtraction) OpenSearchToParentBlockJoinQuery(org.opensearch.index.search.OpenSearchToParentBlockJoinQuery) QueryBitSetProducer(org.apache.lucene.search.join.QueryBitSetProducer) Term(org.apache.lucene.index.Term) Result(org.opensearch.percolator.QueryAnalyzer.Result) QueryAnalyzer.selectBestResult(org.opensearch.percolator.QueryAnalyzer.selectBestResult)

Aggregations

QueryExtraction (org.opensearch.percolator.QueryAnalyzer.QueryExtraction)13 Term (org.apache.lucene.index.Term)12 Result (org.opensearch.percolator.QueryAnalyzer.Result)12 QueryAnalyzer.selectBestResult (org.opensearch.percolator.QueryAnalyzer.selectBestResult)12 BlendedTermQuery (org.apache.lucene.queries.BlendedTermQuery)10 TermQuery (org.apache.lucene.search.TermQuery)10 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)10 ArrayList (java.util.ArrayList)9 HashSet (java.util.HashSet)8 DoublePoint (org.apache.lucene.document.DoublePoint)8 FloatPoint (org.apache.lucene.document.FloatPoint)8 HalfFloatPoint (org.apache.lucene.document.HalfFloatPoint)8 InetAddressPoint (org.apache.lucene.document.InetAddressPoint)8 IntPoint (org.apache.lucene.document.IntPoint)8 LatLonPoint (org.apache.lucene.document.LatLonPoint)8 LongPoint (org.apache.lucene.document.LongPoint)8 BoostQuery (org.apache.lucene.search.BoostQuery)7 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)7 Arrays (java.util.Arrays)6 Collections (java.util.Collections)6