2013-11-04 71 views
1

我正在使用Tire/ElasticSearch創建按字母順序瀏覽數據庫中的所有標籤。然而,輪胎搜索返回標籤我想以及所有其他標籤關​​聯到同一項目。因此,例如,如果我的信件是「A」,而某件商品上有「Aardvark」和「biscuit」標籤,則「Aardvark」和「餅乾」都會顯示爲「A」查詢的結果。我怎樣才能構建這個,以便我只能得到'aardvark'?輪胎搜索按首字母返回術語

def explore 
#get alphabetical tire results with term and count only 

    my_letter = "A" 

    self.search_result = Tire.search index_name, 'tags' => 'count' do 
     query {string 'tags:' + my_letter + '*'} 
     facet 'tags' do 
      terms 'tags', :order => 'term' 
     end 

    end.results 
    end 

映射:

{ 
    items: { 
     item: { 
      properties: { 
       tags: { 
        type: "string", 
        index_name: "tag", 
        index: "not_analyzed", 
        omit_norms: true, 
        index_options: "docs" 
        }, 
       } 
      } 
     } 
    } 
+0

你可以發佈你的**項目**和**標籤**的映射嗎?另外,你能指定你想要什麼嗎?我的意思是,你想要物品和它的標籤嗎?還是隻有標籤才能找到? –

+0

我只想要標籤和計數列表。因此,如果您點擊A鏈接,您將獲得以A開頭的所有標籤以及每個標籤在數據庫中顯示的次數。點擊B標籤可以得到所有以B開頭的標籤,依此類推。我編輯了我的帖子以包含映射。 –

回答

2

以下的事情,你需要改變:

  1. 映射 您需要的標籤,以便通過他們尋找正確映射。作爲您的標籤,在您的項目文檔中,您需要將標籤的屬性設置爲嵌套,以便您也可以在方面應用搜索查詢。下面是你需要設置映射:

    { 
        item: { 
        items: { 
         properties: { 
         tags: { 
          properties: { 
          type: "nested", 
          properties: { 
           value: { 
           type: "string", 
           analyzer: 'not_analyzed' 
           } 
          } 
          } 
         } 
        } 
        } 
        } 
    } 
    
  2. 查詢 現在,您可以使用前綴查詢通過具有一定的字母開頭,並獲得方面的標籤進行搜索,下面是完整的查詢:在計算方面

     
    query: { 
        nested: { 
        path: "tags", 
        query: { 
         prefix: { 
         'tags.value' : 'A' 
         } 
        } 
        } 
    } 
    facets: { 
        words: { 
        terms: {field: 'tags.value'}, 
        type: 'nested', 
        facet_filter: {prefix: { 
              'tags.value' : 'A' 
              } 
            } 
        } 
    } 
    

小面過濾器被應用,所以你只能得到其中將匹配您的標準方面。我更喜歡在普通的exp上進行前綴查詢由於性能問題而進行查詢。但我不太確定前綴查詢是否適用於您的問題。讓我知道它不起作用。