0

我能够在弹性搜索中查询索引。而且,现在我想将数据缩小到某些特定字段。但是,我不断收到错误。

这是我的查询:

es = Elasticsearch(hosts="myhost", "port":0000)


search_body={
    "bool":{
            "filter":[
                {"exists": {"field": "customer_name"}},
                {"match_phrase": {"city": "chicago"}},
                ]
        }

    }

results = es.search(index="some_index", query=search_body)

到目前为止,我很容易得到结果。但是,由于返回的字段太多,我想在将其转换为数据框之前只检索特定字段。我可以将其转换为数据框,然后进行过滤,但这不是最佳的。


我尝试添加_sourcefield方法:

search_body={
    "bool":{
            "filter":[
                {"exists": {"field": "customer_name"}},
                {"match_phrase": {"city": "chicago"}},
                ]
        },
    "_source":{"fields": {"includes":["customer_name", "city", "company", "company_address"] }}
    }

和其他变体,例如,

"fields": {"includes":["customer_name", "city", "company", "company_address"] }

# or 

"_source":{"includes":["customer_name", "city", "company", "company_address"] }

# and several others.

我不断收到错误:

    raise HTTP_EXCEPTIONS.get(status_code, TransportError)(
elasticsearch.exceptions.RequestError: RequestError(400, 'parsing_exception', '[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]')

我跟着:

我在这里想念什么?

4

3 回答 3

0

从您提供的 JSON 看来,您的_source字段正在进入您的bool查询。相反,您应该将其结构如下:

search_body = {
  "query": {
    "bool": [
      {...}
    ]
   },
   "_source": {
     "includes": [...]
   }
}

(免责声明:我是 Python Elasticsearch 客户端的维护者并为 Elastic 工作)

于 2022-01-11T18:09:41.477 回答
0

尝试这个:

results = es.search(index="some_index", query=search_body, source_includes=[...])

代码是最好的文档(有时!)

于 2022-01-11T20:26:10.190 回答
-1

主要问题是将“search_body”参数作为bodyor传递query

如果我的“search_body”如下所示,我无法传递它,query因为查询是我在索引上请求的特定“查询”。请求_source此查询会使请求不正确。

search_body={
    "bool":{
            "filter":[
                {"exists": {"field": "customer_name"}},
                {"match_phrase": {"city": "chicago"}},
                ]
        },
    "_source":{"fields": {"includes":["customer_name", "city", "company", "company_address"] }}
    }

这将通过,因为请求实际上是作为正文传递的,其中包含“查询”和另一个“_source”字段以对数据进行子集化。

es = Elasticsearch(hosts="myhost", "port":0000)

results = es.search(index="some_index", body=search_body)

这将失败,因为我已请求将搜索作为查询并再次要求对数据进行子集化。

es = Elasticsearch(hosts="myhost", "port":0000)

results = es.search(index="some_index", query=search_body)

如果我们search_body的情况如下,则第二个请求将通过:

search_body={
    "bool":{
            "filter":[
                {"exists": {"field": "customer_name"}},
                {"match_phrase": {"city": "chicago"}},
                ]
        }
    }

但对于命名约定,密钥应命名为“query_body”。

query_body={
    "bool":{
            "filter":[
                {"exists": {"field": "customer_name"}},
                {"match_phrase": {"city": "chicago"}},
                ]
        }
    }

并要求:

es = Elasticsearch(hosts="myhost", "port":0000)

results = es.search(index="some_index", query=query_body)

因此,应该理解querybody是请求索引数据的两种不同方式。

注意:Python elasticsearch 客户端可能很快就会弃用body其请求中的参数。在这种情况下,让我们看看如何对过滤/查询的数据进行子集化。

希望它可以帮助别人。

于 2022-01-19T03:18:50.880 回答