ElasticSearch Nested类型全文检索、聚合查询

ElasticSearch Nested类型全文检索、聚合查询

Nested类型全文检索

  1. 创建索引
PUT /products1
{
   
  "mappings": {
   
    "properties": {
   
      "fulltext": {
   
          "type": "text"
        },
      "name": {
   
        "type": "text",
        "fields": {
   
          "keyword": {
   
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "reviews": {
   
        "type": "nested",
        "properties": {
   
          "rating": {
   
            "type": "integer"
          },
          "author": {
   
            "type": "text",
            "copy_to": "fulltext"
          },
          "date": {
   
            "type": "date"
          }
        }
      }
    }
  }
}

以上创建索引语句中实现全文检索重点为"fulltext": { "type": "text" }"copy_to": "fulltext",nested类型中哪个text类型的字段需要全文检索,就在字段上加"copy_to": "fulltext"

  1. 添加数据
PUT /products1/_doc/1
{
   
  "name": "Product A",
  "reviews": [
    {
   
      "rating": 5,
      "author": "Alice",
      "date": "2021-01-01"
    },
    {
   
      "rating": 4,
      "author": "Bob",
      "date": "2021-01-02"
    }
  ]
}

PUT /products1/_doc/2
{
   
  "name": "Product B",
  "reviews": [
    {
   
      "rating": 1,
      "author": "John",
      "date": "2021-01-03"
    },
    {
   
      "rating": 2,
      "author": "Mary",
      "date": "2021-01-04"
    },
    {
   
      "rating": 3,
      "author": "James",
      "date": "2021-01-05"
    },
    {
   
      "rating": 4,
      "author": "Elisabeth",
      "date": "2021-01-06"
    },
    {
   
      "rating": 5,
      "author": "Richard",
      "date": "2021-01-07"
    }
  ]
}


PUT /products1/_doc/3
{
   
  "name": "Product C",
  "reviews": [
    {
   
      "rating": 1,
      "author": "Alex",
      "date": "2021-01-03"
    },
    {
   
      "rating": 2,
      "author": "Alice",
      "date": "2021-01-04"
    }
  ]
}
  1. 执行查询
POST products1/_search
{
   
  "query": {
   
    "simple_query_string": {
   
      "query": "Alice"
    }
  }
}
  1. 结果如下,可以看到nested类型中包含Alice的数据也被检索出来了
{
   
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
   
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
   
    "total" : {
   
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.5442147,
    "hits" : [
      {
   
        "_index" : "products1",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.5442147,
        "_source" : {
   
          "name" : "Product A",
          "reviews" : [
            {
   
              "rating" : 5,
              "author" : "Alice",
              "date" : "2021-01-01"
            },
            {
   
              "rating" : 4,
              "author" : "Bob",
              "date" : "2021-01-02"
            }
          ]
        }
      },
      {
   
        "_index" : "products1",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.5442147,
        "_source" : {
   
          "name" : "Product C",
          "reviews" : [
            {
   
              "rating" : 1,
              "author" : "Alex",
              "date" : "2021-01-03"
            },
            {
   
              "rating" : 2,
              "author" : "Alice",
              "date" : "2021-01-04"
            }
          ]
        }
      }
    ]
  }
}

以上可以看到实现nested类型全文检索

nested类型聚合查询

还是在上面product1索引中测试

  1. 现在,您可以对嵌套文档执行嵌套聚合。例如,让我们计算每个产品的平均评分:
GET /products1/_search
{
   
  "size": 0,
  "aggs": {
   
    "聚合名称": {
   
      "terms": {
   
        "field": "name.keyword"
      },
      "aggs": {
   
        "reviews": {
   
          "nested": {
   
            "path": "reviews"
          },
          "aggs": {
   
            "average_rating": {
   
              "avg": {
   
                "field": "reviews.rating"
              }
            }
          }
        }
      }
    }
  }
}
  1. 我们首先使用术语聚合为每个产品创建存储桶。然后,对于每个产品,我们运行嵌套聚合,以便我们可以访问嵌套文档的集合。最后,我们可以计算这些嵌套文档的指标聚合,在我们的示例中是平均评分
{
   
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
   
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
   
    "total" : {
   
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
   
    "products" : {
   
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
   
          "key" : "Product A",
          "doc_count" : 1,
          "reviews" : {
   
            "doc_count" : 2,
            "average_rating" : {
   
              "value" : 4.5
            }
          }
        },
        {
   
          "key" : "Product B",
          "doc_count" : 1,
          "reviews" : {
   
            "doc_count" : 5,
            "average_rating" : {
   
              "value" : 3.0
            }
          }
        },
        {
   
          "key" : "Product C",
          "doc_count" : 1,
          "reviews" : {
   
            "doc_count" : 2,
            "average_rating" : {
   
              "value" : 1.5
            }
          }
        }
      ]
    }
  }
}

相关推荐

  1. ElasticSearch Nested类型全文检索聚合查询

    2023-12-31 22:22:02       56 阅读
  2. ElasticSearch聚合查询

    2023-12-31 22:22:02       35 阅读
  3. 【Django】聚合查询

    2023-12-31 22:22:02       42 阅读
  4. Django-聚合查询

    2023-12-31 22:22:02       39 阅读
  5. ES聚合查询

    2023-12-31 22:22:02       38 阅读
  6. 【Django】聚合查询——连接和聚合

    2023-12-31 22:22:02       48 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-31 22:22:02       91 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-31 22:22:02       97 阅读
  3. 在Django里面运行非项目文件

    2023-12-31 22:22:02       78 阅读
  4. Python语言-面向对象

    2023-12-31 22:22:02       88 阅读

热门阅读

  1. 9. 进程

    9. 进程

    2023-12-31 22:22:02      42 阅读
  2. 使用axios发送get和post请求

    2023-12-31 22:22:02       64 阅读
  3. STL容器——map

    2023-12-31 22:22:02       58 阅读
  4. python非常好用的文件系统监控库

    2023-12-31 22:22:02       65 阅读
  5. C语言概述

    2023-12-31 22:22:02       49 阅读
  6. 实用的执行sql,持续更新

    2023-12-31 22:22:02       59 阅读
  7. 毕业设计之开题报告

    2023-12-31 22:22:02       56 阅读
  8. Vue生命周期详解

    2023-12-31 22:22:02       53 阅读