elasticsearch 权威指南入门阅读笔记(一)

时间:2022-08-28 08:30:32

相关文档

esapi:https://es.xiaoleilu.com/010_Intro/10_Installing_ES.html     https://esdoc.bbossgroups.com/#/pinyin-ik

es中文社区:https://elasticsearch.cn/question/6899

比较全的api http://cwiki.apachecn.org/pages/viewpage.action?pageId=4260605

什么是elasticsearch

elasticsearch是java基础apache lucene的开源搜索引擎,开箱即用,elasticsearch有很多默认的合理的配置

elasticsearch作用

全文检索,分布式实时文件存储,分布式实时分析引擎,可以扩展到上百台服务器处理PB级结构化数据或非结构化数据

elasticsearch安装

官网下载:https://www.elastic.co/downloads/elasticsearch解压

linux运行:./bin/elasticsearch

访问:http://localhost:9200/?pretty

elasticsearch 权威指南入门阅读笔记(一)

表示安装成功

ES与传统数据的区别

关系型数据库:databases->tables->rows->columns

ES:indices->types->documents->Fileds

index可以理解成数据库  type可以理解为表  doument可以理解为行  filed可以理解为列

索引名词=数据库  索引动词可以理解为数据库的insert语句  索引文档如果存在 旧的将覆盖新的  ES为字段建立的索引为倒排索引

建立索引

现在我们建立1个产品的索引(insert)

posman put请求

http://127.0.0.1:9200/opcmdev/product/1

body:

{
"productName": "法瑞思 天然椰棕床垫棕垫硬床垫定做 薄乳胶棕榈榻榻米床垫定制折叠1.2/1.5/1.8",
"price": 10,
"remark": "不错的床垫",
"tags": ["家具", "床垫", "棉花"]
}

elasticsearch 权威指南入门阅读笔记(一)

我们可以理解为在opcmdev数据库 product表 插入了一条产品信息  方便后面测试 多添加几条数据

http://127.0.0.1:9200/opcmdev/product/2

{
"productName": "南极人(Nanjiren)床垫家纺 加厚立体床垫羽丝绒床垫床褥子学生宿舍",
"price": 109,
"remark": "不错的床垫",
"tags": ["家具", "床垫", "棉花被"]
}

http://127.0.0.1:9200/opcmdev/product/3

{
"productName": "¥99.00志高(CHIGO)电火锅家用多功能电炒锅电煮锅不粘多用途锅电热锅 ZG-RH2",
"price": 99,
"remark": "平底锅",
"tags": ["电器", "平底锅"]
}

简单的检索文档(查询)

根据id查询

get请求:http://127.0.0.1:9200/opcmdev/product/3

elasticsearch 权威指南入门阅读笔记(一)

不带条件的简单搜索 

get请求:http://127.0.0.1:9200/opcmdev/product/_search 默认返回前10条

{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "opcmdev",
"_type": "product",
"_id": "2",
"_score": 1,
"_source": {
"productName": "南极人(Nanjiren)床垫家纺 加厚立体床垫羽丝绒床垫床褥子学生宿舍",
"price": "109",
"remark": "不错的床垫",
"tags": [
"家具",
"床垫",
"棉花被"
]
}
},
{
"_index": "opcmdev",
"_type": "product",
"_id": "1",
"_score": 1,
"_source": {
"productName": "法瑞思 天然椰棕床垫棕垫硬床垫定做 薄乳胶棕榈榻榻米床垫定制折叠1.2/1.5/1.8",
"price": "10",
"remark": "不错的床垫",
"tags": [
"家具",
"床垫",
"棉花被"
]
}
},
{
"_index": "opcmdev",
"_type": "product",
"_id": "3",
"_score": 1,
"_source": {
"productName": "¥99.00志高(CHIGO)电火锅家用多功能电炒锅电煮锅不粘多用途锅电热锅 ZG-RH2",
"price": "99",
"remark": "平底锅",
"tags": [
"电器",
"平底锅"
]
}
}
]
}
}

通过url?q模糊搜索

http://127.0.0.1:9200/opcmdev/product/_search?q=productName:锅

elasticsearch 权威指南入门阅读笔记(一)

DSL查询语句

查询条件分词查询

 doman spcific language 能够完整更强大的查询功能
默认情况下会将相识度最高的 排在前面 比如搜索平底锅  那么平底锅会排在前面 而其他锅则排在后面
get请求:http://127.0.0.1:9200/opcmdev/product/_search
body:
{
"query":{
"match":{
"productName":"丝绒床垫"
}
}
}

结果

{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1.5142827,
"hits": [
{
"_index": "opcmdev",
"_type": "product",
"_id": "2",
"_score": 1.5142827,
"_source": {
"productName": "南极人(Nanjiren)床垫家纺 加厚立体床垫羽丝绒床垫床褥子学生宿舍",
"price": 109,
"remark": "不错的床垫",
"tags": [
"家具",
"床垫",
"棉花被"
]
}
},
{
"_index": "opcmdev",
"_type": "product",
"_id": "1",
"_score": 0.9389185,
"_source": {
"productName": "法瑞思 天然椰棕床垫棕垫硬床垫定做 薄乳胶棕榈榻榻米床垫定制折叠1.2/1.5/1.8",
"price": 10,
"remark": "不错的床垫",
"tags": [
"家具",
"床垫",
"棉花"
]
}
}
]
}
}

查询条件不分词查询

只需要将条件修改为

{
"query":{
"match_phrase":{
"productName":"丝绒床垫"
}
}
}

结果

{
"took": 7,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1.1507283,
"hits": [
{
"_index": "opcmdev",
"_type": "product",
"_id": "2",
"_score": 1.1507283,
"_source": {
"productName": "南极人(Nanjiren)床垫家纺 加厚立体床垫羽丝绒床垫床褥子学生宿舍",
"price": 109,
"remark": "不错的床垫",
"tags": [
"家具",
"床垫",
"棉花被"
]
}
}
]
}
}

范围查询

需求:查询价格大于100的产品

 get请求:http://127.0.0.1:9200/opcmdev/product/_search
5.*之前版本
{
"query" : {
"filtered" : {
"filter" : {
"range" : {
"price" : { "gt" : 30 }
}
},
"query" : {
"match" : {
"productName" : "锅"
}
}
}
}
}

5.*之后版本

{
"query" : {
"bool" : {
"filter" : {
"range" : {
"price" : { "gt" : 100 }
}
},
"must" : {
"match" : {
"productName" : "锅"
}
}
}
}
}

结果

elasticsearch 权威指南入门阅读笔记(一)

为什么结果不对呢

请求:http://127.0.0.1:9200/opcmdev/product/_mapping?pretty

结果:

{
"opcmdev": {
"mappings": {
"product": {
"properties": {
"price": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"productName": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"proudctName": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"remark": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"tags": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}

因为创建索引没有指定mapping(数据类型)默认是text导致无效  但是又不能修改数据类型参考

mapping创建参考:

https://blog.csdn.net/napoay/article/details/52012249

如果正式遇到这种情况 需要修改类型 参考

http://www.cnblogs.com/Creator/p/3722408.html

高亮搜索

get请求:http://127.0.0.1:9200/opcmdev/product/_search

参数

{
"query": {
"match_phrase": {
"productName": "床垫"
}
},
"highlight": {
"fields": {
"productName": {}
}
}
}

结果

{
"took": 19,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.90414375,
"hits": [
{
"_index": "opcmdev",
"_type": "product",
"_id": "2",
"_score": 0.90414375,
"_source": {
"productName": "南极人(Nanjiren)床垫家纺 加厚立体床垫羽丝绒床垫床褥子学生宿舍",
"price": 109,
"remark": "不错的床垫",
"tags": [
"家具",
"床垫",
"棉花被"
]
},
"highlight": {
"productName": [
"南极人(Nanjiren)<em>床</em><em>垫</em>家纺 加厚立体<em>床</em><em>垫</em>羽丝绒<em>床</em><em>垫</em>床褥子学生宿舍"
]
}
},
{
"_index": "opcmdev",
"_type": "product",
"_id": "1",
"_score": 0.90414375,
"_source": {
"productName": "法瑞思 天然椰棕床垫棕垫硬床垫定做 薄乳胶棕榈榻榻米床垫定制折叠1.2/1.5/1.8",
"price": 10,
"remark": "不错的床垫",
"tags": [
"家具",
"床垫",
"棉花"
]
},
"highlight": {
"productName": [
"法瑞思 天然椰棕<em>床</em><em>垫</em>棕垫硬<em>床</em><em>垫</em>定做 薄乳胶棕榈榻榻米<em>床</em><em>垫</em>定制折叠1.2/1.5/1.8"
]
}
}
]
}
}

前端显示我们给em这是红色 就行了

分析

aggregations聚合
类似sql的goup by
5.0之前版本
{
"aggs":{
"all_tags":{
"terms":{
"field":"tags"
}
}
}
}

5.0之后版本会报错

Fielddata is disabled on text fields by default. Set fielddata=true 

因为5.x以后聚合这些操作用单独的数据结构(fielddata)缓存到内存里了 会占用大量堆空间 对性能有影响 默认没有开启

put请求:http://127.0.0.1:9200/opcmdev/_mapping/product/?pretty

结果:

{
"acknowledged": true
}

然后我们就可以得到聚合结果

{
"took": 44,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "opcmdev",
"_type": "product",
"_id": "2",
"_score": 1,
"_source": {
"productName": "南极人(Nanjiren)床垫家纺 加厚立体床垫羽丝绒床垫床褥子学生宿舍",
"price": 109,
"remark": "不错的床垫",
"tags": [
"家具",
"床垫",
"棉花被"
]
}
},
{
"_index": "opcmdev",
"_type": "product",
"_id": "1",
"_score": 1,
"_source": {
"productName": "法瑞思 天然椰棕床垫棕垫硬床垫定做 薄乳胶棕榈榻榻米床垫定制折叠1.2/1.5/1.8",
"price": 10,
"remark": "不错的床垫",
"tags": [
"家具",
"床垫",
"棉花"
]
}
},
{
"_index": "opcmdev",
"_type": "product",
"_id": "3",
"_score": 1,
"_source": {
"productName": "¥99.00志高(CHIGO)电火锅家用多功能电炒锅电煮锅不粘多用途锅电热锅 ZG-RH2",
"price": 99,
"remark": "平底锅",
"tags": [
"电器",
"平底锅"
]
}
}
]
},
"aggregations": {
"all_tags": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 2,
"buckets": [
{
"key": "具",
"doc_count": 2
},
{
"key": "垫",
"doc_count": 2
},
{
"key": "家",
"doc_count": 2
},
{
"key": "床",
"doc_count": 2
},
{
"key": "棉",
"doc_count": 2
},
{
"key": "花",
"doc_count": 2
},
{
"key": "器",
"doc_count": 1
},
{
"key": "平",
"doc_count": 1
},
{
"key": "底",
"doc_count": 1
},
{
"key": "电",
"doc_count": 1
}
]
}
}
}

可以看到  聚合的字段被分词了 不需要分词需要给参数加上keyword

{
"aggs":{
"all_tags":{
"terms":{
"field":"tags.keyword"
}
}
}
}

结果

{
"took": 10,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "opcmdev",
"_type": "product",
"_id": "2",
"_score": 1,
"_source": {
"productName": "南极人(Nanjiren)床垫家纺 加厚立体床垫羽丝绒床垫床褥子学生宿舍",
"price": 109,
"remark": "不错的床垫",
"tags": [
"家具",
"床垫",
"棉花被"
]
}
},
{
"_index": "opcmdev",
"_type": "product",
"_id": "1",
"_score": 1,
"_source": {
"productName": "法瑞思 天然椰棕床垫棕垫硬床垫定做 薄乳胶棕榈榻榻米床垫定制折叠1.2/1.5/1.8",
"price": 10,
"remark": "不错的床垫",
"tags": [
"家具",
"床垫",
"棉花"
]
}
},
{
"_index": "opcmdev",
"_type": "product",
"_id": "3",
"_score": 1,
"_source": {
"productName": "¥99.00志高(CHIGO)电火锅家用多功能电炒锅电煮锅不粘多用途锅电热锅 ZG-RH2",
"price": 99,
"remark": "平底锅",
"tags": [
"电器",
"平底锅"
]
}
}
]
},
"aggregations": {
"all_tags": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "家具",
"doc_count": 2
},
{
"key": "床垫",
"doc_count": 2
},
{
"key": "平底锅",
"doc_count": 1
},
{
"key": "棉花",
"doc_count": 1
},
{
"key": "棉花被",
"doc_count": 1
},
{
"key": "电器",
"doc_count": 1
}
]
}
}
}

如果聚合前需要查询条件 则改参数

{
"query":{
"match":{
"productName":"平底锅"
}
},
"aggs":{
"all_tags":{
"terms":{
"field":"tags.keyword"
}
}
}
}

结果:

{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.51040375,
"hits": [
{
"_index": "opcmdev",
"_type": "product",
"_id": "3",
"_score": 0.51040375,
"_source": {
"productName": "¥99.00志高(CHIGO)电火锅家用多功能电炒锅电煮锅不粘多用途锅电热锅 ZG-RH2",
"price": 99,
"remark": "平底锅",
"tags": [
"电器",
"平底锅"
]
}
}
]
},
"aggregations": {
"all_tags": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "平底锅",
"doc_count": 1
},
{
"key": "电器",
"doc_count": 1
}
]
}
}
}

分级汇总

比如再进行二次统计 上面结果每个tag的平均价格  因为price是text类型 就不演示结果了

{
"aggs":{
"all_tags":{
"terms":{
"field":"tags.keyword"
},
"aggs":{
"avg_price":{
"avg":{"field":"price"}
}
}
}
}
}

检索响应各字段意思

  {
"took": 1,//这个请求耗费的时间(毫秒)
"timed_out": false,//超时是否 一般不会超时 如果响应速度比完整性更重要可以设置超时时间 es会返回超时前收集到的数据
"_shards": {//检索分片的信息
"total": 5,//参与分片的数量
"successful": 5,//有多少成功的
"skipped": 0,
"failed": 0//有多少失败的
},
"hits": {
"total": 3,//查询总数
"max_score": 1,//指的是所以文档中 相似度最大的值
"hits": [
{
"_index": "opcmdev",//索引
"_type": "product",//type
"_id": "2",//id
"_score": 1,//查询条件的相似度默认最大的排最前面 这个json没有查询条件所有都是1
"_source": {//数据
"productName": "南极人(Nanjiren)床垫家纺 加厚立体床垫羽丝绒床垫床褥子学生宿舍",
"price": "109",
"remark": "不错的床垫",
"tags": [
"家具",
"床垫",
"棉花被"
]
}
},
{
"_index": "opcmdev",
"_type": "product",
"_id": "1",
"_score": 1,
"_source": {
"productName": "法瑞思 天然椰棕床垫棕垫硬床垫定做 薄乳胶棕榈榻榻米床垫定制折叠1.2/1.5/1.8",
"price": "10",
"remark": "不错的床垫",
"tags": [
"家具",
"床垫",
"棉花被"
]
}
},
{
"_index": "opcmdev",
"_type": "product",
"_id": "3",
"_score": 1,
"_source": {
"productName": "¥99.00志高(CHIGO)电火锅家用多功能电炒锅电煮锅不粘多用途锅电热锅 ZG-RH2",
"price": "99",
"remark": "平底锅",
"tags": [
"电器",
"平底锅"
]
}
}
]
}
}

elasticsearch 权威指南入门阅读笔记(一)的更多相关文章

  1. 《Elasticsearch 权威指南》阅读笔记

    书籍地址 https://www.elastic.co/guide/cn/elasticsearch/guide/current/languages.html

  2. elasticsearch 权威指南聚合阅读笔记&lpar;七&rpar;

    count(1) select clssId,count(1) from student group by  classId { "size":0, "aggs&quot ...

  3. elasticsearch 权威指南排序阅读笔记&lpar;六&rpar;

    默认排序 默认查询是通过_source 准确性权重来排序 字段排序 { "query":{ "match":{ "productName": ...

  4. elasticsearch 权威指南搜索阅读笔记&lpar;四&rpar;

    多索引多type搜索 分页搜索 每页5条 查询一到3页数据 第一页:http://127.0.0.1:9200/blogs2/product/_search?size=5&from=0 第二页 ...

  5. 《javascript权威指南》阅读笔记 1

    3.1-3.5 3.1 数字 3.1首先声明了在JS中的数字是不区分整数值和浮点数值的.其次给出了js浮点类型表示的范围:最大值是±1.7976931348623157×10^308,最小值±5×10 ...

  6. 《HTTP权威指南》--阅读笔记(二)

    URL的三部分: 1,方案 scheme 2,服务器位置 3,资源路径 URL语法: <scheme>://<user>:<password>@<host&g ...

  7. 《HTTP权威指南》--阅读笔记(一)

    HTTP: HyperText Transfer Protocol 测试站点:http://www.joes-hardware.com URI包括URL和URN URI: Uniform Resour ...

  8. 《javascript权威指南》读书笔记——第一篇

    <javascript权威指南>读书笔记——第一篇 金刚 javascript js javascript权威指南 由于最近想系统学习下javascript,所以开始在kindle上看这本 ...

  9. Elasticsearch&colon; 权威指南(官方教程)

    <Elasticsearch 权威指南>中文版 序言 前言 基础入门 深入搜索 处理人类语言 聚合 地理位置 数据建模 管理.监控和部署

随机推荐

  1. paper 14 &colon; 图像视觉领域部分开源代码

    做图像处理,没有一定的知识储备是不可能的,但是一定要学会“借力打力”,搜集一些很实用的开源代码,你们看看是否需要~~ 场景识别: SegNet: A Deep Convolutional Encode ...

  2. C&num; JSON字符串序列化与反序列化

    JSON与c#对象转换http://hi.baidu.com/donick/item/4d741338870c91fe97f88d33 C# JSON字符串序列化与反序列化 – http://www. ...

  3. ANDROID&lowbar;MARS学习笔记&lowbar;S01&lowbar;009Relative&lowbar;LAYOUT例子

    1. <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android ...

  4. 原始的js代码和jquery对比

    Even a task as simple as this can be complicated without jQuery at our disposal. In plain JavaScript ...

  5. java基础之位运算

    java中常见的位运算符:&(逻辑与) |(逻辑或) ~(取反) ^(逻辑异或) >>(右移) <<(左移) >>>(无符号右移) &(逻辑与 ...

  6. Android面试题目2

    1. 请描述下Activity的声明周期. onCreate->onStart->onRemuse->onPause->onStop->onRestart->onD ...

  7. POST&sol;有道翻译 有bug

    1.发现在翻译时地址没有变,那是POST请求. 2.通过fidder抓包工具抓取url 3.对data分析,发现每次salt和sign都在变化. 4.查看源码,先用站长工具http://tool.ch ...

  8. matlab json文件解析 需要下载一个jsonlab-1&period;5

    % facebookX24.m clc; addpath('C:\Program Files\MATLAB\R2015b\toolbox\jsonlab-1.5'); % read .json jso ...

  9. win10的cmd中显示:telnet不是内部或外部命令也不是可运行的程序或批处理?

    win10的cmd中显示:telnet不是内部或外部命令也不是可运行的程序或批处理? 摘录自:https://blog.csdn.net/haijing1995/article/details/664 ...

  10. 使用Dom4j创建xml文档

    我们学习dom4j为的就是使用它来创建XML文档.读取文档等更为方便,下面通过一个实例来展示如何通过dom4j去创建XML文档.展示怎样通过两种方式去创建元素,可以直接通过addElement方法返回 ...