我如何在python的Elasticsearch查询中使用“过滤器”?

时间:2018-11-06 06:19:10

标签: python elasticsearch

这是我的代码:

import requests
from elasticsearch import Elasticsearch
res = requests.get('http://localhost:9200')
print(res.content)

es = Elasticsearch([{'host': 'localhost', 'port': 9200}])

es.search(index="sw", body={"query":{"bool":{"must":{"match":{'skin_color':'fair'}"filter":{'height':'170'}}}}})

和我的输出错误:

  

文件“”,第1行       es.search(index =“ sw”,body = {“ query”:{“ bool”:{“必须”:{“ match”:{'skin_color':'fair'}“ filter”:{'height': '170'}}}}})                                                                                                ^ SyntaxError:语法无效

3 个答案:

答案 0 :(得分:0)

嗨,这是语法错误

请检查此

 from elasticsearch import Elasticsearch
es = Elasticsearch([{'host': 'your_host', 'port': 9200}])

q = {"filter": 
        {"term": 
            {"job desc": "Data Analyst"}
        },
     "_source": {
            "include": ['job place']
        }
    }
# Assume you want "Data Analyst" in the "job desc" field

result = es.search(index='my_index', doc_type='job_list', body=q)

这将在这里工作

答案 1 :(得分:0)

我认为您可以尝试: 因为我在“ Kibana开发工具”中检查了您的查询,并说不允许在“必须”分区中使用“过滤器”。

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "skin_color": "fair"
          }
        }
      ],
      "filter": {'height':'170}
    }
  }
}

答案 2 :(得分:0)

检出documentation。基于此,我怀疑您的查询应该是:

{
    "query": {
      "bool": {
         "must": {
            "match": {
                "skin_color": "fair"
            }
          },
          "filter": {
               "match": {
                   "height": "170"
               }
          }
      }
   }
}