2015-11-06 122 views
0

我有以下映射:过滤深嵌套项目

PUT /test 
{ 
    "mappings": { 
    "test": { 
     "properties": { 
     "parent": { 
      "type": "nested", 
      "properties": { 
      "@id": { 
       "type": "string", 
       "index": "not_analyzed" 
      }, 
      "@type": { 
       "type": "string" 
      }, 
      "child": { 
       "type": "nested", 
       "properties": { 
       "@id": { 
        "type": "string", 
        "index": "not_analyzed" 
       }, 
       "subchild": { 
        "type": "nested", 
        "properties": { 
        "@id": { 
         "type": "string", 
         "index": "not_analyzed" 
        }, 
        "hasA": { 
         "type": "nested", 
         "properties": { 
         "@value": { 
          "type": "string" 
         } 
         } 
        }, 
        "hasB": { 
         "type": "nested", 
         "properties": { 
         "@id": { 
          "type": "string", 
          "index": "not_analyzed" 
         } 
         } 
        }, 
        "hasC": { 
         "type": "nested", 
         "properties": { 
         "@id": { 
          "type": "string", 
          "index": "not_analyzed" 
         } 
         } 
        } 
        } 
       } 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
} 

而且下列文件:

POST /test/test/1 
{ 
    "parent": { 
    "@id": "12345", 
    "@type": "test", 
    "child": [ 
     { 
     "@id": "1", 
     "subchild": [ 
      { 
      "@id": "1.1", 
      "hasA": { 
       "@value": "hasA value" 
      }, 
      "hasB": { 
       "@id": "hasB_1" 
      }, 
      "hasC": { 
       "@id": "hasC_1" 
      } 
      } 
     ] 
     }, 
     { 
     "@id": "2", 
     "subchild": [ 
      { 
      "@id": "2.1", 
      "hasA": { 
       "@value": "hasA value" 
      }, 
      "hasB": { 
       "@id": "hasB_2" 
      }, 
      "hasC": { 
       "@id": "hasC_2" 
      } 
      } 
     ] 
     } 
    ] 
    } 
} 

而下面的查询:

POST test/test/_search 
{ 
    "query": { 
    "filtered": { 
     "query": { 
     "match_all": {} 
     }, 
     "filter": { 
     "nested": { 
      "path": "parent.child.subchild.hasB", 
      "filter": { 
      "bool": { 
       "must": [ 
       { 
        "term": { 
        "[email protected]": "hasB_2" 
        } 
       } 
       ] 
      } 
      }, 
      "_cache": false 
     } 
     } 
    } 
    } 
} 

我无法将路径设置为parent.child.subchild,以便我可以在hasB和hasC上匹配,似乎我只能在at处选择一个嵌套项我。这是我想什么能够做到:

POST test/test/_search 
{ 
    "query": { 
    "filtered": { 
     "query": { 
     "match_all": {} 
     }, 
     "filter": { 
     "nested": { 
      "path": "parent.child.subchild", 
      "filter": { 
      "bool": { 
       "must": [ 
       { 
        "term": { 
        "[email protected]": "hasB_2" 
        } 
       }, 
       { 
        "term": { 
        "[email protected]": "hasC_2" 
        } 
       } 
       ] 
      } 
      }, 
      "_cache": false 
     } 
     } 
    } 
    } 
} 
+0

@AndreiStefan我发现你重新格式化我的JSON,我可以在将来使用哪个工具来确保我的JSON格式正确? –

回答

1

你在找这样的事吗?

{ 
    "query": { 
    "filtered": { 
     "query": { 
     "match_all": {} 
     }, 
     "filter": { 
     "nested": { 
      "path": "parent.child.subchild", 
      "filter": { 
      "bool": { 
       "must": [ 
       { 
        "nested": { 
        "path": "parent.child.subchild.hasB", 
        "query": { 
         "term": { 
         "[email protected]": "hasB_2" 
         } 
        } 
        } 
       }, 
       { 
        "nested": { 
        "path": "parent.child.subchild.hasC", 
        "query": { 
         "term": { 
         "[email protected]": "hasC_2" 
         } 
        } 
        } 
       } 
       ] 
      } 
      }, 
      "_cache": false 
     } 
     } 
    } 
    } 
} 
+0

一如既往的完美! :-) –

0

查询正确语法多级嵌套的文档都可以找到here。看看里面的@martijnvg评论。

ES Docs在解释多级嵌套查询方面做得并不好。 基本上你需要在子内嵌入subchild,并指定个人path。您将需要三个嵌套查询。

P.S - 我自己没有测试过。请让我知道,如果它不起作用。

+0

谢谢,我试过了,但没有奏效。我能够查询嵌套项目的唯一方法是不使最后的项目嵌套(subchild),而是将其定义为一个对象。 –