过滤bool vs Bool查询:elasticsearch

时间:2016-08-03 13:03:09

标签: 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无法搜索文档。如果我删除第二个字段条件,它会搜索相同的记录,其中field2的值为401。

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

UPDATE2:

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

{
  "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 个答案:

答案 0 :(得分:3)

第一个使用旧的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
      }
    }
  ]
}