Elasticsearch(10) match的使用

本文详细介绍了Elasticsearch7.10.1中ElasticMatch查询语法,包括match查询结构、查询词关系控制、模糊匹配和权重设置。通过示例展示了如何在实际场景中使用match查询进行精确和模糊搜索。
摘要由CSDN通过智能技术生成

elasticsearch version: 7.10.1
Elastic Match是Elasticsearch中用于全文搜索的一种查询语法。它可以将搜索词进行分词,并与目标查询字段进行匹配。

match的语法

GET /<index>/_search
{
  "query": {
    "match": {
      "<field_name>": {
        "query": "<your_search_text>",
        // 可选参数
        "operator": "and|or", // 默认为 or,控制多个词语之间的关系
        "analyzer": "<analyzer_name>", // 指定分析器
        "boost": <boost_value>, // 权重提升
        "fuzziness": "<fuzziness_setting>", // 模糊匹配设置
        // 其他参数,如 prefix_length、max_expansions 等
      }
    }
  }
}
  • operator: 控制如何处理查询词之间的关系,可以是and或or,默认为or
  • fuzziness: 允许查询词与索引中的词有一定程度的差异,支持模糊匹配。
  • boost: 提高匹配此查询的文档的评分,从而影响文档在搜索结果中的排序。

案列

场景1

假设我们有一个产品索引,其中包含商品描述字段 description,现在我们想找到描述中同时包含 “leather jacket” 和 “winter wear” 的商品。

索引创建

PUT /products
{
  "mappings": {
    "properties": {
      "description": {
        "type": "text"
      },
      "title": {
        "type": "text"
      }
    }
  }
}

索引插入

POST /products/_doc
{
  "title": "Leather Jacket for Winter Wear",
  "description": "High-quality leather jacket designed for winter wear. Made with genuine leather and lined with warm fabric."
}

POST /products/_doc
{
  "title": "Summer Denim Jacket",
  "description": "Lightweight denim jacket perfect for summer days."
}

POST /products/_doc
{
  "title": "Winter Wool Coat",
  "description": "Warm wool coat suitable for cold winter months."
}

POST /products/_doc
{
  "title": "Leather Gloves",
  "description": "Soft leather gloves for both men and women, ideal for chilly weather."
}

POST /products/_doc
{
  "title": "Summer Jacket with UV Protection",
  "description": "Lightweight windbreaker jacket featuring built-in UV protection for sunny days outdoors."
}

POST /products/_doc
{
  "title": "Winter Boots with Fur Lining",
  "description": "Waterproof boots lined with cozy fur, providing excellent insulation against harsh winter conditions."
}

POST /products/_doc
{
  "title": "Leather Sneakers for Casual Wear",
  "description": "Stylish leather sneakers suitable for everyday casual wear, made from premium materials."
}

POST /products/_doc
{
  "title": "Wool Scarf with Leather Trim",
  "description": "Luxurious wool scarf featuring a leather trim accent, adding a touch of sophistication to your winter ensemble."
}

查询

GET /products/_search
{
  "query": {
    "match": {
      "description": {
        "query": "leather jacket winter wear",
        "operator": "and"
      }
    }
  }
}

场景2

同样在上述的商品索引中,我们想找到描述中包含 “leather” 或 “jacket” 的商品。

查询语句

GET /products/_search
{
  "query": {
    "match": {
      "description": {
        "query": "leather jacket",
        "operator": "or"
      }
    }
  }
}

分词

指定分词器

PUT my_index
{
  "mappings": {
    "properties": {
      "message": {
        "type": "text",
        "analyzer": "standard"
      }
    }
  }
}

查询分词

POST my_index/_doc
{
  "message": "Please don't split words with hyphens like co-operate or well-known"
}

  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值