2017-04-12 98 views
0

我有以下索引的文檔:elasticsearch精確匹配字符串包括點

curl -XGET "http://127.0.0.1:8200/logstash-test/1/_search" 

{ 
    "took": 1, 
    "timed_out": false, 
    "_shards": { 
    "total": 5, 
    "successful": 5, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 4, 
    "max_score": 1, 
    "hits": [ 
     { 
     "_index": "logstash-test", 
     "_type": "1", 
     "_id": "AVthzksHqNe69jLmmCEp", 
     "_score": 1, 
     "_source": { 
      "foo": "bar2" 
     } 
     }, 
     { 
     "_index": "logstash-test", 
     "_type": "1", 
     "_id": "AVthzlbfqNe69jLmmCSr", 
     "_score": 1, 
     "_source": { 
      "foo": "bar3" 
     } 
     }, 
     { 
     "_index": "logstash-test", 
     "_type": "1", 
     "_id": "AVthwg4_qNe69jLmlStd", 
     "_score": 1, 
     "_source": { 
      "foo": "bar" 
     } 
     }, 
     { 
     "_index": "logstash-test", 
     "_type": "1", 
     "_id": "AVth0IS1qNe69jLmmMpZ", 
     "_score": 1, 
     "_source": { 
      "foo": "bar4.foo_bar.foo" 
     } 
     } 
    ] 
    } 
} 

我要搜索foo=bar2 or foo=ba3 or foo=bar4.foo_bar.foo

curl -XPOST "http://127.0.0.1:8200/logstash-test/1/_search" -d 
    '{"query":{"bool":{"filter":[{"terms":{"foo":["bar3","bar2","bar4.foo_bar.foo"]}}]}}}' 

bar4.foo_bar.foo不匹配。

謝謝。

回答

1

當你在具體條款上使用foo場可用keyword野外搜索,如下圖所示:

curl -XPOST "http://127.0.0.1:8200/logstash-test/1/_search" -d 
    '{ 
    "query": { 
    "bool": { 
     "filter": [ 
     { 
      "terms": { 
      "foo.keyword": [ 
       "bar3", 
       "bar2", 
       "bar4.foo_bar.foo" 
      ] 
      } 
     } 
     ] 
    } 
    } 
}' 

你可以閱讀更多關於multi-fieldshere

方法2:

您可以通過使用不同的analyzer(例如whitespace分析儀)來解決它foo f在爲其定義映射的時候。

PUT logstash-test 
{ 
"mappings": { 
    "1": { 
     "properties": { 
     "foo": { 
      "type": "text", 
      "analyzer": "whitespace" 
     } 
     } 
    } 
    } 
} 

但是作爲對你是在exact terms搜索方法-1是prefferred超過方法-2