弹性搜索和查询

时间:2014-10-29 21:22:10

标签: python elasticsearch

我试图做这样的事情:

SELECT * FROM x WHERE __account_id = 5 AND __import_id IN [1,2,3]

这是我的ES查询:

body = {
    "query": {
        "filtered" : { 
            "filter" : {
                "term" : { 
                    "__account_id" : account.id,
                    "__import_id": [x['id'] for x in ingested_imports]
                }
            }
        }
    }
}

结果似乎仅在__account_id上过滤,但在__import_id上没有过滤。我做错了什么?

1 个答案:

答案 0 :(得分:1)

您需要在term查询中包含termsbool过滤器,因为这两个子句必须匹配。

{
  "query":{
    "filtered":{
      "filter":{
        "bool":{
          "must":[
            {
              "term":{
                "__account_id":5
              }
            },
            {
              "terms":{
                "__import_id":[1, 2, 3]
              }
            }
          ]
        }
      }
    }
  }
}