elasticsearch - 过滤 bool vs Bool 查询 : elasticsearch

标签 elasticsearch querydsl

我在 ES 中有两个查询。两者在同一组文档上的周转时间不同。两者在概念上都在做同样的事情。我几乎没有怀疑

1- 这两者有什么区别? 2- 哪个更好用? 3- 如果两者相同,为什么它们的表现不同?

 1. Filtered bool
    {
      "from": 0,
      "size": 5,
      "query": {
        "filtered": {
          "filter": {
            "bool": {
              "must": [
                {
                  "term": {
                    "called_party_address_number": "1987112602"
                  }
                },
                {
                  "term": {
                    "original_sender_address_number": "6870340319"
                  }
                },
                {
                  "range": {
                    "x_event_timestamp": {
                      "gte": "2016-07-01T00:00:00.000Z",
                      "lte": "2016-07-30T00:00:00.000Z"
                    }
                  }
                }
              ]
            }
          }
        }
      },
      "sort": [
        {
          "x_event_timestamp": {
            "order": "desc",
            "ignore_unmapped": true
          }
        }
      ]
    }

    2. Simple Bool

    {
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "called_party_address_number": "1277478699"
              }
            },
            {
              "term": {
                "original_sender_address_number": "8020564722"
              }
            },
            {
              "term": {
                "cause_code": "573"
              }
            },
            {
              "range": {
                "x_event_timestamp": {
                  "gt": "2016-07-13T13:51:03.749Z",
                  "lt": "2016-07-16T13:51:03.749Z"
                }
              }
            }
          ]
        }
      },
      "from": 0,
      "size": 10,
      "sort": [
        {
          "x_event_timestamp": {
            "order": "desc",
            "ignore_unmapped": true
          }
        }
      ]
    }

映射:

{
   "ccp": {
      "mappings": {
         "type1": {
            "properties": {
               "original_sender_address_number": {
                  "type": "string"
               },
               "called_party_address_number": {
                  "type": "string"
               },
               "cause_code": {
                  "type": "string"
               },               
               "x_event_timestamp": {
                   "type": "date",
                  "format": "strict_date_optional_time||epoch_millis"
               },
               .
               .
               .              
            }
         }
      }
   }
}

更新 1:

我在同一组数据上尝试了 bool/must 查询和 bool/filter 查询,但我发现了奇怪的行为

1- bool/must 查询是否能够搜索到想要的文档

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "called_party_address_number": "8701662243"
          }
        },
        {
          "term": {
            "cause_code": "401"
          }
        }
      ]
    }
  }
}

2- 虽然 bool/filter 无法搜索文档。如果我删除第二个字段条件,它会搜索字段 2 的值为 401 的相同记录。

{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "called_party_address_number": "8701662243"
          }
        },
        {
          "term": {
            "cause_code": "401"
          }
        }
      ]
    }
  }
}

更新2:

通过将 bool/must 查询包装在 "constant_score" 中找到了抑制评分阶段的解决方案。

{
  "query": {
    "constant_score": {
      "filter": {
        "bool": {
          "must": [
            {
              "term": {
                "called_party_address_number": "1235235757"
              }
            },
            {
              "term": {
                "cause_code": "304"
              }
            }
          ]
        }
      }
    }
  }
}

我们尝试匹配的记录有“called_pa​​rty_address_number”:“1235235757”和“cause_code”:“304”。

最佳答案

第一个使用旧的 1.x 查询/过滤器语法(即 filtered 查询已被弃用,取而代之的是 bool/filter )。

第二个使用新的 2.x 语法但不在过滤器上下文中(即您使用的是 bool/must 而不是 bool/filter)。使用 2.x 语法的查询相当于您的第一个查询(即在没有分数计算的过滤器上下文中运行 = 更快)将是这个:

{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "called_party_address_number": "1277478699"
          }
        },
        {
          "term": {
            "original_sender_address_number": "8020564722"
          }
        },
        {
          "term": {
            "cause_code": "573"
          }
        },
        {
          "range": {
            "x_event_timestamp": {
              "gt": "2016-07-13T13:51:03.749Z",
              "lt": "2016-07-16T13:51:03.749Z"
            }
          }
        }
      ]
    }
  },
  "from": 0,
  "size": 10,
  "sort": [
    {
      "x_event_timestamp": {
        "order": "desc",
        "ignore_unmapped": true
      }
    }
  ]
}

关于elasticsearch - 过滤 bool vs Bool 查询 : elasticsearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38744489/

相关文章:

hibernate - 在Querydsl中选择Long类型变量

elasticsearch - 当不是空字符串时,带有字段的Elasticsearch query_string过滤器

elasticsearch - 使用其他搜索对弹性结果进行后处理(从Solr迁移)

elasticsearch - 为什么Elasticsearch的字段数限制为1000

java - Java连接池关闭

java - QueryDsl SQL - 左连接子查询

sql - 如何在QueryDsl中实现左外连接或右外连接

querydsl - 在 Querydsl 中创建一个 where 子句以检查时间戳列

elasticsearch - 未能将映射放在索引上导致elasticsearch崩溃[5.4.1]

python-3.x - 如何使用elasticsearch-dsl-py连接两个ElasticSearch索引?