elasticsearch - 如何重写弹性搜索的查询
问题描述
我有 SQL 查询
SELECT * FROM requests WHERE order_type = 'buyer' AND (plantations_id = 402 OR plantations_id = 460)
我对弹性的查询是
GET /requests/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"order_type": "buyer"
}
},
{
"bool": {
"should": [
{
"terms": {
"plantations_id": [402, 460]
}
}
]
}
}
]
}
}
}
但是结果集中只有“plantations_id”:“460”的项目。我有点困惑如何正确地重写我的原始查询。
提前致谢。
解决方案
您不需要该bool/should
子句,正确的查询是这个:
{
"query": {
"bool": {
"must": [
{
"match": {
"order_type": "buyer"
}
},
{
"terms": {
"plantations_id": [
402,
460
]
}
}
]
}
}
}
或者更好的是,将terms
过滤器移至bool/filter
,因为它不会参与评分:
{
"query": {
"bool": {
"must": [
{
"match": {
"order_type": "buyer"
}
}
],
"filter": [
{
"terms": {
"plantations_id": [
402,
460
]
}
}
]
}
}
}
推荐阅读
- r - mutate:在分组的df(dplyr)中选择除当前行之外的所有行
- python - 如何将光标放在 QTableWidget 单元格的开头
- python - 使用python从文件中导入损坏的矩阵
- c# - 在 C# 中初始化列表数组
- javascript - 为什么 JavaScript 将名为“name”的数组解析为字符串
- neo4j - Neo4J:如何为每个收集将属性增加 1
- r - 循环中的 R 降价 - plotly 和 dygraph 不起作用
- reactjs - 如何显示当前月份?
- java - 在杰克逊中序列化/反序列化别名原语而没有太多样板
- dll - 当提供程序仅附带 xxx.lib 并且您使用的是 MinGW-w64 而不是 MSVC 时,如何链接到 xxx.dll?