【问题标题】:How multi_match search in elastic on main object and nested array of objects?如何在主对象和嵌套对象数组的弹性中进行 multi_match 搜索?
【发布时间】:2021-06-11 04:05:33
【问题描述】:

我正在使用 elastic-search v7,并且我已经映射了如下所示的对象。 项其嵌套的对象数组。

我的问题是,当我尝试按 multi_match 项目字段搜索时,它没有像我预期的那样工作,结果为空。但是当我尝试使用查询和布尔值进行搜索时,它会找到我的文档。

我不正确理解那里有什么不同,我如何理解 query_search 用于过滤和聚合数据的精确匹配,以及 multi_match 用于全文搜索和自动完成,对吧?

以及如何在根字段和嵌套字段中查找文档?


{
  "orders" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "amazonOrderId" : {
          "type" : "keyword"
        },
        "carrierCode" : {
          "type" : "text"
        },
        "carrierName" : {
          "type" : "text"
        },
        "id" : {
          "type" : "keyword"
        },
        "items" : {
          "type" : "nested",
          "properties" : {
            "amazonItemId" : {
              "type" : "keyword"
            },
            "amazonPrice" : {
              "type" : "integer"
            },
            "amazonQuantity" : {
              "type" : "integer"
            },
            "amazonSku" : {
              "type" : "keyword"
            },
            "graingerItem" : {
              "type" : "nested"
            },
            "graingerOrderId" : {
              "type" : "keyword"
            },
            "graingerPrice" : {
              "type" : "integer"
            },
            "graingerShipDate" : {
              "type" : "date"
            },
            "graingerShipMethod" : {
              "type" : "short"
            },
            "graingerTrackingNumber" : {
              "type" : "keyword"
            },
            "graingerWebNumber" : {
              "type" : "keyword"
            },
            "id" : {
              "type" : "keyword"
            }
          }
        }
     }
   }
 }
}

多重匹配请求

GET orders/_search
{
  "query":{
    "multi_match" : {
      "query": "4.48 - 1 pack - 4.48",
      "fields": [
          "items.amazonSku",
          "carrierCode",
          "recipientName"
        ]
    }
  }
}

通过 _explain api 调试返回给我的描述


"explanation" : {
    "value" : 0.0,
    "description" : "Failure to meet condition(s) of required/prohibited clause(s)",
    "details" : [
      {
        "value" : 0.0,
        "description" : "no match on required clause (items.amazonSku:4.48 - 1 pack - 4.48)",
        "details" : [
          {
            "value" : 0.0,
            "description" : "no matching term",
            "details" : [ ]
          }
        ]
      },
      {
        "value" : 0.0,
        "description" : "match on required clause, product of:",
        "details" : [
          {
            "value" : 0.0,
            "description" : "# clause",
            "details" : [ ]
          },
          {
            "value" : 1.0,
            "description" : "DocValuesFieldExistsQuery [field=_primary_term]",
            "details" : [ ]
          }
        ]
      }
    ]
  }

查询搜索

GET orders/_search
{
  "query": {
    "nested": {
      "path": "items",
      "query": {
        "bool": {
          "must": [
            { "match": { "items.amazonSku": "4.48 - 1 pack - 4.48"}}
          ]
        }
      }
    }
  }
}

【问题讨论】:

    标签: elasticsearch elastic-stack


    【解决方案1】:

    由于您正在查询嵌套字段 items,因此您需要在查询中包含 nested 参数,以便它搜索嵌套字段对象

    将您的搜索修改为

    {
      "query": {
        "nested": {
          "path": "items",
          "query": {
            "multi_match": {
              "query": "4.48 - 1 pack - 4.48",
              "fields": [
                "items.amazonSku"
              ]
            }
          }
        }
      }
    }
    

    【讨论】:

    • 如果我理解的话,我们也可以在主对象的嵌套查询之前添加,如果我们需要搜索主对象和嵌套字段,对吗?
    • 是的@Drop,您可以结合嵌套和非嵌套查询。而且,如果它帮助您解决了您的问题,请不要忘记投票并接受答案:)
    • 它的范围很广,但它不适用于我赢得嵌套和根对象,当我添加嵌套映射 include_in_root 后它开始工作
    • @Drop 如果答案帮助您解决了问题,请不要忘记投票并接受答案?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-21
    • 1970-01-01
    • 1970-01-01
    • 2022-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多