当前位置: 首页 > news >正文

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}}}]}}
}
http://www.lryc.cn/news/274607.html

相关文章:

  • 专业级的渗透测试服务,助力航空业数字化安全启航
  • 【docker】安装 Redis
  • pinia的独立维护,统一导出及持久化
  • 【AI视野·今日Robot 机器人论文速览 第六十七期】Mon, 1 Jan 2024
  • FBL刷写
  • OpenCV-Python(24):模板匹配
  • 机器学习--回归算法
  • 计算机网络——多址复用技术
  • stm32 HAL库 4096线ABZ编码器
  • Java爬虫之Jsoup
  • 如何在 C# 12 中使用主构造函数
  • Zookeeper之Java客户端实战
  • 将文本文件导入Oracle数据库的简便方法:SQL Loader Express
  • element-ui table-自定义表格某列的表头样式或者功能
  • Android 13.0 SystemUI状态栏居中显示时间和修改时间显示样式
  • 讲解eureca和nacus的区别
  • python中parsel模块的css解析
  • Oracle OCP怎么样线上考试呢
  • 如何让自己的写的程序在阿里云一直运行
  • 【计算机图形学】NAP: Neural 3D Articulation Prior
  • 知识付费平台搭建?找明理信息科技,专业且高效
  • CentOS7部署Kafka
  • JS的防抖和节流
  • 互联网加竞赛 基于人工智能的图像分类算法研究与实现 - 深度学习卷积神经网络图像分类
  • pip安装报错SSL
  • 手机视频监控客户端APP如何实现跨安卓、苹果和windows平台,并满足不同人的使用习惯
  • 从写下第1个脚本到年薪40W,我的自动化测试心路历程
  • Vue CLI组件通信
  • C语言编译器(C语言编程软件)完全攻略(第九部分:VS2019使用教程(使用VS2019编写C语言程序))
  • 走向云原生 破局数字化