简体   繁体   English

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

[英]How do I use “filter” in Elasticsearch query with python?

Here is my code: 这是我的代码:

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'}}}}})

AND MY OUTPUT ERROR: 和我的输出错误:

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

Hi it is a syntax error 嗨,这是语法错误

please check this 请检查一下

 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)

This will work here 这将在这里工作

I think you can try it: because i check your query in "Kibana Dev Tools" and it say you are not allowed use "filter" in "must" partition. 我认为您可以尝试:因为我在“ Kibana Dev Tools”中检查了您的查询,并说您不允许在“必须”分区中使用“过滤器”。

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

Checkout the documentation . 查看文档 Based on this I suspect your query should be: 基于此,我怀疑您的查询应为:

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

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

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