2017-01-13 205 views
0

我有這樣的結果刪除項目

{ 
    "_index": "products-1479727033119", 
    "_type": "product", 
    "_id": "123", 
    "_score": 5.2519913, 
    "_source": { 
     "name": "Samsung S7", 
     "rating": 4.5123123, 
     "id": "609126", 
     "stores": [ 
     { 
      "id" :123214, 
      "name":"Walmart" 
     }, 
     { 
      "id" :2141251, 
      "name":"ebay" 
     } 
     ], 
    } 
    } 

在某些情況下,我需要我的結果是一樣的,但是我需要過濾數組項

而結果應該是這樣的:

{ 
    "_index": "products-1479727033119", 
    "_type": "product", 
    "_id": "123", 
    "_score": 5.2519913, 
    "_source": { 
     "name": "Samsung S7", 
     "rating": 4.5123123, 
     "id": "609126", 
     "stores": [ 
     { 
      "id" :123214, 
      "name":"Walmart" 
     } 
     ], 
    } 
    } 

這是可能在一個單一的查詢?

回答

0

是的,您必須查看elasticsearch中的嵌套對象。您必須在嵌套對象的模式中定義映射。一旦您將模式設置爲支持源文檔中的嵌套對象,就可以使用嵌套過濾器innerhits。

對文檔使用以下模式映射。現在

-XPut "localhost:9200/products/product/_mapping" 
    { 
     "product": { 
      "_source": { 
       "enabled": true 
      }, 
      "properties": { 
       "stores": { 
        "type": "nested", 
        "properties": { 
         "id": { 
          "type": "string", 
          "index": "analyzed" 
         }, 
         "name": { 
          "type": "string", 
          "index": "analyzed" 
         } 
        } 
       }, 
       "name": { 
        "type": "string", 
        "index": "analyzed" 
       }, 

       "rating": { 
        "type": "integer" 
       }, 
       "id": { 
        "type": "integer" 
       } 
      } 
     } 
    } 

您的用例可以或maynot包括過濾父文檔,但因爲你基本上是需要過濾嵌套文件相匹配的一些標準,你可以使用嵌套過濾器/帶innerhits查詢。 elasticsearch中的innerhits返回一個文檔中的內嵌套文檔與您的嵌套查詢匹配。

讓我們假設兩個情況下─

a)不過濾父文檔 -

{ 
    "query": { 
     "filtered": { 
      "query": { 
       "bool": { 
        "must": [{ 
         "nested": { 
          "path": "stores", 
          "query": { 
           "filtered": { 
            "query": { 
             "bool": { 
              "must": [{ 
               "term": { 
                "stores.name": { 
                 "value": "nokia" 
                } 
               } 
              }] 
             } 
            } 
           } 
          }, 
          "inner_hits":{} 
         } 
        }] 
       } 
      } 
     } 
    } 
} 

b)中篩選既爲親和嵌套對象 -

{ 
    "query": { 
     "filtered": { 
      "query": { 
       "bool": { 
        "must": [ 
         {"term": { 
         "name": { 
          "value": "samsung" 
         } 
         }}, 
         { 
         "nested": { 
          "path": "stores", 
          "query": { 
           "filtered": { 
            "query": { 
             "bool": { 
              "must": [{ 
               "term": { 
                "stores.name": { 
                 "value": "nokia" 
                } 
               } 
              }] 
             } 
            } 
           } 
          }, 
          "inner_hits":{} 
         } 
        }] 
       } 
      } 
     } 
    } 
} 

在情況b中,可以在父文檔上也有一個過濾器。

Innerhits響應 -

{ 
    "took": 6, 
    "timed_out": false, 
    "_shards": { 
    "total": 5, 
    "successful": 5, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 1, 
    "max_score": 2.122635, 
    "hits": [ 
     { 
     "_index": "nested", 
     "_type": "product", 
     "_id": "AVmYrzR0isULrG9L5jlr", 
     "_score": 2.122635, 
     "_source": { 
      "name": "Samsung S7", 
      "rating": 4.5123123, 
      "id": "609126", 
      "stores": [ 
      { 
       "id": 123214, 
       "name": "Walmart" 
      }, 
      { 
       "id": 898989, 
       "name": "nokia" 
      }, 
      { 
       "id": 67676, 
       "name": "nokia" 
      } 
      ] 
     }, 
     "inner_hits": { 
      "stores": { 
      "hits": { 
       "total": 2, 
       "max_score": 1.8472979, 
       "hits": [ 
       { 
        "_index": "nested", 
        "_type": "product", 
        "_id": "AVmYrzR0isULrG9L5jlr", 
        "_nested": { 
        "field": "stores", 
        "offset": 2 
        }, 
        "_score": 1.8472979, 
        "_source": { 
        "id": 67676, 
        "name": "nokia" 
        } 
       }, 
       { 
        "_index": "nested", 
        "_type": "product", 
        "_id": "AVmYrzR0isULrG9L5jlr", 
        "_nested": { 
        "field": "stores", 
        "offset": 1 
        }, 
        "_score": 1.8472979, 
        "_source": { 
        "id": 898989, 
        "name": "nokia" 
        } 
       } 
       ] 
      } 
      } 
     } 
     } 
    ] 
    } 
} 

正如你可以在響應對象看到,inner_hits出來的附加JSON對象與源文檔一起,innerhits JSON對象包含匹配的「所有過濾嵌套文件的陣列諾基亞「作爲名稱。

你確實得到了你所要求的形式,但採用了不同的格式/數據結構。您可以稍後編寫API層翻譯器,模型序列化程序將響應解析爲您希望客戶端(瀏覽器/應用程序)要求的正確格式。

請參閱elastic innerhits doc以獲取更多參考。

希望這會有所幫助 謝謝