use of org.opensearch.index.query.MultiMatchQueryBuilder in project bw-calendar-engine by Bedework.
the class ESQueryFilter method makeQuery.
private QueryBuilder makeQuery(final PropertyInfoIndex pii, final String path, final String fullTermPath, final Object val, final OperationType opType, final float boost, final boolean negate) throws CalFacadeException {
final BwIcalPropertyInfoEntry bwPie = BwIcalPropertyInfo.getPinfo(pii);
QueryBuilder qb;
switch(opType) {
case compare:
if (bwPie.getAnalyzed()) {
if (val instanceof Collection) {
final String[] vals;
try {
final Collection valsC = (Collection) val;
vals = (String[]) (valsC).toArray(new String[valsC.size()]);
} catch (final Throwable t) {
throw new CalFacadeException(CalFacadeException.filterBadOperator, "Invalid query. Multi match only allowed on strings");
}
qb = new MultiMatchQueryBuilder(path, vals);
break;
}
if (bwPie.getTermsField() == null) {
qb = new MatchQueryBuilder(path, val).operator(Operator.AND);
break;
}
/* Build something like this
"query" : {
"function_score" : {
"query" : {
"match" : {
"loc_all" : {
"query" : "hill",
"operator" : "AND"
}
}
},
"functions" : [{
"weight": 10,
"filter": {
"term" : {
"loc_all_terms" : "hill"
}
}
}]
}
},...
*/
qb = new MatchQueryBuilder(path, val).operator(Operator.AND);
if (boost != 1) {
qb.boost(boost);
}
break;
}
if (val instanceof Collection) {
qb = new TermsQueryBuilder(path, (Collection) val);
} else {
qb = new TermQueryBuilder(path, val);
}
if (boost != 1) {
qb.boost(boost);
}
break;
case prefix:
qb = prefixQuery(path, (String) val);
break;
case timeRange:
final RangeQueryBuilder rqb = QueryBuilders.rangeQuery(path);
qb = rqb;
final TimeRange tr = (TimeRange) val;
if (tr.getEnd() == null) {
rqb.gte(tr.getStart().toString());
break;
}
if (tr.getStart() == null) {
rqb.lt(tr.getEnd().toString());
break;
}
rqb.from(tr.getStart().toString());
rqb.to(tr.getEnd().toString());
rqb.includeLower(true);
rqb.includeUpper(false);
break;
case absence:
qb = not(QueryBuilders.existsQuery(path));
break;
case presence:
qb = QueryBuilders.existsQuery(path);
break;
default:
throw new CalFacadeException(CalFacadeException.filterBadOperator, opType.toString());
}
if (negate) {
qb = not(qb);
}
return qb;
}
use of org.opensearch.index.query.MultiMatchQueryBuilder in project OpenSearch by opensearch-project.
the class DisableGraphQueryTests method testMultiMatchQuery.
public void testMultiMatchQuery() throws IOException {
MultiMatchQueryBuilder builder = new MultiMatchQueryBuilder("foo bar baz", "text_shingle_unigram");
Query query = builder.toQuery(shardContext);
assertThat(expectedQueryWithUnigram, equalTo(query));
builder.type(MatchQuery.Type.PHRASE);
query = builder.toQuery(shardContext);
assertThat(expectedPhraseQueryWithUnigram, equalTo(query));
builder = new MultiMatchQueryBuilder("foo bar baz biz", "text_shingle");
query = builder.toQuery(shardContext);
assertThat(expectedQuery, equalTo(query));
builder.type(MatchQuery.Type.PHRASE);
query = builder.toQuery(shardContext);
assertThat(expectedPhraseQuery, equalTo(query));
}
use of org.opensearch.index.query.MultiMatchQueryBuilder in project OpenSearch by opensearch-project.
the class SearchQueryIT method testMultiMatchQuery.
public void testMultiMatchQuery() throws Exception {
createIndex("test");
indexRandom(true, client().prepareIndex("test").setId("1").setSource("field1", "value1", "field2", "value4", "field3", "value3"), client().prepareIndex("test").setId("2").setSource("field1", "value2", "field2", "value5", "field3", "value2"), client().prepareIndex("test").setId("3").setSource("field1", "value3", "field2", "value6", "field3", "value1"));
MultiMatchQueryBuilder builder = multiMatchQuery("value1 value2 value4", "field1", "field2");
SearchResponse searchResponse = client().prepareSearch().setQuery(builder).addAggregation(AggregationBuilders.terms("field1").field("field1.keyword")).get();
assertHitCount(searchResponse, 2L);
// this uses dismax so scores are equal and the order can be arbitrary
assertSearchHits(searchResponse, "1", "2");
searchResponse = client().prepareSearch().setQuery(builder).get();
assertHitCount(searchResponse, 2L);
assertSearchHits(searchResponse, "1", "2");
client().admin().indices().prepareRefresh("test").get();
// Operator only applies on terms inside a field!
builder = multiMatchQuery("value1", "field1", "field2").operator(Operator.AND);
// Fields are always OR-ed together.
searchResponse = client().prepareSearch().setQuery(builder).get();
assertHitCount(searchResponse, 1L);
assertFirstHit(searchResponse, hasId("1"));
refresh();
// Operator only applies on terms inside
builder = multiMatchQuery("value1", "field1").field("field3", 1.5f).operator(Operator.AND);
// a field! Fields are always OR-ed
// together.
searchResponse = client().prepareSearch().setQuery(builder).get();
assertHitCount(searchResponse, 2L);
assertSearchHits(searchResponse, "3", "1");
client().admin().indices().prepareRefresh("test").get();
// Operator only applies on terms
builder = multiMatchQuery("value1").field("field1").field("field3", 1.5f).operator(Operator.AND);
// inside a field! Fields are
// always OR-ed together.
searchResponse = client().prepareSearch().setQuery(builder).get();
assertHitCount(searchResponse, 2L);
assertSearchHits(searchResponse, "3", "1");
// Test lenient
client().prepareIndex("test").setId("3").setSource("field1", "value7", "field2", "value8", "field4", 5).get();
refresh();
builder = multiMatchQuery("value1", "field1", "field2", "field4");
assertFailures(client().prepareSearch().setQuery(builder), RestStatus.BAD_REQUEST, containsString("NumberFormatException[For input string: \"value1\"]"));
builder.lenient(true);
searchResponse = client().prepareSearch().setQuery(builder).get();
assertHitCount(searchResponse, 1L);
assertFirstHit(searchResponse, hasId("1"));
}
use of org.opensearch.index.query.MultiMatchQueryBuilder in project OpenSearch by opensearch-project.
the class SearchQueryIT method testMultiMatchQueryMinShouldMatch.
public void testMultiMatchQueryMinShouldMatch() {
createIndex("test");
client().prepareIndex("test").setId("1").setSource("field1", new String[] { "value1", "value2", "value3" }).get();
client().prepareIndex("test").setId("2").setSource("field2", "value1").get();
refresh();
MultiMatchQueryBuilder multiMatchQuery = multiMatchQuery("value1 value2 foo", "field1", "field2");
multiMatchQuery.minimumShouldMatch("70%");
SearchResponse searchResponse = client().prepareSearch().setQuery(multiMatchQuery).get();
assertHitCount(searchResponse, 1L);
assertFirstHit(searchResponse, hasId("1"));
multiMatchQuery.minimumShouldMatch("30%");
searchResponse = client().prepareSearch().setQuery(multiMatchQuery).get();
assertHitCount(searchResponse, 2L);
assertFirstHit(searchResponse, hasId("1"));
assertSecondHit(searchResponse, hasId("2"));
multiMatchQuery.minimumShouldMatch("70%");
searchResponse = client().prepareSearch().setQuery(multiMatchQuery).get();
assertHitCount(searchResponse, 1L);
assertFirstHit(searchResponse, hasId("1"));
multiMatchQuery.minimumShouldMatch("30%");
searchResponse = client().prepareSearch().setQuery(multiMatchQuery).get();
assertHitCount(searchResponse, 2L);
assertFirstHit(searchResponse, hasId("1"));
assertSecondHit(searchResponse, hasId("2"));
multiMatchQuery = multiMatchQuery("value1 value2 bar", "field1");
multiMatchQuery.minimumShouldMatch("100%");
searchResponse = client().prepareSearch().setQuery(multiMatchQuery).get();
assertHitCount(searchResponse, 0L);
multiMatchQuery.minimumShouldMatch("70%");
searchResponse = client().prepareSearch().setQuery(multiMatchQuery).get();
assertHitCount(searchResponse, 1L);
assertFirstHit(searchResponse, hasId("1"));
// Min should match > # optional clauses returns no docs.
multiMatchQuery = multiMatchQuery("value1 value2 value3", "field1", "field2");
multiMatchQuery.minimumShouldMatch("4");
searchResponse = client().prepareSearch().setQuery(multiMatchQuery).get();
assertHitCount(searchResponse, 0L);
}
use of org.opensearch.index.query.MultiMatchQueryBuilder in project OpenSearch by opensearch-project.
the class MultiMatchQueryIT method testSingleField.
public void testSingleField() throws NoSuchFieldException, IllegalAccessException {
SearchResponse searchResponse = client().prepareSearch("test").setQuery(randomizeType(multiMatchQuery("15", "skill"))).get();
assertNoFailures(searchResponse);
assertFirstHit(searchResponse, hasId("theone"));
searchResponse = client().prepareSearch("test").setQuery(randomizeType(multiMatchQuery("15", "skill", "int-field")).analyzer("category")).get();
assertNoFailures(searchResponse);
assertFirstHit(searchResponse, hasId("theone"));
String[] fields = { "full_name", "first_name", "last_name", "last_name_phrase", "first_name_phrase", "category_phrase", "category", "missing_field", "missing_fields*" };
String[] query = { "marvel", "hero", "captain", "america", "15", "17", "1", "5", "ultimate", "Man", "marvel", "wolferine", "ninja" };
// check if it's equivalent to a match query.
int numIters = scaledRandomIntBetween(10, 100);
for (int i = 0; i < numIters; i++) {
String field = RandomPicks.randomFrom(random(), fields);
int numTerms = randomIntBetween(1, query.length);
StringBuilder builder = new StringBuilder();
for (int j = 0; j < numTerms; j++) {
builder.append(RandomPicks.randomFrom(random(), query)).append(" ");
}
MultiMatchQueryBuilder multiMatchQueryBuilder = randomizeType(multiMatchQuery(builder.toString(), field));
SearchResponse multiMatchResp = client().prepareSearch("test").addSort("_score", SortOrder.DESC).addSort("id", SortOrder.ASC).setQuery(multiMatchQueryBuilder).get();
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery(field, builder.toString());
SearchResponse matchResp = client().prepareSearch("test").addSort("_score", SortOrder.DESC).addSort("id", SortOrder.ASC).setQuery(matchQueryBuilder).get();
assertThat("field: " + field + " query: " + builder.toString(), multiMatchResp.getHits().getTotalHits().value, equalTo(matchResp.getHits().getTotalHits().value));
SearchHits hits = multiMatchResp.getHits();
if (field.startsWith("missing")) {
assertEquals(0, hits.getHits().length);
}
for (int j = 0; j < hits.getHits().length; j++) {
assertThat(hits.getHits()[j].getScore(), equalTo(matchResp.getHits().getHits()[j].getScore()));
assertThat(hits.getHits()[j].getId(), equalTo(matchResp.getHits().getHits()[j].getId()));
}
}
}
Aggregations