[英]Elasticsearch boolean query doesn't work with filter
I'm not very strong in Elasticsearch.我在 Elasticsearch 方面不是很擅长。 I'm trying to set up search in my app and got some strange problems.我正在尝试在我的应用程序中设置搜索并遇到一些奇怪的问题。 I have two documents:我有两个文件:
{
"title": "Second insight"
"content": "Bla bla bla"
"library": "workspace"
}
{
"title": "Test source"
"content": "Bla bla bla"
"library": "workspace"
}
Then, I want to be able to make a search by text fields like title
and content
and apply some filters on fields like library
.然后,我希望能够通过title
和content
等文本字段进行搜索,并在library
等字段上应用一些过滤器。 I have a query:我有一个疑问:
{
"query": {
"bool": {
"should": [
{ "match": { "title": "insight" }}
],
"filter": [
{
"term": {
"library": "workspace"
}
}
]
}
}
}
Despite the fact that I clearly defined title
to be matched to insight
, the query above returns both documents, not only the first one.尽管我明确定义了要与insight
匹配的title
,但上面的查询返回两个文档,而不仅仅是第一个。
If I remove filter
block:如果我删除filter
块:
{
"query": {
"bool": {
"should": [
{ "match": { "title": "insight" }}
]
}
}
}
the query returns correct results.查询返回正确的结果。
Then, I also tried to make a partial search.然后,我也尝试进行部分搜索。 For some reasons, the query uses ins
instead of insight
below doesn't work, so, it returns empty list:由于某些原因,查询使用ins
而不是下面的insight
不起作用,因此,它返回空列表:
{
"query": {
"bool": {
"should": [
{ "match": { "title": "ins" }}
]
}
}
}
How should I make partial search?我应该如何进行部分搜索? And how can I set up filters correctly?以及如何正确设置过滤器? In other words, how to make a search partial query by some fields, but at the same time filtered by other fields?换句话说,如何通过某些字段进行搜索部分查询,但同时被其他字段过滤?
Thanks.谢谢。
You need to supply minimum_should_match
in your first query.您需要在第一个查询中提供minimum_should_match
。
I did the following and only got a single document (your desired outcome)我做了以下,只得到一个文件(你想要的结果)
POST test_things/_search
{
"query": {
"bool": {
"minimum_should_match": 1,
"should": [
{
"match": {
"title": "insight"
}
}
],
"filter": [
{
"term": {
"library": "workspace"
}
}
]
}
}
}
As for why ins
doesn't work, it depends on your mapping + analyzer being used.至于为什么ins
不起作用,这取决于您使用的映射+分析器。 You are matching against analyzed terms in the index, if you want to match against ins
you need to change your analyzer (possibly using the ngram tokenizer ) or use a wildcard query .您正在与索引中的分析词匹配,如果您想与ins
匹配,您需要更改分析器(可能使用ngram tokenizer )或使用通配符查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.