elasticsearch - 过滤 elasticsearch 字段(如果存在),否则忽略过滤器
问题描述
我正在尝试过滤两个不同的索引,但在同一个弹性查询中,
我想过滤两个索引上不存在的字段:
第一指数:
label: string,
value: string,
isShowable: boolean
第二个指数:
label: string,
value: string
我想从第二个索引中检索所有项目,并且只从第一个索引中获取可显示的项目。
我在 php 中使用 Elasticsearch DSL。
这是我尝试过的:
Item::search($formattedRequest, function (Client $client, Search $body) {
$dispensationQuery = new TermQuery("isShowable", true);
$bool = new BoolQuery();
$bool->add($dispensationQuery, BoolQuery::SHOULD);
$body->addQuery($bool);
return $client->search(
['index' => firstIndex . "," . secondIndex, 'body' => $body->toArray()],
);
})->get();
但它会过滤我的所有项目,并且不会从第二个指数中检索值。
如何管理?
解决方案
您可以使用 should 子句来组合存在和布尔检查。如果任一字段存在或其值为 true,则类似于返回文档
GET index1,index2/_search
{
"query": {
"bool": {
"should": [
{
"bool": {
"must_not": [
{
"exists": {
"field": "isShowable"
}
}
]
}
},
{
"term": {
"isShowable": {
"value": true
}
}
}
]
}
}
}
推荐阅读
- python - 如何在变量中管理 HTML 代码?
- php - 无法在 WordPress 的数组上使用 implode() 设置逗号分隔符
- postgresql - 连接到 postgresql 服务器时,如何在 psql 命令行中设置 rejectUnauthorized=false?
- mongodb - Mongo db updateOne 使用文档列表更新文档列表
- algorithm - 关于 O((log n)²) 与 O(log n) 的混淆
- excel - 将 Excel 图表复制到 Word 文档的末尾
- python - 为什么 scipy.linalg.LU 重复求解 Ax = b 这么慢?
- c - 函数如何在不使用 malloc 的情况下返回结构?
- javascript - 为什么我的 content.js 脚本不能与我的 background.js 脚本(chrome 扩展)通信?
- html - 尝试从 C 服务器发送 HTML 但浏览器有问题