@Override public JsonObject compileQuery(String search) { JsonObject r = compileQueryNoOptimize(search); return ElasticsearchQueryOptimizer.optimize(r); }
/** * Compile a search string * @param search the search string to compile * @param path the path where to perform the search (may be null if the * whole data store should be searched) * @return the compiled query */ public JsonObject compileQuery(String search, String path) { return compileQuery(search, path, null); }
/** * Get the priority with which the Elasticsearch query returned by * {@link #compileQuery(QueryPart)} should be applied for the given * GeoRocket query part * @param queryPart the GeoRocket query part * @return the priority * @since 1.1.0 */ default MatchPriority getQueryPriority(QueryPart queryPart) { if (queryPart instanceof StringQueryPart) { return getQueryPriority(((StringQueryPart)queryPart).getSearchString()); } return MatchPriority.NONE; }
@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; } }
/** * <p>Create an Elasticsearch query for the given GeoRocket query part.</p> * <p>Heads up: implementors may use the helper methods from * {@link ElasticsearchQueryHelper} to build the query.</p> * @param queryPart the GeoRocket query part * @return the Elasticsearch query (may be null) * @since 1.1.0 */ default JsonObject compileQuery(QueryPart queryPart) { if (queryPart instanceof StringQueryPart) { return compileQuery(((StringQueryPart)queryPart).getSearchString()); } return null; } }
/** * Optimize a query or an array of queries * @param children a single query or an array of queries * @param parentClause the parent clause in which the children are embedded * @return the optimized query/queries */ private static Object optimizeChildren(Object children, String parentClause) { if (children instanceof JsonArray) { return optimizeChildren((JsonArray)children, parentClause); } else { return optimize((JsonObject)children, parentClause); } }
@Override public void exitKeyvalue(KeyvalueContext ctx) { KeyValueQueryPart kvqp = new KeyValueQueryPart( currentKeyvalue.getString("key"), currentKeyvalue.getString("value"), ComparisonOperator.valueOf(currentKeyvalue.getString("comp"))); JsonObject q = makeQuery(kvqp); if (!combine(q)) { result.push(q); } currentKeyvalue = null; }
/** * <p>Optimize the given Elasticsearch query. For example, remove unnecessary * boolean clauses or merged nested ones.</p> * <p>Attention: This method is destructive. It changes the given object in * place.</p> * @param query the query to optimize * @return the optimized query (not necessarily the same object as * <code>query</code>) */ public static JsonObject optimize(JsonObject query) { return (JsonObject)optimize(query, null); }
/** * 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"); }
@Override public void exitOr(OrContext ctx) { exitLogical(); }
@Override public void enterAnd(AndContext ctx) { enterLogical(Logical.AND); }
/** * Test if a "should" clause with one child is removed */ @Test public void boolOneShould() { expectFixture("optimizeBoolOneShould"); }
/** * 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"); }
@Override public void exitNot(NotContext ctx) { exitLogical(); }
@Override public void enterNot(NotContext ctx) { enterLogical(Logical.NOT); }
/** * 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"); }
@Override public void exitAnd(AndContext ctx) { exitLogical(); }
@Override public void enterOr(OrContext ctx) { enterLogical(Logical.OR); }
/** * 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"); }