20 Wrapper Query

20 WrapperQuery #

A WrapperQuery is a type of query that wraps another query and provides additional functionalities or modifications to the wrapped query. It acts as a wrapper around the wrapped query, allowing the modification or enhancement of the behavior of the wrapped query.

A WrapperQuery is often used in scenarios where the original query needs to be extended or altered without modifying the original query itself. By wrapping the original query, we can add extra logic or filters to the query execution process.

Using a WrapperQuery can be useful in various situations. For example, it can be used to apply security filters to the original query’s results, modify the scoring behavior of the query, or introduce custom logic into the query execution pipeline.

To use a WrapperQuery, you need to create a new query object of the WrapperQuery type and specify the wrapped query as a parameter. The WrapperQuery then acts as a proxy for the wrapped query, intercepting and modifying the query execution as needed.

Here is an example of using a WrapperQuery in Elasticsearch:

{
  "query": {
    "wrapper": {
      "query": {
        "term": {
          "field": "value"
        }
      },
      "boost": 2.0
    }
  }
}

In this example, the term query is wrapped inside a WrapperQuery with a boost value of 2.0. This boost value increases the relevance score of the wrapped query’s results, making them more prominent in the search results.

In summary, a WrapperQuery is a powerful tool for extending or modifying the behavior of an existing query without directly modifying the original query itself. It provides flexibility and customization options that can be useful in various Elasticsearch use cases.

Theoretical Basis of Implementation #

  • Wrapper Query Official Documentation

https://www.elastic.co/guide/en/elasticsearch/reference/6.4/query-dsl-wrapper-query.html

This query is more useful in the context of the Java high-level REST client or transport client to also accept queries as json formatted string. In these cases queries can be specified as a json or yaml formatted string or as a query builder (which is a available in the Java high-level REST client).

GET /_search
{
    "query" : {
        "wrapper": {
            "query" : "eyJ0ZXJtIiA6IHsgInVzZXIiIDogIktpbWNoeSIgfX0=" // Base64 encoded string: {"term" : { "user" : "Kimchy" }}
        }
    }
}
  • Convert DSL JSON statement to map

https://blog.csdn.net/qq_41370896/article/details/83658948

String dsl = "";
Map maps = (Map)JSON.parse(dsl);  
maps.get("query");// dsl query string
  • Java Code

https://blog.csdn.net/tcyzhyx/article/details/84566734

https://www.jianshu.com/p/216ca70d9e62

StringBuffer dsl = new StringBuffer();
dsl.append("{\"bool\": {");
dsl.append("      \"must\": [");
dsl.append("        {");
dsl.append("          \"term\": {");
dsl.append("            \"mdid.keyword\": {");
dsl.append("              \"value\": \"2fa9d41e1af460e0d47ce36ca8a98737\"");
dsl.append("            }");
dsl.append("          }");
dsl.append("        }");
dsl.append("      ]");
dsl.append("    }");
dsl.append("}");
WrapperQueryBuilder wqb = QueryBuilders.wrapperQuery(dsl.toString());
SearchResponse searchResponse = client.prepareSearch(basicsysCodeManager.getYjzxYjxxIndex())
.setTypes(basicsysCodeManager.getYjzxYjxxType()).setQuery(wqb).setSize(10).get();
SearchHit[] hits = searchResponse.getHits().getHits();
for(SearchHit hit : hits){
	String content = hit.getSourceAsString();
	System.out.println(content);
}
  • How to write query + agg

http://www.itkeyword.com/doc/1009692843717298639/wrapperquerybuilder-aggs-query-throwing-query-malformed-exception

"{\"query\":{\"match_all\": {}},\"aggs\":{\"avg1\":{\"avg\":{\"field\":\"age\"}}}}"
SearchSourceBuilder ssb = new SearchSourceBuilder();

// add the query part
String query ="{\"match_all\": {}}";
WrapperQueryBuilder wrapQB = new WrapperQueryBuilder(query);
ssb.query(wrapQB);

// add the aggregation part
AvgBuilder avgAgg = AggregationBuilders.avg("avg1").field("age");
ssb.aggregation(avgAgg);

Implementation Examples #

TBD