2015-04-28 66 views
3

我正在尋找ElasticSearch查詢,該查詢將在其中包含空格的字符串提供精確匹配。彈性搜索 - 在其中包含空格的搜索字符串

例如 - 我想搜索一個詞,如'XYZ公司解決方案'。 我試過querystring查詢,但它給我所有的記錄,不論搜索結果如何。另外我在帖子上閱讀,發現我們必須爲該領域添加一些映射。我在該領域嘗試了'Not_Analyzed'分析儀,但仍然無法正常工作。

如果有人有完整的例子或步驟,那麼你可以請與我分享?

在此先感謝。

謝謝,薩米爾

回答

4

既然你沒有張貼你的代碼很難說什麼是錯的,但"index": "not_analyzed"在你的映射來處理這個正確的方式。

這是一個簡單的工作示例。首先,我創建一個使用"index": "not_analyzed"的映射:

PUT /test_index 
{ 
    "mappings": { 
     "doc": { 
      "properties": { 
       "name":{ 
        "type": "string", 
        "index": "not_analyzed" 
       } 
      } 
     } 
    } 
} 

然後添加幾個文件來測試

POST /test_index/doc/_bulk 
{"index":{"_id":1}} 
{"name":"XYZ Company Solutions"} 
{"index":{"_id":2}} 
{"name":"Another Company"} 

現在我能得到我想要一個簡單的term query文件:

POST /test_index/doc/_search 
{ 
    "query": { 
     "term": { 
      "name": { 
       "value": "XYZ Company Solutions" 
      } 
     } 
    } 
} 
... 
{ 
    "took": 1, 
    "timed_out": false, 
    "_shards": { 
     "total": 1, 
     "successful": 1, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 1, 
     "max_score": 1, 
     "hits": [ 
     { 
      "_index": "test_index", 
      "_type": "doc", 
      "_id": "1", 
      "_score": 1, 
      "_source": { 
       "name": "XYZ Company Solutions" 
      } 
     } 
     ] 
    } 
} 

A term filter甚至match query也可以在這種情況下工作。

這裏是我用來測試它的代碼:我使用的設置和映射上面的定義索引

http://sense.qbox.io/gist/90fcc7f7a88d58f098c50d5aaf0315fdf06e9e9a

+1

我只是補充說,自從版本5.0以來,不再支持類型「字符串」,並引入了兩個新的字符串。 「text」默認設置爲分析,「keyword」設置爲not_analyze。因此,如果您使用的是較新的版本,則應該足以將類型更改爲「關鍵字」。 –

0
PUT /index_1 
{ 
    "settings": { 
    "analysis": { 
     "normalizer": { 
     "lowercase_normalizer": { "type": "custom", "char_filter": [],   "filter": ["lowercase"]} 
     } 
    } 
    }, 
    "mappings": { 
    "doc_type": { 
      "properties": { 
       "name":{"type": "keyword", "normalizer": "lowercase_normalizer"} 
      } 
    } 
    } 
} 

。 然後推幾個值到數據庫中現在

POST index_1/doc_type/1 
{ 
    "name" : "a b c" 
} 

POST index_1/doc_type/1 
{ 
    "name" : "a c" 
} 

POST index_1/doc_type/1 
{ 
    "name" : "a b" 
} 

,如果我們在上述指標的名稱字段搜索單個字母,我們得到什麼回報

GET index_1/doc_type/_search 
{ 
    "query" : 
    {"match": {"name": "A"}} 
} 

GET index_1/doc_type/_search 
{ 
    "query" : 
    {"match": {"name": "b"}} 
} 

但如果我們搜索

GET index_1/doc_type/_search 
{ 
    "query" : 
    {"match": {"name": "A B C"}} 
} 

我們將得到匹配

這有助於搜索com完成關鍵字,同時避免區分大小寫

相關問題