2016-08-03 92 views
0

我想构建一个查询,其中父项的子项根据数组中的国家代码进行过滤。如果孩子没有国家领域,我仍然想要返回结果。Elasticsearch 2.3 has_child with must_not.exists查询

我有两个工作查询:

{ 
    "query": { 
    "bool": { 
     "should": { 
     "has_child": { 
      "inner_hits": {}, 
      "type": "service", 
      "score_mode": "sum", 
      "query": { 
      "bool": { 
       "filter": [ 
       { 
        "term": { 
        "countries": "AF" 
        } 
       } 
       ] 
      } 
      } 
     } 
     } 
    } 
    } 
} 

其中“AF”是国家的数组中,并正式返回结果的数组:

{ 
    "query": 
    { 
     "bool": { 
      "should": { 
       "has_child": { 
        "inner_hits": {}, 
        "type": "service", 
        "score_mode": "sum", 
        "query": { 
         "bool": { 
          "must_not": { 
           "exists": { 
            "field": "countries" 
           } 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
} 

回报我想那里的孩子有结果没有国家领域。

我弄不清楚的是如何将这两个查询结合起来以获得一组组合结果。也就是说我想'或'这两套。

回答

0

没有实际测试过这个,这是一个盲目的建议:

{ 
    "query": { 
    "bool": { 
     "should": { 
     "has_child": { 
      "inner_hits": {}, 
      "type": "service", 
      "score_mode": "sum", 
      "query": { 
      "bool": { 
       "must": [ 
       { 
        "bool": { 
        "should": [ 
         { 
         "bool": { 
          "must": [ 
          { 
           "exists": { 
           "field": "countries" 
           } 
          }, 
          { 
           "term": { 
           "countries": "AF" 
           } 
          } 
          ] 
         } 
         }, 
         { 
         "missing": { 
          "field": "countries" 
         } 
         } 
        ] 
        } 
       }, 
       { 
        "term": { 
        "whatever": "blabla" 
        } 
       } 
       ] 
      } 
      } 
     } 
     } 
    } 
    } 
} 
+0

这当然似乎工作,但我不明白为什么:(如何查询都被转换为一个或那是哪里?记录下来?现在我想添加另一个与整个查询进行AND运算的术语过滤器,但我不清楚该怎么做 – kpg

+0

'bool'' should'与'OR'类似,如果新的' term'是AND,那么在同一个'bool'中添加'must'并在'must'中加上'term'。新的'term'仍然与'has_child'查询有关? –

+0

是的,新的这个词与has_child有关,我希望它适用于两种情况(没有国家和国家的比赛)。将一个must.term绑定到顶级布尔,但它将返回与“必须”匹配的所有子项,而不管国家/国家/地区。 – kpg