2015-10-20 165 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文檔,我認爲它提供了您需要的所有詳細信息以及類似於您的案例的相關評論示例。

編輯:

對不起,我只注意到你的問題有關的映射 - 您的第一個映射是正確的。