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