2016-09-21 67 views
0

我使用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" 
        } 
        ] 
    } 
} 

是否有人知道正确的方式来实现这一目标?

回答

0

见,如果这个工程:

{ 
     "query" : { 
      "bool" : { 
       "should" : [{ 
         "nested" : { 
          "path" : "cities", 
          "query" : { 
           "match" : { 
            "cities.name" : "Madrid" 
           } 
          } 
         } 
        }, { 
         "match" : { 
          "country" : "Spain" 
         } 
        } 
       ], 
       "filter" : 
       [{ 
         "term" : { 
          "continent" : "Europe" 
         } 
        } 
       ] 
      } 
     } 
    } 
+0

不,它没有。但无论如何,使用“必须”它不会有**或**的行为。我需要它匹配,如果它是某个国家或它是某个城市。 – Ander

+0

你可以尝试用一个应该替换的“必须”吗?更新了答案。 – jay

0

其实我得到它的工作是这样的:

{ 
    "query": { 
    "bool": { 
     "filter": [ 
     { 
      "term": { 
      "continent": "Europe" 
      } 
     } 
     ], 
     "must": [ 
     { 
      "bool": { 
      "should": [ 
       { 
       "nested": { 
        "path": "cities", 
        "query": { 
        "match": { 
         "cities.name": "Madrid" 
        } 
        } 
       } 
       }, 
       { 
       "match": { 
        "country": "Andorra" 
       } 
       } 
      ] 
      } 
     } 
     ] 
    } 
    } 
} 

我知道..奇怪,但如果查询不匹配任何长期使用主查询中的“应该”而不是“必须”将从Europe返回大量随机结果。

0

如果您的bool查询有filtermust子句,那么should子句不需要任何匹配即可获得结果。如果它是独立的,那么它需要它有1匹配。它的文档在:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html

所以,给这个尝试和力量至少1应该使用"minimum_should_match": 1匹配。

{ 
    "query": { 
    "bool": { 
     "filter": [ 
     { 
      "term": { 
      "continent": "Europe" 
      } 
     } 
     ], 
     "should": [ 
     { 
      "nested": { 
      "path": "cities", 
      "query": { 
       "match": { 
       "cities.name": "Madrid" 
       } 
      } 
      } 
     }, 
     { 
      "match": { 
      "country": "Andorra" 
      } 
     } 
     ], 
     "minimum_should_match": 1 
    } 
    } 
} 
相关问题