【问题标题】:How to combine bool query with interval query or any other query如何将布尔查询与间隔查询或任何其他查询结合使用
【发布时间】:2020-03-20 09:35:56
【问题描述】:

我已将数据索引到 elasticsearch

我想查询Boolean queryInterval query 的组合。

如果可以像这样组合两个查询,那么请告诉我 _score 是否会是两者的组合。

我正在尝试进行这样的查询:

GET trademark28/_search?explain=true
{
  "size": 100, 
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "tm_applied_for_anan": {
              "query": "7 o' clock",
              "boost": 1
            }
          }
        }
      ]
    }, 
    "intervals" : {
      "tm_applied_for_only_char_without_space" : {
        "all_of" : {
          "intervals" : [
            {
              "match" : {
                "query" : "7 o' clock",
                "max_gaps" : 0
              }
            }
          ]
        }
      }
    }

  }
}

但是这个查询抛出错误。

{
  "error" : {
    "root_cause" : [
      {
        "type" : "parsing_exception",
        "reason" : "[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
        "line" : 16,
        "col" : 5
      }
    ],
    "type" : "parsing_exception",
    "reason" : "[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
    "line" : 16,
    "col" : 5
  },
  "status" : 400
}

但是当我单独运行以下查询时。它们运行得很好。

GET trademark28/_search?explain=true
{
  "size": 100, 
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "tm_applied_for_anan": {
              "query": "7 o' clock",
              "boost": 1
            }
          }
        }
      ]
    }
  }
}

GET trademark28/_search?explain=true
{
  "size": 100, 
  "query": {
    "intervals" : {
      "tm_applied_for_only_char_without_space" : {
        "all_of" : {
          "intervals" : [
            {
              "match" : {
                "query" : "7 o' clock",
                "max_gaps" : 0
              }
            }
          ]
        }
      }
    }

  }
}

有没有办法可以像这样组合两种类型的查询?

【问题讨论】:

    标签: elasticsearch data-science elastic-stack


    【解决方案1】:

    可以使用 must 子句 (AND) 或 should 子句 (OR) 组合这两个查询。 总分基于执行的所有查询。使用 ?explain=true 显示两个查询的得分总和。

    GET <index_name>/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "tm_applied_for_anan": {
                  "query": "7 o' clock",
                  "boost": 1
                }
              }
            },
            {
              "intervals": {
                "tm_applied_for_only_char_without_space": {
                  "all_of": {
                    "intervals": [
                      {
                        "match": {
                          "query": "7 o' clock",
                          "max_gaps": 0
                        }
                      }
                    ]
                  }
                }
              }
            }
          ]
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-10
      • 2020-02-14
      • 2019-08-11
      • 1970-01-01
      • 1970-01-01
      • 2022-01-14
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多