【问题标题】:Elasticsearch DSL, filter list of objects with in list of valuesElasticsearch DSL,使用值列表过滤对象列表
【发布时间】:2022-01-08 10:26:09
【问题描述】:

我的数据如下所示:

[
    {
        "id": "00f0bbe514dcaf262c8a",
        "status": "CL",
        "type": "opportunity",
        "locations": [
            {
                "name": "New York, USA",
                "lat": 99.0853,
                "lng": 99.7818,
                "id": "456",
                "type": "CI"
            },
            {
                "name": "Boston, USA",
                "lat": 80.0853,
                "lng": 80.7818,
                "id": "555",
                "type": "CI"
            },
            {
                "name": "London, UK",
                "lat": 10.0853,
                "lng": 10.7818,
                "id": "999",
                "type": "CI"
            }
        ]
    },
    {
        "id": "sadl9asod01",
        "status": "CL",
        "type": "opportunity",
        "locations": [
            {
                "name": "Boston, USA",
                "lat": 80.0853,
                "lng": 80.7818,
                "id": "555",
                "type": "CI"
            },
        ]
    },
    {
        "id": "13094ulk",
        "status": "CL",
        "type": "project",  # has right location but not type
        "locations": [
            {
                "name": "Boston, USA",
                "lat": 80.0853,
                "lng": 80.7818,
                "id": "555",
                "type": "CI"
            },
        ]
    }

]

我想构建一个类型必须是机会的查询:

type_q = ElasticQ('bool', must=[ElasticQ('match', type='opportunity')])
query = self.index.search().query(type_q)

我知道如何使用 dsl 构建“in”查询,例如:

excluded_ids = self._excluded_jobs() # list
query = query.exclude('terms', id=excluded_ids)

但是,我怎样才能在 SQL 中添加我会这样的查询:

WHERE type='opportunity' 
AND 
location.id in (1, 2, 3)
  • location 代表文档的locations数组中的here对象

【问题讨论】:

    标签: python elasticsearch elasticsearch-dsl elasticsearch-dsl-py


    【解决方案1】:

    类似:

    type_q = ElasticQ('bool', must=[
      ElasticQ('match', type='opportunity'),
      ElasticQ('terms', id=excluded_ids),
    ])
    

    或者,如果您真的想排除这些 ID:

    type_q = ElasticQ('bool', 
                      must=[ElasticQ('match', type='opportunity')]
                      must_not=[ElasticQ('terms', id=excluded_ids)]
    )
    

    【讨论】:

      猜你喜欢
      • 2014-08-14
      • 2017-06-14
      • 2019-04-07
      • 1970-01-01
      • 2019-01-24
      • 1970-01-01
      • 2023-02-06
      • 2022-01-12
      相关资源
      最近更新 更多