2017-10-06 48 views
0

应该如何此表达式可以写成的查询:如何使用组合的AND和OR逻辑查询嵌套对象?

(attributes.id = 14 OR attributes.id = 15)和(attributes.id = 4843 OR attributes.id = 4859)

的嵌套的对象是这样的:

{ 
    "attributes":[ 
    { 
     "id":14, 
     "type":"color", 
     "name":"Sort", 
     "version":1 
    }, 
    { 
     "id":15, 
     "type":"color", 
     "name":"Sølv", 
     "version":1 
    }, 
    { 
     "id":2031, 
     "type":"brand", 
     "name":"Jimmy Choo", 
     "version":1 
    }, 
    { 
     "id":4843, 
     "type":"size", 
     "name":"36x28", 
     "version":1 
    }, 
    { 
     "id":4859, 
     "type":"size", 
     "name":"38x36", 
     "version":1 
    }, 
    { 
     "id":4927, 
     "type":"size", 
     "name":"60J", 
     "version":1 
    }, 
    { 
     "id":4958, 
     "type":"size", 
     "name":"75F", 
     "version":1 
    } 
    ] 
} 

我已经使用这个查询尝试 - 其中许多 - 没有任何的运气:

{ 
    "query":{ 
    "nested":{ 
     "path":"attributes", 
     "query":{ 
     "bool":{ 
      "should":[ 
      { 
       "terms":{ 
       "attributes.id":[ 
        14, 
        15 
       ] 
       } 
      }, 
      { 
       "terms":{ 
       "attributes.id":[ 
        4843, 
        4859 
       ] 
       } 
      } 
      ], 
      "minimum_should_match":2 
     } 
     } 
    } 
    } 
} 

以上查询返回零结果。

任何帮助,将不胜感激。

回答

0

我得到了答案上Elastic Stack by Daniel_Penning

{ 
    "query": { 
    "bool": { 
     "must": [ 
     { 
      "nested": { 
      "path": "variants.attributes", 
      "query": { 
       "terms": { 
       "variants.attributes.id": [ 
        14, 15 
       ] 
       } 
      } 
      } 
     }, 
     { 
      "nested": { 
      "path": "variants.attributes", 
      "query": { 
       "terms": { 
       "variants.attributes.id": [ 
        4843, 4859 
       ] 
       } 
      } 
      } 
     } 
     ] 
    } 
    } 
} 
1
{ 
    "query":{ 
    "nested":{ 
    "path":"attributes", 
    "query":{ 
    "bool":{ 
     "must":[{ 
     "bool": { 
      "should": [ 
       { 
        "term": { 
         "attributes.id": 14 
        } 
       },{ 
        "term": { 
         "attributes.id": 15 
        } 
       } 
      ] 
     } 
     },{ 
     "bool": { 
      "should": [ 
       { 
        "term": { 
         "attributes.id": 4843 
        } 
       },{ 
        "term": { 
         "attributes.id": 4859 
        } 
       } 
      ] 
     } 
     } 
     ] 
    } 
} 

}

这应该工作。

+0

谢谢你的答案,但遗憾的是,这是行不通的。没有匹配返回。 我使用Elasticsearch 5.6.2,如果它有任何区别。 –