使用 - 和 * 在 Elasticsearch 中查询字符串查询

Query String Query in Elasticsearch with - and *

我在 Elasticsearch 中遇到 Query String Query 的问题。我这里写个Sense代码复制一下。

POST myindex
POST myindex/mytype
    {
        "name":"t-shirt",
        "season": "2016-3"
    }

然后我搜索:

POST myindex/_search
{
    "query": {
        "query_string": {
           "query": "2016-*",
           "fields": ["name", "season"]
        }
    }
}

或者:

POST myindex/_search
{
    "query": {
        "query_string": {
           "query": "t-shirt*"
        }
    }
}

这些查询 return 没有文档(但它应该 return 索引文档)而且我不知道如何修复它。

我希望尽可能通用,因为这是一个示例,但我的文档有很多字段,用户可以不受语法限制地搜索他想要的内容。

name 字段已被 standard analyzer 分析并产生了两个标记,即 tshirt,正如您在下面的 [=15= 中看到的那样] 查询

curl -XGET localhost:9200/test/_analyze?pretty -d 't-shirt'
{
  "tokens" : [ {
    "token" : "t",
    "start_offset" : 0,
    "end_offset" : 1,
    "type" : "<ALPHANUM>",
    "position" : 0
  }, {
    "token" : "shirt",
    "start_offset" : 2,
    "end_offset" : 7,
    "type" : "<ALPHANUM>",
    "position" : 1
  } ]
}

您可以搜索 shirt*,您会得到一些结果

POST myindex/_search
{
    "query": {
        "query_string": {
           "query": "shirt*"
        }
    }
}

尝试wildcard query

POST myindex/_search
{
  "query": {
    "wildcard": {
      "season": {
        "value": "2016-*"
      }
    }
  }
}

我使用了 "analyze_wildcard": true(默认为 false),解决了在同一查询中使用 - 和 * 搜索时返回零文档的问题。

注意:"fields": ["_all"](或不指定)与指定所有字段的名称(例如 "fields": ["name", "season"])之间存在不同的行为。在我最完整的测试中,我尝试了它。