2013-05-02 57 views
2

我想爲具有動態屬性的嵌套類型動態創建構面。在Elasticsearch中創建動態構面

假設這些文章都在我的索引和我在尋找名稱和品牌:

{ 
    name: iPhone 16GB 
    brand: Apple 
    type: Smartphone 
    attributes: { 
     Color: White 

    } 
} 
{ 
    name: iPad 
    brand: Apple 
    type: Tablet 
    attributes: { 
     Color: Black 
    } 
} 
{ 
    name: Cruzer USB 16GB 
    brand: SanDisk 
    type: USB-Stick 
    attributes: { 
     Color: Black 
     Capacity: 16GB 
     USB-Standard: USB 2.0 
    } 
} 
{ 
    name: Cruzer USB 16GB 
    brand: SanDisk 
    type: USB-Stick 
    attributes: { 
     Color: Red 
     Capacity: 16GB 
     USB-Standard: USB 3.0 
    } 
} 

現在,如果我在尋找「蘋果」我希望有一個具有搜索結果以下方面:

Brand: 
    Apple 2 
Type: 
    Smartphone 1 
    Tablet 1 
Color: 
    Black 1 
    White 1 

一種用於 '16GB' 搜索應包括那些方面:

Brand: 
    Apple 1 
    SanDisk 2 
Type: 
    Smartphone 1 
    USB-Stick 2 
Color: 
    Black 1 
    White 1 
    Red 1 
Capacity: 
    16GB 2 
USB-Standard: 
    USB 3.0 1 
    USB 2.0 2 

瀝因此,每篇文章都可以具有任意屬性。

品牌和類型的分面搜索很容易,但我怎麼能做到這一點?彈性搜索甚至可能嗎?

我希望你能unterstand我的意思......

+0

一個想法是做一個搜索,並增加了對每個屬性方面之前得到的映射嘗試。但我不認爲這是一個好的和有效的解決方案... – Felix 2013-05-02 15:24:53

+0

另一種解決方案是先做一個非分面搜索。只要您獲得搜索結果,就會執行另一次搜索(通過結果集的ID)。在此搜索中,您可以通過查看結果的屬性來創建方面。 但是,這將包括另一個網絡roudtrip ... 我仍然希望有人有一個真正的解決方案。 – Felix 2013-05-03 07:43:17

+0

爲什麼扁平化所有屬性不是有效的解決方案? – 2013-05-03 13:31:47

回答

0

下面

POST _search 
{ 
    "query": { 
     "query_string": { 
     "query": "Apple" 
     } 
    }, 
    "facets": { 
     "tags": { 
     "terms": { 
      "fields": ["name","brand","type","attributes.Color"] 
     } 
     } 
    } 
} 
+0

事情是,我不知道哪些屬性與查詢匹配的文章具有屬性。 – Felix 2014-11-19 07:40:01

+0

它會檢查每個屬性的文章 – 2014-11-25 09:15:40

+0

如何?在您的查詢中,您只指定了顏色屬性(「attributes.Color」)。 – Felix 2014-11-25 11:23:10