是否可以在弹性搜索中使用没有文本搜索的过滤器 [英] Is it possible to only use filters without text search in elasticsearch
问题描述
我正在使用ES作为我的Laravel应用程序,我需要做一个仅包含过滤器和文本搜索的搜索查询,但我不知道如何写。
I am using ES for my Laravel app, and I need to do a search query that only contains filters and no "text search" but I am not sure on how to write it.
我必须使用match_all ,例如:
$query = [
'filtered' => [
'query' => [
'match_all' => []
],
'filter'=> [
'bool' => [
'must' => [
[ 'range' => [
'price' => [
'lte' => 9000
]
]
],
],
]
],
],
];
或像这样:
$query = [
'filtered' => [
'filter'=> [
'bool' => [
'must' => [
[ 'range' => [
'price' => [
'lte' => 9000
]
]
],
],
]
],
],
];
我想要的是仅使用无文本搜索的过滤布尔查询。
What I want is to only use a filtered bool query without text search.
推荐答案
事实上,如果您不在过滤的查询中指定查询
默认情况下使用match_all查询。引用 doc :
In fact, if you don't specify the query
part in your filtered query, a match_all query is used by default. Quoting the doc :
如果未指定查询,则默认为match_all查询。这个
意味着过滤的查询可以用来包装一个过滤器,所以
可以在任何查询都可以使用的地方使用。
If a query is not specified, it defaults to the match_all query. This means that the filtered query can be used to wrap just a filter, so that it can be used wherever a query is expected.
您的第二个查询应该执行以下任务:过滤器必须包含在过滤的
( doc )或 constant_score
doc )查询要使用的。
Your second query should do the job : filters must be wrapped either in filtered
(doc) or constant_score
(doc) queries to be used.
如果评分部分对您无用,您可以坚持过滤的
查询。
If the scoring part isn't useful for you, you can stick to the filtered
query.
最后一件事:你不需要将过滤器嵌入到一个 bool
过滤器中,除非你想将它与其他(s)过滤器。在您的演示案例中,您可以直接写:
Last thing : you don't have to nest your filter in a bool
filter, unless you want to combine it with other(s) filter(s). In your demo case, you can write directly :
$query = [
'filtered' => [
'filter'=> [
'range' => [
'price' => [
'lte' => 9000
]
]
]
]
];
希望这将有所帮助:)
这篇关于是否可以在弹性搜索中使用没有文本搜索的过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!