我的資料是這樣的:
[
{
"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物件
uj5u.com熱心網友回復:
就像是:
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)]
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/375324.html
標籤:Python 弹性搜索 elasticsearch-dsl 弹性搜索-dsl-py
