elasticsearch query_string 使用或运算符的关键字搜索不起作用
elasticsearch query_string keyword search with or operator not working
我已经为这个问题创建了一个简单的复制器,如下所示:
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 relies on the analyzer to split everything that is
in-between query operators into terms. And the analyzer on keyword
fields is a keyword
analyzer.
So there has to be a way to configure keyword fields so that text
would be split on whitespace at query time. Therefore,
split_queries_on_whitespace
parameter is added to the field that
sets a whitespace analyzer as a search analyzer when set to true.
By adding this parameter and setting it to true full-text queries (match
, multi_match
, query_string
,..) that target the field will split the input on whitespace to build the query terms.
请参阅这些 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,
"size": 20,
"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",
"_type": "_doc",
"_id": "2",
"_score": 0.0,
"_source": {
"status": "installed"
}
}
]
我已经为这个问题创建了一个简单的复制器,如下所示:
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 relies on the analyzer to split everything that is in-between query operators into terms. And the analyzer on
keyword
fields is akeyword
analyzer.
So there has to be a way to configure keyword fields so that text would be split on whitespace at query time. Therefore,
split_queries_on_whitespace
parameter is added to the field that sets a whitespace analyzer as a search analyzer when set to true. By adding this parameter and setting it to true full-text queries (match
,multi_match
,query_string
,..) that target the field will split the input on whitespace to build the query terms.
请参阅这些 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,
"size": 20,
"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",
"_type": "_doc",
"_id": "2",
"_score": 0.0,
"_source": {
"status": "installed"
}
}
]