查找包含带特殊字符的子字符串的文档

Find documents containing substring with special characters

我在 elasticsearch v5.2.2 中有一些文档如下所示:

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "company",
        "_type": "external",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "IBM",
          "bloomberg": "IBMB34:BZ"
        }
      },
      {
        "_index": "company",
        "_type": "external",
        "_id": "1",
        "_score": 1,
        "_source": {
          "name": "Apple",
          "bloomberg": "AAPL34:BZ"
        }
      },
      {
        "_index": "company",
        "_type": "external",
        "_id": "3",
        "_score": 1,
        "_source": {
          "name": "Microsoft",
          "bloomberg": "MSFT34:BZ"
        }
      }
    ]
  }
}

我想查找包含可能包含特殊字符(在我的示例中为“:”)的子字符串的所有文档。

{
    "query": {
        "wildcard" : { "bloomberg" : "*b34*" }
    }
}

Returns 一个结果(符合预期)。

{
    "query": {
        "wildcard" : { "bloomberg" : "*b34:*" }
    }
}

Returnsnone。我尝试了 query_string 和其他各种方法,但似乎没有任何效果。

问题是我使用的映射只是开箱即用的映射。将 bloomberg 过滤器的分析器更改为 "whitespace" 为我解决了这个问题。

{
    "mappings": {
      "external" : {
        "properties" : {
          "name" : {
            "type" : "string",
            "analyzer": "standard",
            "search_analyzer": "standard"
          },
          "bloomberg" : {
            "type" : "string",
            "analyzer": "whitespace",
            "search_analyzer": "whitespace"
          }
        }
      }
    }
}