Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to construct QueryBuilder from JSON DSL when using Java API in ElasticSearch?

Tags:

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).

like image 300
Armstrongya Avatar asked Sep 16 '14 02:09

Armstrongya


People also ask

For what purpose is query DSL used in Elasticsearch?

Query DSLedit. Elasticsearch provides a full Query DSL (Domain Specific Language) based on JSON to define queries. Think of the Query DSL as an AST (Abstract Syntax Tree) of queries, consisting of two types of clauses: Leaf query clauses.

What is Elasticsearch in Java example?

Elasticsearch is a real-time distributed and open source full-text search and analytics engine. It is used in Single Page Application (SPA) projects. Elasticsearch is an open source developed in Java and used by many big organizations around the world. It is licensed under the Apache license version 2.0.

What is an Elasticsearch query?

Elasticsearch is a distributed, open-source search and analytics engine built on Apache Lucene and developed in Java. It started as a scalable version of the Lucene open-source search framework then added the ability to horizontally scale Lucene indices.


1 Answers

You can use QueryBuilders.wrapperQuery(jsonQueryString);

like image 175
bradvido Avatar answered Oct 02 '22 07:10

bradvido