我有一个关于Elasticsearch DSL的问题。
我想进行全文搜索,但是将可搜索的记录范围限定在特定的数据库id数组中。
在SQL世界中,它相当于WHERE id IN(1, 2, 3, 4)
。
我一直在研究,但我发现Elasticsearch查询DSL文档有点晦涩,缺乏有用的示例。有人能指导我吗?
我有一个关于Elasticsearch DSL的问题。
我想进行全文搜索,但是将可搜索的记录范围限定在特定的数据库id数组中。
在SQL世界中,它相当于WHERE id IN(1, 2, 3, 4)
。
我一直在研究,但我发现Elasticsearch查询DSL文档有点晦涩,缺乏有用的示例。有人能指导我吗?
{
"bool": {
"must": {
"match": {
"_all": "your search text"
}
},
"filter": {
"ids": {
"values": ["1","2","3","4"]
}
}
}
}
正如Ali Beyad所讨论的那样,查询中的ids字段可以为您完成此操作。为了补充他的答案,我提供一个工作示例。以防未来有人需要。
GET index_name/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"field": "your query"
}
},
{
"ids" : {
"values" : ["0aRM6ngBFlDmSSLpu_J4", "0qRM6ngBFlDmSSLpu_J4"]
}
}
]
}
}
}
你有两个选项。
GET index/_search
{
"query": {
"ids": {
"values": ["1, 2, 3"]
}
}
}
或者
GET index/_search
{
"query": {
"terms": {
"yourNonPrimaryIdField": ["1", "2","3"]
}
}
}
ids
查询是针对文档的内部 _id
字段(即主要 ID)的。但通常情况下,文档包含次要(及更多)ID,您可以通过terms
查询来定位它们。
请注意,如果您的次要 ID 包含大写字符并且您没有将其字段映射到 keyword
,则它们将被标准化(转换为小写),并且 terms 查询将显示错误,因为它仅适用于精确匹配。了解更多信息,请参阅:Elasticsearch 仅在区分大小写时返回结果
使用elasticaBundle symfony 5.2
$query = new Query();
$IdsQuery = new Query\Ids();
$IdsQuery->setIds($id);
$query->setQuery($IdsQuery);
$this->finder->find($query, $limit);