/** * Add a "must_not" clause to a bool query. Do not add it if the clause * already exists in the query. * @param bool the bool query * @param mustNot the clause to add */ public static void boolAddMustNot(JsonObject bool, JsonObject mustNot) { boolAdd(bool, mustNot, "must_not"); }
result = ElasticsearchQueryHelper.termQuery("tags", search); } else if (queryPart instanceof KeyValueQueryPart) { KeyValueQueryPart kvqp = (KeyValueQueryPart)queryPart; result = ElasticsearchQueryHelper.termQuery("props." + key, value); break; case GT: result = ElasticsearchQueryHelper.gtQuery("props." + key, value); break; case GTE: result = ElasticsearchQueryHelper.gteQuery("props." + key, value); break; case LT: result = ElasticsearchQueryHelper.ltQuery("props." + key, value); break; case LTE: result = ElasticsearchQueryHelper.lteQuery("props." + key, value); break; JsonObject cq = ElasticsearchQueryHelper.termQuery("correlationId", value); if (result != null) { JsonObject bool = ElasticsearchQueryHelper.boolQuery(1); ElasticsearchQueryHelper.boolAddShould(bool, result); ElasticsearchQueryHelper.boolAddShould(bool, cq); result = bool; } else {
String prefix = PathUtils.addTrailingSlash(path); JsonObject qi = boolQuery(1); boolAddShould(qi, termQuery("path", path)); boolAddShould(qi, prefixQuery("path", prefix)); filter.add(qi); filter.add(existsQuery(keyExists)); JsonObject qr = boolQuery(1); boolAddShould(qr, qb); filter.forEach(q -> boolAddMust(qr, q));
switch (l) { case OR: boolAddShould(b, other); break; case AND: boolAddMust(b, other); break; case NOT: boolAddMustNot(b, other); break; JsonObject bqb = boolQuery(1); Logical l = currentLogical.peek(); switch (l) { case OR: boolAddShould(bqb, b); boolAddShould(bqb, other); break; case AND: boolAddMust(bqb, b); boolAddMust(bqb, other); break; case NOT: boolAddMustNot(bqb, b); boolAddMustNot(bqb, other); break;
/** * Test that {@link ElasticsearchQueryHelper#boolAddShould(JsonObject, JsonObject)} * does not add duplicated queries */ @Test public void testBoolAddShouldNoDuplicate() { JsonObject s = new JsonObject() .put("term", new JsonObject() .put("tags", "Fraunhofer IGD")); JsonObject q = boolQuery(); boolAddShould(q, s); boolAddShould(q, s); JsonObject expected = new JsonObject() .put("bool", new JsonObject() .put("should", s)); assertEquals(expected, q); }
JsonObject bqb = boolQuery(1); for (QueryCompiler f : queryCompilers) { MatchPriority mp = f.getQueryPriority(str); return f.compileQuery(str); case SHOULD: boolAddShould(bqb, f.compileQuery(str)); break; case MUST: boolAddMust(bqb, f.compileQuery(str)); break; case NONE:
@Override public JsonObject compileQuery(QueryPart queryPart) { if (queryPart instanceof StringQueryPart) { // match values of all fields regardless of their name String search = ((StringQueryPart)queryPart).getSearchString(); return ElasticsearchQueryHelper.multiMatchQuery(search, "genAttrs.*"); } else if (queryPart instanceof KeyValueQueryPart) { KeyValueQueryPart kvqp = (KeyValueQueryPart)queryPart; String key = kvqp.getKey(); String value = kvqp.getValue(); ComparisonOperator comp = kvqp.getComparisonOperator(); switch (comp) { case EQ: return ElasticsearchQueryHelper.termQuery("genAttrs." + key, value); case GT: return ElasticsearchQueryHelper.gtQuery("genAttrs." + key, value); case GTE: return ElasticsearchQueryHelper.gteQuery("genAttrs." + key, value); case LT: return ElasticsearchQueryHelper.ltQuery("genAttrs." + key, value); case LTE: return ElasticsearchQueryHelper.lteQuery("genAttrs." + key, value); } } return null; } }
/** * Create a query that allows for boolean combinations of other queries * @param minimumShouldMatch the minimum number of should clauses to match * @return the query * @since 1.1.0 */ public static JsonObject boolQuery(int minimumShouldMatch) { JsonObject r = boolQuery(); r.getJsonObject("bool") .put("minimum_should_match", 1); return r; }
@Override public JsonObject compileQuery(QueryPart queryPart) { if (queryPart instanceof StringQueryPart) { String search = ((StringQueryPart)queryPart).getSearchString(); return ElasticsearchQueryHelper.termQuery("gmlIds", search); } else if (queryPart instanceof KeyValueQueryPart) { KeyValueQueryPart kvqp = (KeyValueQueryPart)queryPart; if (isGmlIdEQ(kvqp)) { return ElasticsearchQueryHelper.termQuery("gmlIds", kvqp.getValue()); } } return null; } }
/** * Test {@link ElasticsearchQueryHelper#boolAddShould(JsonObject, JsonObject)} */ @Test public void testBoolAddShould() { JsonObject s = new JsonObject() .put("term", new JsonObject() .put("tags", "Fraunhofer IGD")); JsonObject q = boolQuery(); boolAddShould(q, s); JsonObject expected = new JsonObject() .put("bool", new JsonObject() .put("should", s)); assertEquals(expected, q); }
@Override public JsonObject compileQuery(QueryPart queryPart) { if (queryPart instanceof StringQueryPart) { // match values of all fields regardless of their name String search = ((StringQueryPart)queryPart).getSearchString(); return multiMatchQuery(search, "address.*"); } else if (queryPart instanceof KeyValueQueryPart) { KeyValueQueryPart kvqp = (KeyValueQueryPart)queryPart; String key = kvqp.getKey(); String value = kvqp.getValue(); ComparisonOperator comp = kvqp.getComparisonOperator(); String name = "address." + key; switch (comp) { case EQ: return termQuery(name, value); case GT: return gtQuery(name, value); case GTE: return gteQuery(name, value); case LT: return ltQuery(name, value); case LTE: return lteQuery(name, value); } } return null; } }
/** * Enter a logical expression * @param l the logical operation */ private void enterLogical(Logical l) { JsonObject bqb = boolQuery(1); combine(bqb); result.push(bqb); currentLogical.push(l); }
/** * Test that {@link ElasticsearchQueryHelper#boolAddShould(JsonObject, JsonObject)} * does not add queries specified multiple times */ @Test public void testBoolAddShouldNoMultiple() { JsonObject s = new JsonObject() .put("term", new JsonObject() .put("tags", "Fraunhofer IGD")); JsonObject s2 = new JsonObject() .put("term", new JsonObject() .put("tags", "Elvis")); JsonObject q = boolQuery(); boolAddShould(q, s); boolAddShould(q, s2); boolAddShould(q, s); boolAddShould(q, s); JsonObject expected = new JsonObject() .put("bool", new JsonObject() .put("should", new JsonArray() .add(s) .add(s2))); assertEquals(expected, q); } }
/** * Add a "should" clause to a bool query. Do not add it if the clause already * exists in the query. * @param bool the bool query * @param should the clause to add */ public static void boolAddShould(JsonObject bool, JsonObject should) { boolAdd(bool, should, "should"); }
/** * Add a filter to a bool query. Do not add it if the clause already exists * in the query. * @param bool the bool query * @param filter the filter to add */ public static void boolAddFilter(JsonObject bool, JsonObject filter) { boolAdd(bool, filter, "filter"); }
/** * Add a "must" clause to a bool query. Do not add it if the clause already * exists in the query. * @param bool the bool query * @param must the clause to add */ public static void boolAddMust(JsonObject bool, JsonObject must) { boolAdd(bool, must, "must"); }