2015-10-20 103 views
0

部分我的映射的一对属性的查询是:Elasticsearch。对于在嵌套对象

"individual_attributes" : { 
    "type" : "nested", 
     "properties" : { 
      "template_id" : {"type" : "integer"}, 
      "attributes_set" : { 
       "type" : "nested", 
       "properties" : { 
        "attribute_id" : {"type" : "integer"}, 
        "attribute_value" : {"type" : "string", "index" : "not_analyzed"} 
       } 
      } 
     } 
    } 

我需要筛选那些已与ATTRIBUTE_VALUE attribute_id =“X”为给定的ID等于“Y”的文件。所以我需要匹配一对字段。可能吗?或者,我需要改变我的映射是这样的:

"individual_attributes" : { 
    "type" : "nested", 
     "properties" : { 
      "template_id" : {"type" : "integer"}, 
      "attributes_set" : { 
       "type" : "nested", 
       "properties" : { 
        "attribute_id" : {"type" : "integer", 
         "properties" : { 
          "attribute_value" : {"type" : "string", "index" : "not_analyzed"} 
         } 
        }, 

       } 
      } 
     } 
    } 

的样本数据:

    "attributes_set": [ 
       { 
        "attribute_id": 17, 
        "attribute_value": "dolorum" 
       }, 
       { 
        "attribute_id": 15, 
        "attribute_value": "at" 
       }, 
       { 
        "attribute_id": 18, 
        "attribute_value": "maxime" 
       }, 
       { 
        "attribute_id": 14, 
        "attribute_value": "et" 
       }, 
       { 
        "attribute_id": 11, 
        "attribute_value": "nemo" 
       }, 
       { 
        "attribute_id": 12, 
        "attribute_value": "rem" 
       }, 
       { 
        "attribute_id": 10, 
        "attribute_value": "eius" 
       }, 
       { 
        "attribute_id": 19, 
        "attribute_value": "deleniti" 
       }, 
       { 
        "attribute_id": 13, 
        "attribute_value": "modi" 
       }, 
       { 
        "attribute_id": 16, 
        "attribute_value": "neque" 
       } 
       ] 

我需要:SELECT * WHERE属性(16,例如)=值(预期值,也)。换句话说,我们需要一个数据集内对匹配字段的:

{ 
    "attribute_id": x, 
    "attribute_value": "y" 
} 

回答

2

这里是一个简化的例子。你的第一个映射应该适合你想要做的事情。为了简化解释,我拿出了一层嵌套来简化解释,但是同样的原理可以与任意层次的嵌套一起工作(如果你没有看到如何推广我的例子,我可以用另一个例子编辑答案)。

我成立了一个简单的映射是这样的:

PUT /test_index 
{ 
    "mappings": { 
     "doc": { 
     "properties": { 
      "attributes_set": { 
       "type": "nested", 
       "properties": { 
        "attribute_id": { 
        "type": "integer" 
        }, 
        "attribute_value": { 
        "type": "string", 
        "index": "not_analyzed" 
        } 
       } 
      } 
     } 
     } 
    } 
} 

然后添加两个文件有两个嵌套每个文件:

POST /test_index/doc/_bulk 
{"index":{"_id":1}} 
{"attributes_set": [{"attribute_id": 18,"attribute_value": "dolorum"},{"attribute_id": 15,"attribute_value": "at"}]} 
{"index":{"_id":2}} 
{"attributes_set": [{"attribute_id": 18,"attribute_value": "maxime"},{"attribute_id": 14,"attribute_value": "et"}]} 

现在我可以查询的文档具有特定嵌套doc如下:

POST /test_index/_search 
{ 
    "filter": { 
     "nested": { 
     "path": "attributes_set", 
     "filter": { 
      "bool": { 
       "must": [ 
        { 
        "term": { 
         "attributes_set.attribute_id": { 
          "value": 18 
         } 
        } 
        }, 
        { 
        "term": { 
         "attributes_set.attribute_value": { 
          "value": "maxime" 
         } 
        } 
        } 
       ] 
      } 
     } 
     } 
    } 
} 

其中r eturns:

{ 
    "took": 24, 
    "timed_out": false, 
    "_shards": { 
     "total": 5, 
     "successful": 5, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 1, 
     "max_score": 1, 
     "hits": [ 
     { 
      "_index": "test_index", 
      "_type": "doc", 
      "_id": "2", 
      "_score": 1, 
      "_source": { 
       "attributes_set": [ 
        { 
        "attribute_id": 18, 
        "attribute_value": "maxime" 
        }, 
        { 
        "attribute_id": 14, 
        "attribute_value": "et" 
        } 
       ] 
      } 
     } 
     ] 
    } 
} 

这里就是我用来测试它的代码:

http://sense.qbox.io/gist/5e75461a4f0cf96e012cbf0f8262b22f3f8e5ec0

这是否帮助?

+0

非常感谢您的时间和耐心,而您一直在写答案。非常直,细节。这正是我需要的。我更改了使用更多嵌套层次的代码。有,我不知道http://sense.qbox.io。有用的工具。谢谢你指出。现在是将聚合添加到我的过滤器的时候了。 ) –

+0

以及如何添加更多属性集?试过这样,但它没有工作: '“bool”:[“必须”:[{“term”:{...}},...]}, {“must”:[ {“term”:{...}},...]} ] ' –

+0

您必须将整个'nested'子句放在一个'bool-must'中,再加上另一个与您要查找的第二个嵌套文档匹配的嵌套子句。请记住,嵌套过滤器说,“找到符合这些标准的嵌套文档”。如果您正在查找多个嵌套文档,则需要多个嵌套过滤器。 –

1

看起来你已经制定了这个完美的映射等等,所有你需要做的是建立涉及nested查询正确的查询。

请参阅Elasticsearch文档,了解如何查询嵌套对象。请参阅他们的Querying a Nested Object文档,我认为它提供了您需要的所有详细信息以及类似于您的案例的相关评论示例。

编辑:

对不起,我只注意到你的问题有关的映射 - 您的第一个映射是正确的。