如何解决elasticsearch query_string关键字搜索与或运算符不起作用
我为此问题创建了一个简单的复制器,如下所示:
PUT trial
{
"settings": {
"analysis": {
"analyzer": {
"default": {
"type": "custom","tokenizer": "whitespace","filter": [ "lowercase" ]
}
}
}
},"mappings": {
"properties": {
"status": {
"type": "keyword"
}
}
}
}
PUT trial/_doc/2
{
"status": "not_installed"
}
PUT trial/_doc/1
{
"status": "installed"
}
POST trial/_search
{
"from": 0,"size": 20,"query": {
"bool": {
"filter": [
{
"query_string": {
"query": "not_installed installed","default_operator": "or"
}
}
]
}
}
}
如果我从上到下依次执行这些命令,则期望doc 1,2为结果。但这并没有被获取。我的理解有什么问题?有人可以帮忙吗? 使用ES版本7.4
解决方法
在搜索查询中尝试以下操作(采用相同的索引映射和数据)
{
"from": 0,"size": 20,"query": {
"bool": {
"filter": [
{
"query_string": {
"query": "not_installed OR installed"
}
}
]
}
}
}
上面的查询给出了预期的结果。要了解有关query_string
的更多信息,请参阅此documentation。
更新1:
Elasticsearch依靠分析器来拆分所有 在查询运算符之间转换成术语。并且
keyword
字段上的分析器是keyword
分析器。
因此必须有一种配置关键字字段的方法,以便文本 将在查询时在空白处拆分。因此, 将
split_queries_on_whitespace
参数添加到 设置为true时,将空白分析器设置为搜索分析器。 通过添加此参数并将其设置为真正的针对字段的全文查询(match
,multi_match
,query_string
等),将在空格上拆分输入以构建查询词
请参阅以下GitHub问题以了解有关此内容的更多信息:
https://github.com/elastic/elasticsearch/issues/29148 https://github.com/elastic/elasticsearch/issues/30131 https://github.com/elastic/elasticsearch/issues/30393 https://github.com/elastic/elasticsearch/pull/30691
在这种情况下,您需要在关键字字段中添加split_queries_on_whitespace
参数,以在对字段status
进行查询时在空白处分割输入。要了解更多有关此信息,请参阅Keyword datatype上的此官方文档。
索引映射:
修改后的映射如下:
{
"settings": {
"analysis": {
"analyzer": {
"default": {
"type": "custom","tokenizer": "whitespace","filter": [ "lowercase" ]
}
}
}
},"mappings": {
"properties": {
"status": {
"type": "keyword","split_queries_on_whitespace": true --> note this
}
}
}
}
搜索查询:
{
"from": 0,"query": {
"bool": {
"filter": [
{
"query_string": {
"query": "installed not_installed","default_operator":"OR"
}
}
]
}
}
}
搜索结果:
"hits": [
{
"_index": "my_index","_type": "_doc","_id": "1","_score": 0.0,"_source": {
"status": "not_installed"
}
},{
"_index": "my_index","_id": "2","_source": {
"status": "installed"
}
}
]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。