在Elasticsearch上使用通配符运行查询时,不会返回预期结果。
要复制该问题:
版本5.5.1
PUT / example-test /
{
"mappings": {
"Change": {
"properties": {
"id": {
"type": "keyword"
},
"searchProperties": {
"type": "nested",
"properties": {
"key": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"value": {
"type": "text",
"analyzer": "standard"
}
}
}
}
}
}
}
POST / example-test / Change / 01_MEP_01
{
"id": "01_MEP_01",
"searchProperties": [
{
"key": "a.x",
"value": "You take the red pill you stay in Wonderland, and I show you how deep the rabbit hole goes"
}
]
}
获取/ example-test / Change / _search
POST / example-test / Change / _search
{
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "you ta",
"default_operator": "and",
"allow_leading_wildcard": true,
"analyze_wildcard": true,
"fields": [
"_all"
]
}
}
]
}
}
}
从这里开始,我将仅指定查询的“查询”部分。我已经为几个示例运行了此操作:
+-------------------+-------+
| Query | Found |
+-------------------+-------+
| you ta | false |
| you ta* | true |
| you take* | true |
| land | false |
| *land* | true |
| *land,* | false |
| *land, | false |
| wonderland | true |
| wonderland, | true |
| wonderlan*, | false |
| \\*wonderland,\\* | true |
+-------------------+-------+
注意-在最后一个示例中,通读此处https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html的文档,其中指出了通配符前的反斜杠(可能是错误的):
另一种选择是在查询字符串本身中提供通配符字段搜索(正确地转义*符号)
这只是在查询中指定字段名称时吗?
如果我想允许我的用户搜索“土地”并返回文档(因为它包含“ wonderland”一词,)我将如何使用通配符返回我的文档?
在这里看来逗号引起了问题。这似乎很奇怪。查看通过运行生成的令牌:
POST / example-test / _analyze
{
"analyzer": "standard",
"text": "You take the red pill you stay in Wonderland, and I show you how deep the rabbit hole goes"
}
我们可以看到标记为“ wonderland”的“ Wonderland”。我的理解是,查询将流经相同的分析器(https://www.elastic.co/guide/en/elasticsearch/reference/current/search-analyzer.html),所以结果应该相同吗?看来不是这样吗?