我在 Laravel 应用程序中使用 ES,我需要执行一个仅包含过滤器而没有“文本搜索”的搜索查询,但我不确定如何编写它。
我必须使用 match_all 例如:
$query = [
'filtered' => [
'query' => [
'match_all' => []
],
'filter'=> [
'bool' => [
'must' => [
[ 'range' => [
'price' => [
'lte' => 9000
]
]
],
],
]
],
],
];
或者像这样:
$query = [
'filtered' => [
'filter'=> [
'bool' => [
'must' => [
[ 'range' => [
'price' => [
'lte' => 9000
]
]
],
],
]
],
],
];
我想要的是仅使用过滤的 bool 查询而不使用文本搜索。
最佳答案
事实上,如果您未在过滤查询中指定 query
部分,则默认使用 match_all 查询。引用 doc :
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.
您的第二个查询应该完成这项工作:过滤器必须包装在要使用的 filtered
( doc ) 或 constant_score
( doc ) 查询中。
如果评分部分对您没有用,您可以坚持使用过滤
查询。
最后一件事:您不必将过滤器嵌套在 bool
过滤器中,除非您想将其与其他过滤器组合。在您的演示案例中,您可以直接编写:
$query = [
'filtered' => [
'filter'=> [
'range' => [
'price' => [
'lte' => 9000
]
]
]
]
];
希望这会有所帮助:)
关于php - 在elasticsearch中是否可以只使用过滤器而不使用文本搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28989479/