Elasticsearch:如何防止我的客户端自动插入顶级查询属性 [英] Elasticsearch: how to prevent my client from automatically inserting the top-level query property

查看:77
本文介绍了Elasticsearch:如何防止我的客户端自动插入顶级查询属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要生成一个具有以下结构的查询字符串:

  {
query:{
filtered:{
query:{
match_all:{}
},
filter:{
....
}
}
},
聚合:{
facets:{
terms:{
:subject
}
}
}
}

我正在使用以下客户端进行搜索:

 客户端客户端=新的TransportClient(my_settings_object)
.addTransportAddress (新的InetSocketTransportAddress(localhost,9200));
SearchResponse sr = client.prepareSearch(my_index)
.setTypes(my_doctype)
.setQuery(query); //这是上面手动构建的查询字符串
.execute()。actionGet();
return sr;

我收到以下错误:

  QueryParsingException [[my_idex]没有注册[查询]的查询] 

似乎这个错误在这个SO文章中解释:



elasticsearch - 没有查询[query]]



基本上,我的客户端自动将顶级查询属性插入到使它看起来像:

  {
查询:{
查询:{
过滤:{
查询:{
match_all:{}
},
过滤器:{
....

}
},
聚合:{
facets:{
terms:{
field:subject
}
}
}
}
}



/ p>

解决方案

使您的查询和聚合查询单个JSON对象。将查询更改为:

  {
已过滤:{
查询:{
match_all:{

}
},
过滤器:{
....
}
}
}

并更改聚合,以便它们位于单个JSON对象中:

  {
facets:{
terms:{
field:subject
}
}
}

设置查询和聚合SearchRequestBuilder:

  SearchResponse sr = client.prepareSearch(my_index)
.setTypes(my_doctype)
.setQuery(query); //这是上面手动构建的查询字符串
.setAggregations(aggregations.getBytes());
.execute()。actionGet();


I need to generate a query string with the following structure:

{
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
       ....
      }
    }
  },
  "aggregations": {
    "facets": {
      "terms": {
        "field": "subject"
      }
    }
  }
}

I am using the following client for search:

Client client = new TransportClient(my_settings_object)
        .addTransportAddress(new InetSocketTransportAddress("localhost", "9200"));
SearchResponse sr = client.prepareSearch("my_index")
        .setTypes("my_doctype")
        .setQuery(query) ; // this is above query string built manually
        .execute().actionGet();         
return sr;

I got the following error:

QueryParsingException[[my_idex] No query registered for [query]]

It seems that this error is explained in this SO post:

elasticsearch - No query registered for [query]]

Basically, my client automatically inserts the top-level query property to make it look like:

{
    query:  {
      "query": {
        "filtered": {
          "query": {
            "match_all": {}
          },
          "filter": {
           ....
          }
        }
      },
      "aggregations": {
        "facets": {
          "terms": {
            "field": "subject"
          }
        }
      }
    }
}

How can I prevent my client from automatically inserting the top-level query property?

Thanks and regards.

解决方案

Make your query and aggregations query individaul JSON objects. Change the query to:

{
    "filtered": {
        "query": {
            "match_all": {

            }
        },
        "filter": {
            ....
        }
    }
}

And change the aggregations so that they are in individual JSON object:

    {
        "facets": {
            "terms": {
                "field": "subject"
            }
        }
    }

Set the query and aggregations in the SearchRequestBuilder:

SearchResponse sr = client.prepareSearch("my_index")
    .setTypes("my_doctype")
    .setQuery(query) ; // this is above query string built manually
    .setAggregations(aggregations.getBytes());
    .execute().actionGet(); 

这篇关于Elasticsearch:如何防止我的客户端自动插入顶级查询属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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