2017-02-01 62 views
1

大家好我是新來的彈性搜索,但我已經通過基本的ElasticSearch 5.1文檔。Elasticsearch使用多個過濾器搜索查詢

問題在一條線:

搜索是成功的,但過濾器不能正常工作。正如@Darth_Vader指出

映射數據類型

{ 
    "properties": { 
     "title": {"type": "string"}, 
     "description": {"type": "string"}, 
     "slug": {"type": "string"}, 
     "course_type": {"type": "string", "index" : "not_analyzed"}, 
     "price": {"type": "string"}, 
     "categories": {"type": "keyword", "index" : "not_analyzed"}, 
     "tags": {"type" : "keyword"}, 
     // "tags": {"type" : "keyword", "index" : "not_analyzed"}, 
     "status": {"type" : "string","index" : "not_analyzed"}, 
    } 
} 

我試圖映射爲好。以下是我的映射

文檔索引(REQ-1)

.... 
{ 
    "_index": "learnings", 
    "_type": "materials", 
    "_id": "582d9xxxxxxxx9b27fab2c", 
    "_score": 1, 
    "_source": { 
     "title": "Mobile Marketing", 
     "slug": "mobile-marketing", 
     "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla eleifend hendrerit vehicula.", 
     "categories": [ 
     "Digital Marketing" 
     ], 
     "tags": [ 
     "digital-marketing", 
     "mobile-marketing" 
     ], 
     "status": "published" 
    } 
}, 
... 

就像上面我有一個像數百個文檔的索引

搜索足本,我使用

"query": { 
    "bool": { 
     "must": { 
      "multi_match" : { 
       "query" : "mobile", 
       "fields" : [ "title^5", "tags^4", "categories^3" ], 
       "operator": "and" 
      } 
     }, 
     "filter": { 
      "bool" : { 
       "must" : [ 
        {"term" : {"status": "published"} } 
       ] 
      } 
     } 
    } 
} 
  • 在上面的查詢中,最重要的搜索條件/過濾器是{"term" : {"status": "published"} }。每一個搜索結果都必須符合這個 的要求。

  • 現在從結果列表中,我想篩選更多。所以說我只想得到documents其中有mobile-marketing作爲tag。我的文檔(REQ-1)有這個tagmobile-marketing

現在的問題是:

如果我修改我的搜索查詢並添加我的要求過濾如下所示:儘管我的文檔(Req-1)(即使我的文檔沒有搜索結果(點擊率= 0) 0作爲tag

"filter": { 
    "bool" : { 
     "must" : [ 
      {"term" : {"status": "published"} }, 
      {"term" : {"tags": "mobile-marketing"} } 
     ] 
    } 
} 

BUT如果我改變濾波器{"tags": "mobile-marketing"}TO{"tags": "mobile"}時,得到需要的文件(REQ-1)作爲結果。

我想獲得使用此過濾器相同的文檔:{"tags": "mobile-marketing"}。那麼我在哪裏做錯了?

我的搜索查詢需要哪些修改?

感謝

+0

[對於有類似問題的人]。上面的程序和相關的代碼工作,請確保您在映射和其他相關操作時使用正確的索引名稱和索引類型。 –

回答

1

請問你映射tags

似乎你已經得到了你的映射tags字段爲分析。什麼*分析`是,從books

首先分析字符串,然後索引它。換句話說,請將此 字段索引爲全文。

因此,它首先分析它的價值看起來像移動營銷。因此,由於中間的連字符,它將分開存儲移動設備和市場營銷,並將其標記爲令牌。即:將移動營銷存儲到兩個不同的標記。

而如果它是not_analyzed

指數這一領域,所以它是搜索,但指數值嚴格按照 規定。不要分析它。

所以這將基本上存儲的價值,因爲它是沒有分析它,這應該做你的情況的伎倆。也許你應該看看point

希望它有幫助!

+0

感謝@Darth_vader,我沒有做任何映射,我只是創建了一個索引並開始填充文檔而沒有任何映射。我想我需要做的映射,並使其「類型」:「字符串」, 「索引」:「not_analyzed」。讓我檢查它是否有效。謝謝 –

+0

是的,讓我知道它是否有效@SiddharthaChowdhury。 – Kulasangar

+0

嘿@Darth_Vader,我試過映射,並在問題中添加了我的映射細節。但它沒有奏效。它的行爲方式相同。 –

相關問題