2015-02-09 66 views
1

我使用匹配來搜索特定電子郵件,但結果是錯誤的。匹配屬性給我帶來了類似的結果。如果結果存在,結果顯示在第一行,但是當結果不存在時,結果會由同一個域導致。ElasticSearch - 匹配(電子郵件值)返回錯誤的寄存器

這裏是我的查詢:

{ 
    "query": { 
     "match" : { 
      "email" : "[email protected]" 
     } 
    } 
} 

此電子郵件不會在我的基地存在,但返回如香蕉值@xxx.net,ronyvon @xxx.net *

如何從查詢中強制返回值僅爲等於

提前致謝。

回答

2

您需要在"email"字段輸入"index":"not_analyzed"。這樣,查詢的唯一條款是已存儲到該字段的確切值(與standard analyzer的情況相反,如果沒有列出分析儀,則爲默認值)。

爲了說明這一點,我設置了不分析email領域一個簡單的映射,並增加了兩個簡單的文檔:

DELETE /test_index 

PUT /test_index 
{ 
    "settings": { 
     "number_of_shards": 1, 
     "number_of_replicas": 0 
    }, 
    "mappings": { 
     "doc": { 
     "properties": { 
      "email": { 
       "type": "string", 
       "index": "not_analyzed" 
      } 
     } 
     } 
    } 
} 

PUT /test_index/doc/1 
{"email": "[email protected]"} 

PUT /test_index/doc/2 
{"email": "[email protected]"} 

現在你匹配查詢將只返回精確匹配查詢的文檔:

POST /test_index/_search 
{ 
    "query": { 
     "match" : { 
      "email" : "[email protected]" 
     } 
    } 
} 
... 
{ 
    "took": 2, 
    "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": { 
       "email": "[email protected]" 
      } 
     } 
     ] 
    } 
} 

這裏是我使用的代碼:

http://sense.qbox.io/gist/12763f63f2a75bf30ff956c25097b5955074508a

PS:您實際上可能想要的是term query甚至term filter,因爲您不希望對查詢文本進行任何分析。所以也許是這樣的:

POST /test_index/_search 
{ 
    "query": { 
     "constant_score": { 
     "filter": { 
      "term": { 
       "email": "[email protected]" 
      } 
     } 
     } 
    } 
} 
+0

是否可能將它添加到所有索引? – placplacboom 2015-02-10 12:10:41

+0

您可以設置如下所示的內容:https://github.com/elasticsearch/elasticsearch/issues/8015,使用[copy_to](http://www.elasticsearch.org/guide/en/elasticsearch/reference /current/mapping-core-types.html#copy-to)。我無法使用''_all「'字段使其工作。 – 2015-02-10 17:12:59

+0

非常感謝! – placplacboom 2015-02-10 17:13:50