模糊查询不适用于文本类型,但适用于关键字类型

时间:2020-01-23 09:31:52

标签: elasticsearch fuzzy-search

我有一个仅适用于关键字类型的查询,我不知道为什么。

但是,如果我使用匹配查询和模糊参数,则可以使其与文本类型一起使用。

为什么会这样?

请参见下面的查询

(正在运行的查询应返回Eddie的文档。)

1)模糊查询文本类型->不起作用

GET kibana_sample_data_ecommerce/_search
{
  "query": {
    "fuzzy": {
      "customer_first_name": {
        "value": "Eddi",
        "fuzziness": "AUTO"
      }
    }
  }
}

2)模糊查询关键字类型-工作

GET kibana_sample_data_ecommerce/_search
{
  "query": {
    "fuzzy": {
      "customer_first_name.keyword": {
        "value": "Eddi",
        "fuzziness": "AUTO"
      }
    }
  }
}

3)匹配查询+ FUZINESS->工作

GET kibana_sample_data_ecommerce/_search
{
  "query": {
    "match": {
      "customer_first_name.keyword": {
        "query": "Eddi",
        "fuzziness": "Auto"
      }
    }
  }
}

索引设置

{
      "kibana_sample_data_ecommerce" : {
        "aliases" : { },
        "mappings" : {
          "properties" : {
            "category" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword"
                }
              }
            },
            "currency" : {
              "type" : "keyword"
            },
            "customer_birth_date" : {
              "type" : "date"
            },
            "customer_first_name" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "customer_full_name" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "customer_gender" : {
              "type" : "keyword"
            },
            "customer_id" : {
              "type" : "keyword"
            },
            "customer_last_name" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "customer_phone" : {
              "type" : "keyword"
            },
            "day_of_week" : {
              "type" : "keyword"
            },
            "day_of_week_i" : {
              "type" : "integer"
            },
            "email" : {
              "type" : "keyword"
            },
            "geoip" : {
              "properties" : {
                "city_name" : {
                  "type" : "keyword"
                },
                "continent_name" : {
                  "type" : "keyword"
                },
                "country_iso_code" : {
                  "type" : "keyword"
                },
                "location" : {
                  "type" : "geo_point"
                },
                "region_name" : {
                  "type" : "keyword"
                }
              }
            },
            "manufacturer" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword"
                }
              }
            },
            "order_date" : {
              "type" : "date"
            },
            "order_id" : {
              "type" : "keyword"
            },
            "products" : {
              "properties" : {
                "_id" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  }
                },
                "base_price" : {
                  "type" : "half_float"
                },
                "base_unit_price" : {
                  "type" : "half_float"
                },
                "category" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword"
                    }
                  }
                },
                "created_on" : {
                  "type" : "date"
                },
                "discount_amount" : {
                  "type" : "half_float"
                },
                "discount_percentage" : {
                  "type" : "half_float"
                },
                "manufacturer" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword"
                    }
                  }
                },
                "min_price" : {
                  "type" : "half_float"
                },
                "price" : {
                  "type" : "half_float"
                },
                "product_id" : {
                  "type" : "long"
                },
                "product_name" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword"
                    }
                  },
                  "analyzer" : "english"
                },
                "quantity" : {
                  "type" : "integer"
                },
                "sku" : {
                  "type" : "keyword"
                },
                "tax_amount" : {
                  "type" : "half_float"
                },
                "taxful_price" : {
                  "type" : "half_float"
                },
                "taxless_price" : {
                  "type" : "half_float"
                },
                "unit_discount_amount" : {
                  "type" : "half_float"
                }
              }
            },
            "sku" : {
              "type" : "keyword"
            },
            "taxful_total_price" : {
              "type" : "half_float"
            },
            "taxless_total_price" : {
              "type" : "half_float"
            },
            "total_quantity" : {
              "type" : "integer"
            },
            "total_unique_products" : {
              "type" : "integer"
            },
            "type" : {
              "type" : "keyword"
            },
            "user" : {
              "type" : "keyword"
            }
          }
        },
        "settings" : {
          "index" : {
            "number_of_shards" : "1",
            "auto_expand_replicas" : "0-1",
            "provided_name" : "kibana_sample_data_ecommerce",
            "creation_date" : "1579684918696",
            "number_of_replicas" : "0",
            "uuid" : "Ga3UfyyAQjGpa5JDbJB7Sw",
            "version" : {
              "created" : "7050299"
            }
          }
        }
      }
    }

1 个答案:

答案 0 :(得分:0)

模糊查询是术语级别查询。这意味着查询不会被分析。

为什么查询1)失败

如果您查询“ Eddi”,它将与分析文本进行比较,在这种情况下为“ eddie”。

从“埃迪”到“埃迪”,它是2个编辑距离。

因此,查询不会成功,因为长度在3到5之间的词的最大编辑距离是1(使用“模糊性:自动”配置)

为什么查询2)成功

另一方面,如果您使用关键字,则关键字将被存储而不进行分析。因此。埃迪(Eddi)与埃迪(Eddie)的编辑距离为1。

相关问题