我使用Elasticsearch 2.4,我試圖讓一個行爲像下面的SQL語句的查詢:如何在過濾查詢使用或與Elasticsearch 2.X
SELECT * FROM countries WHERE continent='Europe' and (country='Andorra' OR cities in ['Madrid'])
在Elasticsearch 1.5我得到它的工作使用以下查詢:
{
"query": {
"filtered": {
"filter": {
"term": {
"continent": "Europe"
}
},
"query": {
"bool": {
"should": [
{
"nested": {
"path": "cities",
"query": {
"match": {
"cities.name": "Madrid"
}
}
}
},
{
"match": {
"country": "Andorra"
}
}
]
}
}
}
}
}
但是好像在2.x版的「過濾」 has been deprecated帕拉姆。我試圖使用新的方法使用過濾器來構建查詢,但是它沒有正確找到嵌套值。這是結果查詢:
{
"query": {
"bool": {
"filter": [
{
"term": {
"continent": "Europe"
}
}
],
"should": [
{
"nested": {
"path": "cities",
"query": {
"match": {
"cities.name": "Madrid"
}
}
}
},
{
"match": {
"country": "Andorra"
}
}
]
}
}
}
這是我試圖找回數據:
{
"_index":"countries",
"_type":"item",
"_id":"123",
"_version":1,
"found":true,
"_source":{
"country": "Spain",
"cities": [
{
"id_city": 2133,
"name": "Madrid"
},
{
"id_city": 8382,
"name": "Barcelona"
}
]
}
}
是否有人知道正確的方式來實現這一目標?
不,它沒有。但無論如何,使用「必須」它不會有**或**的行爲。我需要它匹配,如果它是某個國家或它是某個城市。 – Ander
你可以嘗試用一個應該替換的「必須」嗎?更新了答案。 – jay