简体   繁体   English

通过Java API进行Elasticsearch查询

[英]Elasticsearch query through Java API

I am using the following elasticsearch query to fetch the details, 我正在使用以下elasticsearch查询来获取详细信息,

{
    "query": {
        "bool": {
            "must": {
                "match_all": {}
            },
            "filter": {
                "bool": {
                    "should": [
                    {"match": {
                        "val": "GET"
                    }}]
                }
            }
        }
    }
}

It is working fine and given the result as required. 它工作正常,并按要求给出了结果。

I want to execute the same query through java and get the same results and tried the following, 我想通过Java执行相同的查询并获得相同的结果,并尝试了以下方法,

 getClient().prepareSearch(esIndex)
                .setQuery(QueryBuilders.queryStringQuery(QUERY)).execute().actionGet();

It is not returning the data and throw some query format wrong exception as well. 它不返回数据,也不抛出某些查询格式错误的异常。

Is there any Java API available using which the same query can be executed? 是否有可用的Java API可以用来执行相同的查询?

NOTE : There is a possibility available to create the boolquery and aggregation builders in java api and execute the same. 注意 :可以在java api中创建boolquery和aggregation构建器并执行它们。 I am just curious to find a way to execute this query directly through elasticsearch java api 我只是很好奇地找到一种直接通过elasticsearch java api执行此查询的方法

If you really want to use the Query String Query, your query has to follow Query String Syntax : 如果您确实要使用查询字符串查询,则您的查询必须遵循“ 查询字符串语法”

getClient().prepareSearch(esIndex)
           .setQuery(QueryBuilders.queryStringQuery("val: \"GET\""))
           .execute()
           .actionGet();

As already stated, you should construct your query by using the provided QueryBuilders instead of strings. 如前所述,您应该使用提供的QueryBuilders而不是字符串来构造查询。 This will keep your code clean and readable even for complex queries. 即使复杂的查询,这也可以使您的代码保持干净和可读性。

getClient().prepareSearch(esIndex)
           .setQuery(QueryBuilders.boolQuery()
                                  .should(QueryBuilders.matchQuery("val", "GET"))
           .execute()
           .actionGet();
BoolQueryBuilder bool       =   boolQuery();

bool.must(QueryBuilders.matchAllQuery()); 
    bool.filter(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("Val", "GET")));

    AggregationBuilder agg = AggregationBuilders.terms("").field("");

    SearchResponse reponse =    getClient().prepareSearch().setIndices("indexName").setTypes("indexType")
                .setQuery(bool).addAggregation(agg).execute().actionGet();

you should use boolQuery() when you construct your QueryBuilder: 在构造QueryBuilder时应使用boolQuery():

QueryBuilder qb = boolQuery()
            .must(termQuery("content", "test1"))
            .must(termQuery("content", "test4"))
            .mustNot(termQuery("content", "test2"))
            .should(termQuery("content", "test3"))
            .filter(termQuery("content", "test5"));

Official docs: https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-compound-queries.html 官方文档: https : //www.elastic.co/guide/zh-CN/elasticsearch/client/java-api/current/java-compound-queries.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM