Elasticsearch query_string按词搜索复杂关键字



现在,我知道关键字不应该包含非结构化文本,但是假设由于某种原因,恰好将此类文本写入关键字字段。 使用匹配或术语查询搜索此类文档时,找不到文档,但当使用query_string搜索时,通过部分匹配(关键字内的"术语"(找到文档。我不明白当 Elasticsearch 的文档明确指出关键字按原样反向索引,没有术语标记化时,这怎么可能。 例: 我的索引映射:

PUT my_index
{
"mappings": {
"my_type": {
"properties": {
"full_text": {
"type":  "text" 
},
"exact_value": {
"type":  "keyword" 
}
}
}
}
}

然后我放了一个文档:

PUT my_index/my_type/2
{
"full_text":   "full text search", 
"exact_value": "i want to find this trololo!"  
}

想象一下,当我按关键字术语而不是完全匹配获得文档时,我的惊讶:

GET my_index/my_type/_search
{
"query": {
"match": {
"exact_value": "trololo" 
}
}
}

- 没有结果;

GET my_index/my_type/_search
{
"query": {
"term": {
"exact_value": "trololo" 
}
}
}

- 没有结果;

POST my_index/_search
{"query":{"query_string":{"query":"trololo"}}}

- 我的文档被返回(!

"hits": {
"total": 1,
"max_score": 0.27233246,
"hits": [
{
"_index": "my_index",
"_type": "my_type",
"_id": "2",
"_score": 0.27233246,
"_source": {
"full_text": "full text search",
"exact_value": "i want to find this trololo!"
}
}
]
}

当您在 elastic 上执行如下所示的query_string查询时

POST index/_search
{
"query": {
"query_string": {
"query": "trololo"
}
}
}

这实际上对_all字段进行搜索,如果您不提及,则由弹性中的标准分析器进行分析。

如果在查询中指定字段,如下所示,则不会获取关键字字段的记录。

POST my_index/_search
{
"query": {
"query_string": {
"default_field": "exact_value", 
"query": "field"
}
}
}

最新更新