Elasticsearch(15) multi_match的使用

elasticsearch version: 7.10.1

multi_match是Elasticsearch中的一种查询类型,允许在一个或多个字段上执行全文本搜索,并合并各个字段的结果得分。这种查询有助于实现跨多个字段的统一搜索体验。

语法

{
  "query": {
    "multi_match" : {
      "query":         "搜索文本",       // 要搜索的关键词或短语
      "fields":        ["field1", "field2"], // 搜索涉及的字段列表
      "type":          "best_fields",     // (可选)匹配类型,默认是"best_fields",也可以是"most_fields""cross_fields""phrase""phrase_prefix""operator":      "and",             // (可选)查询词之间的关系,默认是"or",可以改为"and"
      "analyzer":      "standard",        // (可选)使用的分析器
      "minimum_should_match": 1,           // (可选)最少匹配的条件,默认是基于类型的不同有不同的计算规则
      "tie_breaker":   0.3,              // (可选)交叉字段查询时,决定每个字段得分与整体得分之间关系的权重因子
      "boost":         2.0               // (可选)提升查询的权重,会影响相关度评分
    }
  }
}

query:要搜索的文本。
fields:一个字段名称列表,将在这些字段上执行全文搜索。
operator: and 搜索次需要全部匹配
type:定义匹配类型,不同类型的含义如下:

  • best_fields:默认类型,选取匹配分数最高的那个字段的得分。
  • most_fields:综合所有字段的得分。
  • cross_fields:将所有字段当作一个大的字段来处理,进行跨字段的短语查询。
  • phrase:强制执行短语匹配。
  • phrase_prefix:类似于phrase,但是支持前缀匹配。

例题

索引创建
PUT /blog_posts
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text"
      },
      "body": {
        "type": "text"
      },
      "tags": {
        "type": "keyword"
      }
    }
  }
}
文档插入
POST /blog_posts/_doc
{
  "title": "How to Use Elasticsearch for Full-text Search",
  "body": "In this article, we explore the power of Elasticsearch as a distributed full-text search engine.",
  "tags": ["Elasticsearch", "full-text search", "distributed systems"]
}

POST /blog_posts/_doc
{
  "title": "Distributed Systems Design with Elasticsearch",
  "body": "Learn about how Elasticsearch can be used in designing scalable and fault-tolerant distributed systems.",
  "tags": ["Elasticsearch", "distributed systems", "design patterns"]
}

POST /blog_posts/_doc
{
  "title": "Understanding Elasticsearch Aggregations",
  "body": "Discover the power of Elasticsearch aggregations for data analysis and exploration.",
  "tags": ["Elasticsearch", "aggregations", "data analytics"]
}

POST /blog_posts/_doc
{
  "title": "Scaling Elasticsearch Clusters for High Performance",
  "body": "Learn techniques for scaling Elasticsearch clusters to handle large volumes of data and high traffic loads.",
  "tags": ["Elasticsearch", "cluster scaling", "performance optimization"]
}

POST /blog_posts/_doc
{
  "title": "Introduction to Elasticsearch Relevance Scoring",
  "body": "Explore how Elasticsearch calculates relevance scores for search results to improve user experience.",
  "tags": ["Elasticsearch", "relevance scoring", "search algorithms"]
}

POST /blog_posts/_doc
{
  "title": "Real-time Log Analytics with Elasticsearch and Logstash",
  "body": "Configure Elasticsearch, Logstash, and Kibana (ELK Stack) for real-time log processing and visualization.",
  "tags": ["Elasticsearch", "Logstash", "Kibana", "log analytics"]
}

POST /blog_posts/_doc
{
  "title": "Securing Your Elasticsearch Cluster",
  "body": "Implement security measures to protect your Elasticsearch cluster from unauthorized access and data breaches.",
  "tags": ["Elasticsearch", "security", "access control"]
}

POST /blog-posts/_doc/
{
  "title": "How to use Elasticsearch Multi-Match Queries",
  "body": "In this article, we discuss various types of multi_match queries in Elasticsearch.",
  "tags": ["elasticsearch", "full-text search", "queries"]
}

POST /blog-posts/_doc/
{
  "title": "Exploring Phrase Matching in Elasticsearch",
  "body": "Learn how phrase matching works in Elasticsearch and its application in multi_match queries.",
  "tags": ["elasticsearch", "phrase matching"]
}
查询语句
GET /blog-posts/_search
{
  "query": {
    "multi_match" : {
      "query":      "use Elasticsearch multi-match queries",
      "fields":     ["title^3", "body"],
      "type":       "best_fields"
    }
  }
}
GET /blog-posts/_search
{
  "query": {
    "multi_match" : {
      "query":      "Elasticsearch multi-match",
      "fields":     ["title", "body", "tags.keyword"],
      "type":       "most_fields"
    }
  }
}
GET /blog-posts/_search
{
  "query": {
    "multi_match" : {
      "query":      "\"use Elasticsearch\" multi match",
      "fields":     ["title", "body"],
      "type":       "cross_fields"
    }
  }
}
GET /blog-posts/_search
{
  "query": {
    "multi_match" : {
      "query":      "\"phrase matching\" in Elasticsearch",
      "fields":     ["title", "body"],
      "type":       "phrase"
    }
  }
}
GET /blog-posts/_search
{
  "query": {
    "multi_match" : {
      "query":      "phrase matc*",
      "fields":     ["title", "body"],
      "type":       "phrase_prefix"
    }
  }
}

相关推荐

  1. Elasticsearch(15) multi_match使用

    2024-03-26 03:12:09       20 阅读
  2. Elasticsearch(11) intervals使用

    2024-03-26 03:12:09       22 阅读
  3. Elasticsearch(10) match使用

    2024-03-26 03:12:09       19 阅读
  4. Elasticsearch(13) match_phrase使用

    2024-03-26 03:12:09       24 阅读
  5. Elasticsearch(14) match_phrase_prefix使用

    2024-03-26 03:12:09       17 阅读
  6. Elasticsearch(12) match_bool_prefix使用

    2024-03-26 03:12:09       25 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-26 03:12:09       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-26 03:12:09       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-26 03:12:09       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-26 03:12:09       20 阅读

热门阅读

  1. 剑指offer面试题40 数组中只出现一次的数字

    2024-03-26 03:12:09       20 阅读
  2. anaconda配置虚拟python环境

    2024-03-26 03:12:09       21 阅读
  3. 速盾:免备案防攻击cdn

    2024-03-26 03:12:09       20 阅读
  4. Shell脚本总结-read-case语句

    2024-03-26 03:12:09       18 阅读
  5. 突破编程_C++_面试(STL 编程 queue)

    2024-03-26 03:12:09       18 阅读
  6. 数据结构-栈-004

    2024-03-26 03:12:09       18 阅读
  7. 鸿蒙 ohpm 的异常报错

    2024-03-26 03:12:09       18 阅读
  8. webpack的核心概念

    2024-03-26 03:12:09       18 阅读
  9. mysql 截取字符串及解析json

    2024-03-26 03:12:09       20 阅读