use of org.opensearch.percolator.QueryAnalyzer.QueryExtraction in project OpenSearch by opensearch-project.
the class QueryAnalyzerTests method testExtractQueryMetadata_commonTermsQuery.
public void testExtractQueryMetadata_commonTermsQuery() {
CommonTermsQuery commonTermsQuery = new CommonTermsQuery(BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD, 100);
commonTermsQuery.add(new Term("_field", "_term1"));
commonTermsQuery.add(new Term("_field", "_term2"));
Result result = analyze(commonTermsQuery, Version.CURRENT);
assertThat(result.verified, is(false));
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(result.minimumShouldMatch, equalTo(1));
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"));
}
use of org.opensearch.percolator.QueryAnalyzer.QueryExtraction in project OpenSearch by opensearch-project.
the class QueryAnalyzerTests method testExtractQueryMetadata_booleanQuery.
public void testExtractQueryMetadata_booleanQuery() {
BooleanQuery.Builder builder = new BooleanQuery.Builder();
TermQuery termQuery1 = new TermQuery(new Term("_field", "term0"));
builder.add(termQuery1, BooleanClause.Occur.SHOULD);
PhraseQuery phraseQuery = new PhraseQuery("_field", "term1", "term2");
builder.add(phraseQuery, BooleanClause.Occur.SHOULD);
BooleanQuery.Builder subBuilder = new BooleanQuery.Builder();
TermQuery termQuery2 = new TermQuery(new Term("_field1", "term4"));
subBuilder.add(termQuery2, BooleanClause.Occur.MUST);
TermQuery termQuery3 = new TermQuery(new Term("_field3", "term5"));
subBuilder.add(termQuery3, BooleanClause.Occur.MUST);
builder.add(subBuilder.build(), BooleanClause.Occur.SHOULD);
BooleanQuery booleanQuery = builder.build();
Result result = analyze(booleanQuery, Version.CURRENT);
assertThat("Should clause with phrase query isn't verified, so entire query can't be verified", result.verified, is(false));
assertThat(result.minimumShouldMatch, equalTo(1));
List<QueryExtraction> terms = new ArrayList<>(result.extractions);
terms.sort(Comparator.comparing(qt -> qt.term));
assertThat(terms.size(), equalTo(5));
assertThat(terms.get(0).field(), equalTo(termQuery1.getTerm().field()));
assertThat(terms.get(0).bytes(), equalTo(termQuery1.getTerm().bytes()));
assertThat(terms.get(1).field(), equalTo(phraseQuery.getTerms()[0].field()));
assertThat(terms.get(1).bytes(), equalTo(phraseQuery.getTerms()[0].bytes()));
assertThat(terms.get(2).field(), equalTo(phraseQuery.getTerms()[1].field()));
assertThat(terms.get(2).bytes(), equalTo(phraseQuery.getTerms()[1].bytes()));
assertThat(terms.get(3).field(), equalTo(termQuery2.getTerm().field()));
assertThat(terms.get(3).bytes(), equalTo(termQuery2.getTerm().bytes()));
assertThat(terms.get(4).field(), equalTo(termQuery3.getTerm().field()));
assertThat(terms.get(4).bytes(), equalTo(termQuery3.getTerm().bytes()));
}
use of org.opensearch.percolator.QueryAnalyzer.QueryExtraction in project OpenSearch by opensearch-project.
the class QueryAnalyzerTests method testExtractQueryMetadata_termQuery.
public void testExtractQueryMetadata_termQuery() {
TermQuery termQuery = new TermQuery(new Term("_field", "_term"));
Result result = analyze(termQuery, 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(termQuery.getTerm().field()));
assertThat(terms.get(0).bytes(), equalTo(termQuery.getTerm().bytes()));
}
use of org.opensearch.percolator.QueryAnalyzer.QueryExtraction in project OpenSearch by opensearch-project.
the class QueryAnalyzerTests method testselectBestResult_random.
public void testselectBestResult_random() {
Set<QueryExtraction> terms1 = new HashSet<>();
int shortestTerms1Length = Integer.MAX_VALUE;
int sumTermLength = randomIntBetween(1, 128);
while (sumTermLength > 0) {
int length = randomInt(sumTermLength);
shortestTerms1Length = Math.min(shortestTerms1Length, length);
terms1.add(new QueryExtraction(new Term("field", randomAlphaOfLength(length))));
sumTermLength -= length;
}
Set<QueryExtraction> terms2 = new HashSet<>();
int shortestTerms2Length = Integer.MAX_VALUE;
sumTermLength = randomIntBetween(1, 128);
while (sumTermLength > 0) {
int length = randomInt(sumTermLength);
shortestTerms2Length = Math.min(shortestTerms2Length, length);
terms2.add(new QueryExtraction(new Term("field", randomAlphaOfLength(length))));
sumTermLength -= length;
}
Result result1 = new Result(true, terms1, 1);
Result result2 = new Result(true, terms2, 1);
Result result = selectBestResult(result1, result2);
Set<QueryExtraction> expected = shortestTerms1Length >= shortestTerms2Length ? terms1 : terms2;
assertThat(result.extractions, sameInstance(expected));
}
use of org.opensearch.percolator.QueryAnalyzer.QueryExtraction in project OpenSearch by opensearch-project.
the class QueryAnalyzerTests method testExtractQueryMetadata_multiPhraseQuery.
public void testExtractQueryMetadata_multiPhraseQuery() {
MultiPhraseQuery multiPhraseQuery = new MultiPhraseQuery.Builder().add(new Term("_field", "_term1")).add(new Term[] { new Term("_field", "_term2"), new Term("_field", "_term3") }).add(new Term[] { new Term("_field", "_term4"), new Term("_field", "_term5") }).add(new Term[] { new Term("_field", "_term6") }).build();
Result result = analyze(multiPhraseQuery, Version.CURRENT);
assertThat(result.verified, is(false));
assertThat(result.minimumShouldMatch, equalTo(4));
List<QueryExtraction> terms = new ArrayList<>(result.extractions);
terms.sort(Comparator.comparing(qt -> qt.term));
assertThat(terms.size(), equalTo(6));
assertThat(terms.get(0).field(), equalTo("_field"));
assertThat(terms.get(0).bytes().utf8ToString(), equalTo("_term1"));
assertThat(terms.get(1).field(), equalTo("_field"));
assertThat(terms.get(1).bytes().utf8ToString(), equalTo("_term2"));
assertThat(terms.get(2).field(), equalTo("_field"));
assertThat(terms.get(2).bytes().utf8ToString(), equalTo("_term3"));
assertThat(terms.get(3).field(), equalTo("_field"));
assertThat(terms.get(3).bytes().utf8ToString(), equalTo("_term4"));
assertThat(terms.get(4).field(), equalTo("_field"));
assertThat(terms.get(4).bytes().utf8ToString(), equalTo("_term5"));
assertThat(terms.get(5).field(), equalTo("_field"));
assertThat(terms.get(5).bytes().utf8ToString(), equalTo("_term6"));
}
Aggregations