elasticsearch完全匹配字符串包括点

时间:2017-04-12 11:12:17

标签: elasticsearch elastic-stack

我有以下索引文档:

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 个答案:

答案 0 :(得分:1)

当您使用keyword字段上的foo字段搜索确切字词时,如下所示:

  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-fields here

的更多信息

方式-2:

您可以为analyzer字段使用不同的whitespace(例如foo分析器),同时为其定义映射来解决此问题。

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

但正如您在exact terms 上搜索方法1 时,方法2