2017-01-27 20 views
0

我想写一个弹性搜索查询,其中我有一个具有完全相同结构的子文档数组,我必须在同一子文档中检查两个条件。弹性搜索Fliter在同一子文件内的两个字段

我的数据是这样的:

{ 
    "_id": "5672ba7c0d71c93d159c408e", 
    "productId": "1723913526", 
    "title": "Digitab Tablet", 
    "instock": true, 
    "specifications": [{ 
     "category": "In the box", 
      "name": "Test1", 
      "value": "No" 
     }, { 
      "category": "Warranty", 
      "name": "Test2", 
      "value": "Yes" 
     }, { 
      "category": "General", 
      "name": "Test3", 
      "value": "Abcd" 
     }], 
    } 
} 

我试图是:我应该得到的产品,其中specifications.name是“测试1”和相同的子文件specifications.value为“是”

下面是我对这个书面查询:

{ 
    "query": { 
    "bool": { 
     "must": [ 
     { 
      "wildcard": { 
      "specifications.name": "Test1*" 
      } 
     }, 
     { 
      "term": { 
      "instock": "true" 
      } 
     }, 
     { 
      "term": { 
      "specifications.value": "yes" 
      } 
     } 
     ], 
     "must_not": [], 
     "should": [] 
    } 
    } 
} 

问题是 - 它不应该返回亲因为“value”:“Yes”对于“name”:“Test2”而不是“name”:“Test1”,所以id为1723913526。

我希望问题清楚,如果需要更多信息,请发表评论。

在此先感谢。

回答

1

您需要修改您的映射以指示specificationsnested类型。

映射,如:

"mappings": { 
    "product": { 
    "properties": { 
     ... 
     "specifications": { 
     "type": "nested" 
     } 
    } 
    } 
} 

,然后查询,如:

"bool": { 
    "must": [{ 
    "nested": { 
     "path": "specifications", 
     "query": { 
     "bool": { 
      "must": [ 
      { "wildcard": { "specifications.first": "Test1*" }}, 
      { "term": { "specifications.value": "yes" }} 
      ] 
     } 
     } 
    }, 
    { 
     "term": { "instock": "true" } 
    } 
    }] 
} 
+0

谢谢,我会试试看,并更新你的。 –