2014-11-03 99 views
6

基本問題如下: 是否有一種方便的方法來指定嵌套查詢的所有字段上的多字段匹配? 對於正常查詢{ match : { _all : "query string" }}的作品。 這可能不會在嵌套查詢中工作,因爲嵌套對象沒有_all?在嵌套對象的所有字段上進行彈性搜索嵌套查詢

更詳細如下問題:

我有一個名爲「父」的嵌套文件如下:

{ 
    "children" : [ 
     { 
      "field_a": "value_a_1", 
      "field_b" : "value_b_1", 
      "field_c" : [ { 
       "field_c_a" : "value_c_a_1", 
       "field_c_b" : "value_c_b_1" 
      } ] 
     }, 
     { 
      "field_a": "value_a_2", 
      "field_b" : "value_b_2", 
      "field_c" : [ { 
       "field_c_a" : "value_c_a_2", 
       "field_c_b" : "value_c_b_2" 
      } ] 
     } 
    ] 
} 

這是我用於製作兒童嵌套對象的映射:

"Parent" : { 
    "properties" : { 
     "children" : { 
      "type" : "nested", 
      "include_in_parent" : true 
       } 
    } 
} 

這裏是一個查詢,我想在所有子字段查詢中使用匹配選擇一些術語,以及一個術語查詢:

"query" : { 
    "nested": { 
     "path" : "children", 
     "query" : { 
      "bool" : { 
       "must" : [ 
         {"multi_match" : {"query": "value_c_a_1", "fields" : ["children.*"]}}, 
         {"term" : {children.field_a : "value_a_1" }} 
        ] 
       } 
      } 
    } 
} 

上述查詢不起作用,因爲我無法在嵌套對象的多匹配查詢中選擇所有字段。

"query" : { 
    "nested": { 
     "path" : "children", 
     "query" : { 
      "bool" : { 
       "must" : [ 
         {"multi_match" : {"query": "value_c_a_1", "fields" : ["*_c_a"]}} 
        ] 
       } 
      } 
    } 
} 

上面的查詢工作,因爲該模式匹配允許在字符串前放置一個*,但由於某種原因後(?)

有選擇的所有字段一個很好的速記方法一個嵌套對象?

也很高興知道爲什麼期望的孩子。*通配符不按預期工作。

回答

0

正常工作給我(注:Elasticsearch 1.7)。注意不同的名字雖然(aparent,somechildren) - 我沒有用原來的名字測試它,但它可能與它有關。

架構:

curl -XPUT localhost:9200/test -d '{ 
    "mappings": { 
    "aparent": { 
     "properties": { 
     "somechildren": { 
      "type": "nested", 
      "include_in_parent": true 
     } 
     } 
    } 
    } 
}' 

文獻:

curl -XPUT localhost:9200/test/aparent/1 -d '{ 
    "somechildren" : [ 
    { 
     "field_a": "value_a_1", 
     "field_b" : "value_b_1", 
     "field_c" : [ { 
     "field_c_a" : "value_c_a_1", 
     "field_c_b" : "value_c_b_1" 
     } ] 
    }, 
    { 
     "field_a": "value_a_2", 
     "field_b" : "value_b_2", 
     "field_c" : [ { 
     "field_c_a" : "value_c_a_2", 
     "field_c_b" : "value_c_b_2" 
     } ] 
    } 
    ] 
}' 

查詢:

GET test/_search 
{ 
    "query": { 
    "nested": { 
     "path": "somechildren", 
     "query": { 
     "bool": { 
      "must": [ 
      { 
       "multi_match": { 
       "query": "value_c_a_1", 
       "fields": [ 
        "somechildren.*" 
       ] 
       } 
      }, 
      { 
       "term": { 
       "somechildren.field_a": { 
        "value": "value_a_1" 
       } 
       } 
      } 
      ] 
     } 
     } 
    } 
    } 
} 

結果:

{ 
    "took": 2, 
    "timed_out": false, 
    "_shards": { 
     "total": 5, 
     "successful": 5, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 1, 
     "max_score": 0.8603305, 
     "hits": [ 
      { 
       "_index": "test", 
       "_type": "aparent", 
       "_id": "1", 
       "_score": 0.8603305, 
       "_source": { 
       "somechildren": [ 
        { 
         "field_a": "value_a_1", 
         "field_b": "value_b_1", 
         "field_c": [ 
          { 
          "field_c_a": "value_c_a_1", 
          "field_c_b": "value_c_b_1" 
          } 
         ] 
        }, 
        { 
         "field_a": "value_a_2", 
         "field_b": "value_b_2", 
         "field_c": [ 
          { 
          "field_c_a": "value_c_a_2", 
          "field_c_b": "value_c_b_2" 
          } 
         ] 
        } 
       ] 
       } 
      } 
     ] 
    } 
    } 
相關問題