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);
}
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()));
}
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()));
}
Aggregations