Search in sources :

Example 11 with QueryExtraction

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

the class QueryAnalyzerTests method testSelectBestResult.

public void testSelectBestResult() {
    Set<QueryExtraction> queryTerms1 = terms(new int[0], "12", "1234", "12345");
    Result result1 = new Result(true, queryTerms1, 1);
    Set<QueryAnalyzer.QueryExtraction> queryTerms2 = terms(new int[0], "123", "1234", "12345");
    Result result2 = new Result(true, queryTerms2, 1);
    Result result = selectBestResult(result1, result2);
    assertSame(queryTerms2, result.extractions);
    assertFalse(result.verified);
    queryTerms1 = terms(new int[] { 1, 2, 3 });
    result1 = new Result(true, queryTerms1, 1);
    queryTerms2 = terms(new int[] { 2, 3, 4 });
    result2 = new Result(true, queryTerms2, 1);
    result = selectBestResult(result1, result2);
    assertSame(queryTerms1, result.extractions);
    assertFalse(result.verified);
    queryTerms1 = terms(new int[] { 4, 5, 6 });
    result1 = new Result(true, queryTerms1, 1);
    queryTerms2 = terms(new int[] { 1, 2, 3 });
    result2 = new Result(true, queryTerms2, 1);
    result = selectBestResult(result1, result2);
    assertSame(queryTerms2, result.extractions);
    assertFalse(result.verified);
    queryTerms1 = terms(new int[] { 1, 2, 3 }, "123", "456");
    result1 = new Result(true, queryTerms1, 1);
    queryTerms2 = terms(new int[] { 2, 3, 4 }, "123", "456");
    result2 = new Result(true, queryTerms2, 1);
    result = selectBestResult(result1, result2);
    assertSame(queryTerms1, result.extractions);
    assertFalse(result.verified);
    queryTerms1 = terms(new int[] { 10 });
    result1 = new Result(true, queryTerms1, 1);
    queryTerms2 = terms(new int[] { 1 });
    result2 = new Result(true, queryTerms2, 1);
    result = selectBestResult(result1, result2);
    assertSame(queryTerms2, result.extractions);
    queryTerms1 = terms(new int[] { 10 }, "123");
    result1 = new Result(true, queryTerms1, 1);
    queryTerms2 = terms(new int[] { 1 });
    result2 = new Result(true, queryTerms2, 1);
    result = selectBestResult(result1, result2);
    assertSame(queryTerms1, result.extractions);
    assertFalse(result.verified);
    queryTerms1 = terms(new int[] { 10 }, "1", "123");
    result1 = new Result(true, queryTerms1, 1);
    queryTerms2 = terms(new int[] { 1 }, "1", "2");
    result2 = new Result(true, queryTerms2, 1);
    result = selectBestResult(result1, result2);
    assertSame(queryTerms1, result.extractions);
    assertFalse(result.verified);
    queryTerms1 = terms(new int[] { 1, 2, 3 }, "123", "456");
    result1 = new Result(true, queryTerms1, 1);
    queryTerms2 = terms(new int[] { 2, 3, 4 }, "1", "456");
    result2 = new Result(true, queryTerms2, 1);
    result = selectBestResult(result1, result2);
    assertSame("Ignoring ranges, so then prefer queryTerms1, because it has the longest shortest term", queryTerms1, result.extractions);
    assertFalse(result.verified);
    queryTerms1 = terms(new int[] {});
    result1 = new Result(false, queryTerms1, 0);
    queryTerms2 = terms(new int[] {});
    result2 = new Result(false, queryTerms2, 0);
    result = selectBestResult(result1, result2);
    assertSame("In case query extractions are empty", queryTerms2, result.extractions);
    assertFalse(result.verified);
    queryTerms1 = terms(new int[] { 1 });
    result1 = new Result(true, queryTerms1, 1);
    queryTerms2 = terms(new int[] {});
    result2 = new Result(false, queryTerms2, 0);
    result = selectBestResult(result1, result2);
    assertSame("In case query a single extraction is empty", queryTerms1, result.extractions);
    assertFalse(result.verified);
    queryTerms1 = terms(new int[] {});
    result1 = new Result(false, queryTerms1, 0);
    queryTerms2 = terms(new int[] { 1 });
    result2 = new Result(true, queryTerms2, 1);
    result = selectBestResult(result1, result2);
    assertSame("In case query a single extraction is empty", queryTerms2, result.extractions);
    assertFalse(result.verified);
    result1 = new Result(true, true);
    queryTerms2 = terms(new int[] { 1 });
    result2 = new Result(true, queryTerms2, 1);
    result = selectBestResult(result1, result2);
    assertSame("Conjunction with a match_all", result2, result);
    assertTrue(result.verified);
    queryTerms1 = terms(new int[] { 1 });
    result1 = new Result(true, queryTerms2, 1);
    result2 = new Result(true, true);
    result = selectBestResult(result1, result2);
    assertSame("Conjunction with a match_all", result1, result);
    assertTrue(result.verified);
}
Also used : QueryExtraction(org.opensearch.percolator.QueryAnalyzer.QueryExtraction) Result(org.opensearch.percolator.QueryAnalyzer.Result) QueryAnalyzer.selectBestResult(org.opensearch.percolator.QueryAnalyzer.selectBestResult)

Example 12 with QueryExtraction

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

the class QueryAnalyzerTests method testExtractQueryMetadata_boostQuery.

public void testExtractQueryMetadata_boostQuery() {
    TermQuery termQuery1 = new TermQuery(new Term("_field", "_term"));
    BoostQuery constantScoreQuery = new BoostQuery(termQuery1, 1f);
    Result result = analyze(constantScoreQuery, Version.CURRENT);
    assertThat(result.verified, is(true));
    assertThat(result.minimumShouldMatch, equalTo(1));
    List<QueryExtraction> terms = new ArrayList<>(result.extractions);
    assertThat(terms.size(), equalTo(1));
    assertThat(terms.get(0).field(), equalTo(termQuery1.getTerm().field()));
    assertThat(terms.get(0).bytes(), equalTo(termQuery1.getTerm().bytes()));
}
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) ArrayList(java.util.ArrayList) Term(org.apache.lucene.index.Term) BoostQuery(org.apache.lucene.search.BoostQuery) Result(org.opensearch.percolator.QueryAnalyzer.Result) QueryAnalyzer.selectBestResult(org.opensearch.percolator.QueryAnalyzer.selectBestResult)

Example 13 with QueryExtraction

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

the class QueryAnalyzerTests method testExtractQueryMetadata_constantScoreQuery.

public void testExtractQueryMetadata_constantScoreQuery() {
    TermQuery termQuery1 = new TermQuery(new Term("_field", "_term"));
    ConstantScoreQuery constantScoreQuery = new ConstantScoreQuery(termQuery1);
    Result result = analyze(constantScoreQuery, Version.CURRENT);
    assertThat(result.verified, is(true));
    assertThat(result.minimumShouldMatch, equalTo(1));
    List<QueryExtraction> terms = new ArrayList<>(result.extractions);
    assertThat(terms.size(), equalTo(1));
    assertThat(terms.get(0).field(), equalTo(termQuery1.getTerm().field()));
    assertThat(terms.get(0).bytes(), equalTo(termQuery1.getTerm().bytes()));
}
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) ArrayList(java.util.ArrayList) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) 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