ElasticSearch查询,不包含内容

huangapple go评论73阅读模式
英文:

ElasticSearch query with not contains

问题

我正在编写一个Java应用程序,使用Elasticsearch作为数据库。

我正在寻找一种编写“不包含”查询的方法。

例如,我有一个带有以下值的类别字段:

 - 车
 - 公共汽车
 - 火车
 - 快车

现在我想获取所有不在类别中包含“fast”的条目。

我应该如何构建这样的查询?
我已经在这里四处寻找,但找不到任何信息。

提前致谢

所以感谢@James和@Bhavya Gupta,我可以更新我的查询,但它的实际效果不如预期。

我的查询现在是:

"query": {
     "bool": {
         "must": {
             "match_all": {}
         },
         "filter": {
             "bool": {
                 "must_not": [
                     {
                         "term": {
                             "BetriebsID": "Aufzug"
                         }
                     }
                 ]
             }
         }
     }
 }

但结果仍然包含诸如此类的命中:

"hits": [
    {
        "_source": {
            ...
            "FormNo": 9150520000000692,
            "CreatedTime": "2020-05-15T08:19:03.000Z",
            "templateId": "Aufzugsstoerung",
            "SYS_FORM_ID": "150520_000000692",
            "trecId": 151,
            "BetriebsID": "20200515_1019_Aufzugsstoerung",
            "displayName": "Aufzugsstörung",
            ...
        }
    }
]

我还尝试了James提供的通配符查询,但结果相同。

英文:

I'm writing a Java application and use elastic search as database.

I'm looking for a way to write a "not-contains" query.

For Example I've got a field category with values:

  • Car
  • Bus
  • Train
  • Fastcar

Now I want to get all entries which not contains "fast" in their category.

How can I build a query like this?
I have been looking around here but can't find anything.

Thanks in advance

So thanks to @James and @Bhavya Gupta I could update my query but it doesn't work as expected.

My query now is:

"query": {
        "bool": {
            "must": {
                "match_all": {}
            },
            "filter": {
                "bool": {
                    "must_not": [
                        {
                            "term": {
                                "BetriebsID": "Aufzug"
                            }
                        }   ]
                    }
                }
        }
    }

But the result still contains hits like this:

"hits": [
            "_source": {
                ...
                "FormNo": 9150520000000692,
                "CreatedTime": "2020-05-15T08:19:03.000Z",
                "templateId": "Aufzugsstoerung",
                "SYS_FORM_ID": "150520_000000692",
                "trecId": 151,
                "BetriebsID": "20200515_1019_Aufzugsstoerung",
                "displayName": "Aufzugsstörung",
                ...
            }
            }]

I also tried it with the wildcard query from James, with the same result.

答案1

得分: 2

你可以使用must_not进行布尔查询。Kibana 查询如下:

GET my_index/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "wildcard": {
            "category": {
              "value": "fast*"
            }
          }
        }
      ]
    }
  }
}

示例 Java 代码:

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
    .query(QueryBuilders.boolQuery().mustNot(QueryBuilders.wildcardQuery("category", "fast*")));
SearchRequest searchRequest = new SearchRequest("index_name");
searchRequest.source(searchSourceBuilder);
SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
英文:

You can make use of Boolean Queries with must_not for the same. Kibana query would look like:

GET my_index/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "wildcard": {
            "category": {
              "value": "fast*"
            }
          }
        }
      ]
    }
  }
}

Sample Java Code:

		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
				.query(QueryBuilders.boolQuery().mustNot(QueryBuilders.wildcardQuery("category", "fast*")));
		SearchRequest searchRequest = new SearchRequest("index_name");
		searchRequest.source(searchSourceBuilder);
		SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

答案2

得分: 2

通配符查询也可以像@James建议的那样使用,但不建议在搜索查询的开头使用通配符(特别是*),因为它可能会影响性能并减慢搜索速度。

添加一个带有索引映射、搜索查询和搜索结果的工作示例。

索引映射:

{
    "settings": {
        "analysis": {
            "analyzer": {
                "my_analyzer": {
                    "tokenizer": "my_tokenizer"
                }
            },
            "tokenizer": {
                "my_tokenizer": {
                    "type": "ngram",
                    "min_gram": 2,
                    "max_gram": 10,
                    "token_chars": [
                        "letter",
                        "digit"
                    ]
                }
            }
        },
        "max_ngram_diff": 50
    },
    "mappings": {
        "properties": {
            "category": {
                "type": "text",
                "analyzer": "my_analyzer",
                "search_analyzer": "standard"
            }
        }
    }
}

搜索查询:

{
    "query": {
        "bool": {
            "must": {
                "match_all": {}
            },
            "filter": {
                "bool": {
                    "must_not": [
                        {
                            "term": {
                                "category": "fast"
                            }
                        }
                    ]
                }
            }
        }
    }
}

搜索结果:

"hits": [
      {
        "_index": "stof_64009221",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.0,
        "_source": {
          "category": "car"
        }
      },
      {
        "_index": "stof_64009221",
        "_type": "_doc",
        "_id": "2",
        "_score": 1.0,
        "_source": {
          "category": "bus"
        }
      },
      {
        "_index": "stof_64009221",
        "_type": "_doc",
        "_id": "3",
        "_score": 1.0,
        "_source": {
          "category": "train"
        }
      }
    ]
英文:

Wildcard queries can also be used as suggested by @James, but it is not recommended to use Wildcard (especially at the beginning of search query), as it may affect the performance and slow down the search.

Adding a working example with index mapping, search query, and search result

Index Mapping:

{
    "settings": {
        "analysis": {
            "analyzer": {
                "my_analyzer": {
                    "tokenizer": "my_tokenizer"
                }
            },
            "tokenizer": {
                "my_tokenizer": {
                    "type": "ngram",
                    "min_gram": 2,
                    "max_gram": 10,
                    "token_chars": [
                        "letter",
                        "digit"
                    ]
                }
            }
        },
        "max_ngram_diff": 50
    },
    "mappings": {
        "properties": {
            "category": {
                "type": "text",
                "analyzer": "my_analyzer",
                "search_analyzer": "standard"
            }
        }
    }
}

Search Query:

{
    "query": {
        "bool": {
            "must": {
                "match_all": {}
            },
            "filter": {
                "bool": {
                    "must_not": [
                        {
                            "term": {
                                "category": "fast"
                            }
                        }
                    ]
                }
            }
        }
    }
}

Search Result:

"hits": [
      {
        "_index": "stof_64009221",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.0,
        "_source": {
          "category": "car"
        }
      },
      {
        "_index": "stof_64009221",
        "_type": "_doc",
        "_id": "2",
        "_score": 1.0,
        "_source": {
          "category": "bus"
        }
      },
      {
        "_index": "stof_64009221",
        "_type": "_doc",
        "_id": "3",
        "_score": 1.0,
        "_source": {
          "category": "train"
        }
      }
    ]

huangapple
  • 本文由 发表于 2020年9月22日 19:54:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/64009221.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定