Elasticsearch Jest客户端向JSON查询添加条件 [英] Elasticsearch Jest client add condition to json query

查看:105
本文介绍了Elasticsearch Jest客户端向JSON查询添加条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Elasticsearch 6.3与Jest客户端6.3(Java API)一起使用

I am using Elasticsearch 6.3 with Jest client 6.3 (Java API)

Search search = new Search.Builder(jsonQueryString)
                    .addIndex("SOME_INDEX")
                    .build();
SearchResult result = jestClient.execute(search);

这是我的示例JSON查询

And this is my sample JSON query

{
"query": {
    "bool" : {
        "filter": {
            "match" :{ 
                "someField" : "some value" 
                }
            }
        }
    }
}

将JSON查询字符串作为POST请求正文接受,然后传递给Jest客户端.在Jest客户端上执行json查询之前,我需要为查询添加条件,例如

The JSON query string is accepted as a POST request body and then passed to the Jest client. Before I can execute the json query on the Jest client, I need to add conditions to the query for e.g.

{
"query": {
    "bool" : {
        "filter": {
            "match" :{ 
                "someField" : "some value" 
                }
            }
        },
        "must": {
            "match" :{ 
                "systemField" : "pre-defined value" 
                }
            }
        }
    }
}

是否有一个API,可以解析JSON查询并为其添加条件,然后才能在Jest客户端上执行它?JSON查询可以是Query DSL支持的任何查询,并且不一定包含bool条件.我需要向查询添加预定义条件.我对此表示感谢.非常感谢.

Is there an API that allows to parse the JSON query and add conditions to it before it can be executed on Jest client? The JSON query can be any query supported by Query DSL and not necessarily contain bool condition. I need to add a pre-defined condition to the query. I appreciate any help on this. Thanks very much.

推荐答案

没有开箱即用的Elasticsearch或Jest API可以实现上述目标,我实现的解决方法是使用Jackson ObjectMapper

There is no out of the box Elasticsearch or Jest API to achieve the above, the workaround I implemented is using Jackson ObjectMapper

// convert the search request body into object node
ObjectNode searchRequestNode = objectMapper.readValue(queryString, ObjectNode.class);
// extract the query
String query = searchRequestNode.get("query").toString();

// wrap the original query and add conditions
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.wrapperQuery(query));
boolQueryBuilder.filter(QueryBuilders.termsQuery("fieldA", listOfValues));
boolQueryBuilder.filter(QueryBuilders.termQuery("fieldB", value));

// convert querybuilder to json query string
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(queryBuilder);
String queryWithFilters = searchSourceBuilder.toString();

// convert json string to object node
ObjectNode queryNode = objectMapper.readValue(queryWithFilters, ObjectNode.class);

// replace original query with the new query containing added conditions 
searchRequestNode.set("query", queryNode.get("query"));
String finalSearchRequestWithOwnFilters = searchRequestNode.toString();

这篇关于Elasticsearch Jest客户端向JSON查询添加条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆