【问题标题】:elasticsearch exact matching string include dotelasticsearch精确匹配字符串包括点
【发布时间】:2017-04-12 11:12:16
【问题描述】:

我有以下索引文档:

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 不匹配。

谢谢。

【问题讨论】:

    标签: elasticsearch elastic-stack


    【解决方案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

    您可以通过在为 foo 字段定义映射时使用不同的 analyzer(例如 whitespace 分析器)来解决它。

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

    但是当您在 exact terms 上搜索时,方法 #1 优于 方法 #2

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-18
      • 1970-01-01
      • 2020-08-21
      • 1970-01-01
      相关资源
      最近更新 更多