ElasticSearcho从入门到放弃:(二)操作, 编程, 架构原理, ES SQL - Go语言中文社区

ElasticSearcho从入门到放弃:(二)操作, 编程, 架构原理, ES SQL


文章目录

一、操作:

1. 创建索引

为了能够搜索数据, 需要提前在ES中创建索引, 然后才能进行关键字的检索;
在ES中, 也可以使用mysql中创建一个表, 指定表名, 列, 列属性的方式;

1.1 创建带有映射的索引:

ES中, 可以使用RESTful APi来进行索引的各种操作;
创建mysql表时, 使用DDL来描述表结构, 字段, 字段类型,约束等; 在ES中, 使用DSL来定义

PUT /mysql-index
{
	"mappings": {
		"properties" {
			"employee-id": {
				"type": "keyword",
				"index": false
			}
		}
	}
}

在这里插入图片描述

1.2 字段类型

分类类型名称说明
简单类型text需要进行全文检索的字段;
通常使用text类型来对应邮件正文、产品描述或短文等非结构化文本数据;
分词器先会将文本进行分词转换为词条列表; 将来就可以基于词条进行检索了;
文本字段不能用户排序, 也很少聚合计算;
keyword使用keyword来对应结构化的数据, 如ID,、电子邮件地址、主机名、状态码、标签等;
可以使用keyword来进行排序和聚合计算;
注意: keyword是不能进行分词的;
long/integer/short/byte64位整数/32位整数/16位整数/8位整数
doublefloat
booleantrue / false
ipIpv4 / ipv6
json分层嵌套类型object用于保存json对象
nested用于保存json数组
特殊类型geo_point用于保存经纬度坐标
geo_shape用于保存地图上多边形坐标

1.3 创建保存"职位"信息的索引

ps: 判断使用text还是keyword, 主要看是否需要分词

字段说明类型
doc_id唯一标识(作为文档ID)keyword
area职位所在区域keyword
exp岗位要求的工作经验text
edu学历要求keyword
salary薪资范围keyword
job_type职位类型(全职/兼职/实习)keyword
cmp公司名text
pv浏览量keyword
title岗位名称text
jd职位描述text
PUT /job_idx
{
	"mappings": {
		"properties": {
			"area": { "type": "text", "store": true },
			"exp": { "type": "text", "store": true },
			"edu": { "type": "keyword", "store": true },
			"salary": { "type": "keyword", "store": true },
			"job_type": { "type": "keyword", "store": true },
			"cmp": { "type": "text", "store": true },
			"pv": { "type": "keyword", "store": true },
			"title": { "type": "text", "store": true },
			"jd": { "type": "text", "store": true }
		}
	}
}

result:

{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "job_idx"
}

1.4 查看索引映射

使用Get请求查看索引映射

GET /job_idx/_mapping

result:

{
    "job_idx": {
        "mappings": {
            "properties": {
                "area": {
                    "type": "text",
                    "store": true
                },
                "cmp": {
                    "type": "text",
                    "store": true
                },
                "edu": {
                    "type": "keyword",
                    "store": true
                },
                "exp": {
                    "type": "text",
                    "store": true
                },
                "jd": {
                    "type": "text",
                    "store": true
                },
                "job_type": {
                    "type": "keyword",
                    "store": true
                },
                "pv": {
                    "type": "keyword",
                    "store": true
                },
                "salary": {
                    "type": "keyword",
                    "store": true
                },
                "title": {
                    "type": "text",
                    "store": true
                }
            }
        }
    }
}

1.5 查看ES中素有索引

GET _cat/indices

result:
在这里插入图片描述

1.6 删除索引

DELETE /job-idx

result:

{
    "acknowledged": true
}

1.7 指定使用IK分词器

因为存放在索引库中的数据, 是以中文的形式存储的, 所以, 使用Ik分词器

PUT /job_idx
{
	"mappings": {
		"properties": {
			"area": { "type": "text", "store": true, "analyzer": "ik_max_word" },
			"exp": { "type": "text", "store": true, "analyzer": "ik_max_word" },
			"edu": { "type": "keyword", "store": true },
			"salary": { "type": "keyword", "store": true },
			"job_type": { "type": "keyword", "store": true },
			"cmp": { "type": "text", "store": true, "analyzer": "ik_max_word" },
			"pv": { "type": "keyword", "store": true },
			"title": { "type": "text", "store": true, "analyzer": "ik_max_word" },
			"jd": { "type": "text", "store": true, "analyzer": "ik_max_word" }
		}
	}
}

2. 使用PUT添加一条数据

在es中, 每一个文档都有唯一的ID, 也是使用json格式来描述数据的;

PUT /customer/_doc/1
{
	"name": "John"
}

在这里插入图片描述

  • 如果在costomer中, 不存ID为1的文档, ES会自动创建

2.1 添加一条职位信息

PUT /job_idx/_doc/29097
{
	"area": "深圳-南山区",
	"exp": "一年经验",
	"edu": "本科及以上",
	"salary": "8-12K/月",
	"job_type": "实习",
	"cmp": "乐有家",
	"pv": "618万人浏览过/14人评价/113人正在关注",
	"title": "桃园 深大销售实习 岗前培训",
	"jd": "这是一个 桃园 深大销售实习 岗前培训的职位描述, 一些乱七八在的说明, 我没有文档, 懒得手打了"
}

result:

{
    "_index": "job_idx",
    "_id": "29097",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

3 修改数据

3.1 执行update操作

POST /job_idx/29097
{
	"doc": {
		"salary": "80-120k/月"
	}
}

result:

{
    "_index": "job_idx",
    "_id": "29097",
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 1
}

4. 删除操作

DELETE /job_idx/_doc/29097

result:

{
    "_index": "job_idx",
    "_id": "29097",
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 1
}

5. 批量导入json数据

5.1 bulk导入:

es提供了bulk接口, 用来批量导入json文件中的数据

curl -H "Content-Type:application/json" -XPOST "localhost:9200/job_idx/bulk?pretty&refresh" --data-binary "@job_info.json"

6. 查看索引状态

GET /_cat/indices?index=job_idx

在这里插入图片描述

7. 检索:

7.1 根据ID检索数据

在这里插入图片描述

GET /job_idx/_search
{
    "query": {
        "ids": {
            "values": ["29097"]
        }
    }
}

result:

{
    "took": 47,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "job_idx",
                "_id": "29097",
                "_score": 1.0,
                "_source": {
                    "area": "深圳-南山区",
                    "exp": "一年经验",
                    "edu": "本科及以上",
                    "salary": "80-120k/月",
                    "job_type": "实习",
                    "cmp": "乐有家",
                    "pv": "618万人浏览过/14人评价/113人正在关注",
                    "title": "桃园 深大销售实习 岗前培训",
                    "jd": "这是一个 桃园 深大销售实习 岗前培训的职位描述, 一些乱七八在的说明, 我没有文档, 懒得手打了"
                }
            }
        ]
    }
}

7.2 根据关键字搜索

检索jd中"销售"相关的岗位

GET /job_idx/_search
{
    "query": {
        "match": {
            "jd": "销售"
        }
    }
}

result:

{
    "took": 49,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0.2876821,
        "hits": [
            {
                "_index": "job_idx",
                "_id": "29097",
                "_score": 0.2876821,
                "_source": {
                    "area": "深圳-南山区",
                    "exp": "一年经验",
                    "edu": "本科及以上",
                    "salary": "80-120k/月",
                    "job_type": "实习",
                    "cmp": "乐有家",
                    "pv": "618万人浏览过/14人评价/113人正在关注",
                    "title": "桃园 深大销售实习 岗前培训",
                    "jd": "这是一个 桃园 深大销售实习 岗前培训的职位描述, 一些乱七八在的说明, 我没有文档, 懒得手打了"
                }
            }
        ]
    }
}

官方网站视频课: https://www.elastic.co/cn/webinars/getting-started-elasticsearch?baymax=rtp&elektra=doc&storm-top-video&iesrc=ctr

7.3 根据关键分页搜索

在存在大量数据时, 一般进行查询都需要进行分页查询;

7.3.1 使用from和size来进行分页

在执行查询时, 可以指定from(从第n个开始)和size(每页返回多少条)来完成分页

GET /job_idx/_search
{
	"from": 0,
	"size": 5,
	"query": {
		"multi_match": {
			"query": "销售",
			"fields": ["title", "jd"]
		}
	}
}

ps:

  • from = (page-1) *size

7.3.2 使用scroll方式进行分页

使用from和size方式, 查询1w-5w条数据以内是ok的, 但是, 如果数据比较多的时候, 会出现性能问题; ES做了一个限制, 不允许查询超过1w条以后的数据, 如果要查询, 需要使用ES中提供的scoll(游标)来查询;
在进行大量分页时, 每次分页都需要将要查询的数据进行重新排序, 这样非常浪费性能;
使用scoll是将要用的数据一次性排序好, 然后分批取出; 性能要比from+size好很多;
使用scroll查询后, 排序后数据会保持一段时间, 后续分页查询都从该快照取数据;
使用scoll是为了解决深分页的性能问题

第一次使用scroll分页查询
此处, 让排序数据保持1分钟

GET /job_idx/_search?scroll=1m
{
	"size": 100,
	"query": {
		"multi_match": { // 检索多个字段
			"query": "销售",
			"fields": ["title", "jd"]
		}
	}
}

result:

{
    "_scroll_id": "FGluY2x1ZGVfY29udGV4dF91dWlkDXF1ZXJ5QW5kRmV0Y2gBFk9TSmltZ2kxU1hlbHVJcHd3dEphUXcAAAAAAAAAWBY1Ymo4VGlzclI4V0dzc0x6aXZsczNR",
    "took": 29,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0.2876821,
        "hits": [
            {
                "_index": "job_idx",
                "_id": "29097",
                "_score": 0.2876821,
                "_source": {
                    "area": "深圳-南山区",
                    "exp": "一年经验",
                    "edu": "本科及以上",
                    "salary": "80-120k/月",
                    "job_type": "实习",
                    "cmp": "乐有家",
                    "pv": "618万人浏览过/1
                        
版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_35709559/article/details/123973584
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2023-01-02 20:22:22
  • 阅读 ( 227 )
  • 分类:架构

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢