在ElasticSearch中使用Java API时如何从JSON DSL构造QueryBuilder? [英] How to construct QueryBuilder from JSON DSL when using Java API in ElasticSearch?

查看:3265
本文介绍了在ElasticSearch中使用Java API时如何从JSON DSL构造QueryBuilder?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Spring Web项目中使用ElasticSearch作为搜索服务,它使用传输客户端与ES进行通信。



我想知道是否存在可以从JSON DSL构建QueryBuilder的方法。例如,将此布尔查询DSL JSON转换为QueryBuilder。

  {
查询:{
bool:{
必须 :{match:{content:quick},
should:{match:{content:lazy}
}
}
}

我需要这种方法,因为我必须从Web前端接收用户的bool字符串输入,并将此bool字符串解析为QueryBuilder。但是它不适合使用 QueryBuilders.boolQuery()。must(matchQB).should(shouldQB).must_not(mustNotQB)。因为我们可能需要几个必须或非必须查询。



如果存在一种方法可以从JSON DSL构建QueryBuilder或存在替代解决方案,这将会更容易。 / p>

PS:我发现了两种可以将DSL字符串包装到QueryBuilder进行ES搜索的方法。
一个是 WrapperQueryBuilder ,请参阅这里的详细信息。 http://javadoc.kyubu.de/elasticsearch/HEAD/org/elasticsearch/ index / query / WrapperQueryBuilder.html
另一个是 QueryBuilders.wrapperQuery (String DSL)。

解决方案

您可以使用 QueryBuilders.wrapperQuery(jsonQueryString); p>

I'm using ElasticSearch as a search service in Spring Web project which using Transport Client to communicate with ES.

I'm wondering if there exists a method which can construct a QueryBuilder from a JSON DSL. for example, convert this bool query DSL JSON to a QueryBuilder.

{
    "query" : {
        "bool" : {
            "must" : { "match" : {"content" : "quick"},
            "should": { "match": {"content" : "lazy"}
        }
    }
}

I need this method because I have to receive user's bool string input from web front-side, and parse this bool string to a QueryBuilder. However it not suit to use QueryBuilders.boolQuery().must(matchQB).should(shouldQB).must_not(mustNotQB). Because we may need several must or non must query.

If there exist a method can construct a QueryBuilder from JSON DSL or there exists alternative solutions, it will much easier.

PS: I have found two method which can wrap a DSL String to a QueryBuilder for ES search. One is WrapperQueryBuilder, see details here. http://javadoc.kyubu.de/elasticsearch/HEAD/org/elasticsearch/index/query/WrapperQueryBuilder.html Another is QueryBuilders.wrapperQuery(String DSL).

解决方案

You can use QueryBuilders.wrapperQuery(jsonQueryString);

这篇关于在ElasticSearch中使用Java API时如何从JSON DSL构造QueryBuilder?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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