
Elasticsearch基础检索
Elasticsearch基础检索
1.实验简介
本实验将由浅入深上手实操【全文检索】、【多语言检索】、【地理位置查询】,搭建高频业务场景应用
2.资源准备
Elasticsearch 8.5.1
-
免费试用
阿里云检索分析服务Elasticsearch版新用户,可享受2c4g规格免费试用一个月
-
按量付费
若免费试用资格已过期或已无试用资格,可选择按量付费,推荐4c8g规格,3.7元起/时(不同地域价格有所差别)
3.场景一:全文检索实验
3.1.中文公开数据集介绍
3.2.导入数据并创建索引
3.2.1.进入网址:https://free.aliyun.com/?pipCode=elasticsearch
选择试用2c4g
3.2.2.进入控制台,进入实例
3.2.3.点击可视化控制
3.2.4.点击修改配置
3.2.5.点击修改配置右侧的公网入口
登录
用户:elastic 密码:自己设置的
3.2.6.下载数据集:https://tianchi.aliyun.com/dataset/132745
3.2.7.点击上传文件
3.2.8.编辑字段名称
点应用,点击导入
导入完成
3.2.9.选择开发工具
3.3.针对索引进行全文检索
POST _analyze
{
"analyzer": "ik_smart",
"text": "双十一"
}
POST _analyze
{
"analyzer": "standard",
"text": "双十一"
}
GET message/_search
{
"query": {
"match_all": {}
}
}
GET message/_search
{
"query":{
"match": {
"content": "双十一"
}
}
}
#使用match_phrase加slop参数匹配
# query里的词必须都匹配,而且还要相邻
# slop:允许词语间跳过的数量,默认可以不写,不写是挨着的没有跳数
GET message/_search
{
"query": {
"match_phrase": {
"content": {
"query": "儿童汽车",
"slop": 10
}
}
}
}
4.场景二:多语言检索实验
DELETE english_index
PUT /english_index
{
"mappings": {
"properties": {
"text":{
"type": "text"
}
}
}
}
PUT _bulk
{"index":{"_index":"english_index","_id":"1"}}
{"text":" Education is the very thing that we want to receive. Our parents send us to school so as to enable us to get/obtain knowledge and achieve great things in the future. The following are the duties of a student (which/that) we should keep in mind."}
{"index":{"_index":"english_index","_id":"2"}}
{"text":"As we all know, love is the crux of a happy life.Love helps us stay calm and serene even when things are tough.It can carry us through the hard times.Love looks for ways to be of service.Love is enjoying the surprises of life, and being totally delighted with what life gives you.Love is the key to happiness and it is a real blessing to others.People who love make the world a kind and gentle place and other people feel safe around them.They appreciate differences instead of making them a cause for prejudice or fighting."}
{"index":{"_index":"english_index","_id":"3"}}
{"text":"Do you mind being called a bad student? Of course. So/As far as I know,everybody intends to be/become a model student.However, to be a model student is by no means an easy thing. First, he must do his best to obtain knowledge. A man without sufficient knowledge will not succeed. Secondly, he must remember to improve hishealth. Only a strong man can do great tasks. Thirdly, he should receive moral education. If his conduct is not good, no one will consider making friends with him."}
{"index":{"_index":"english_index","_id":"4"}}
{"text":"There is no doubt that happiness is the most precious thing in the world. Without it, life will be empty and meaningless. If you wish to know how to get happiness, you must pay attention to the following two points.First, health is the secret of happiness/the key to happiness. Only a strong man can enjoy the pleasure of life.Secondly, happiness consists in contentment. A man who is dissatisfied with his present condition is always in distress."}
GET english_index/_search
{
"query": {
"match_all": {}
}
}
GET english_index/_search
{
"query": {
"match": {
"text": "The GDP of Hangzhou"
}
}
}
DELETE french_index
PUT /french_index
{
"mappings": {
"properties": {
"text":{
"type": "text",
"analyzer": "french"
}
}
}
}
PUT _bulk
{"index":{"_index":"french_index","_id":"1"}}
{"text":"Meilleur scénario: aimez-vous vous-même, profitez de la vie, souriez parce que vous êtes heureux, pas parce que vous le devez."}
{"index":{"_index":"french_index","_id":"2"}}
{"text":"Le bonheur est comme un papillon. Lorsqu'on le poursuit, il est toujours hors de notre portée, mais lorsqu'on s'assoit calmement, il pourrait venir se déposer sur nous."}
{"index":{"_index":"french_index","_id":"3"}}
{"text":"Si la beauté est dans votre coeur, le monde à travers vos yeux le sera aussi."}
GET french_index/_search
{
"query": {
"match_all": {}
}
}
GET french_index/_search
{
"query": {
"match": {
"text": "Si la beauté"
}
}
}
DELETE language
POST language/_doc
{
"中文":"中文",
"英语":"english"
}
GET language/_search
5.场景三:地理位置查询实验
5.1.创建索引并插入测试数据
5.2.地理位置进行查询检索
DELETE /attractions
PUT /attractions
{
"mappings": {
"properties": {
"userid":{
"type": "keyword"
},
"locationname":{
"type": "text"
},
"location":{
"type": "geo_point"
},
"accesstime":{
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
}
}
GET /attractions/_search
{
"query": {
"match_all": {}
}
}
POST /_bulk
{"index":{"_index":"attractions","_id":"1"}}
{"userid":"001","locationname":"颐和园","location":[116.273987,39.992019],"accesstime":"2021-05-01 10:30:00"}
{"index":{"_index":"attractions","_id":"2"}}
{"userid":"002","locationname":"中关村","location":[116.326933,39.981078],"accesstime":"2021-05-02 14:45:30"}
{"index":{"_index":"attractions","_id":"3"}}
{"userid":"003","locationname":"动物园","location":[116.338239,39.942547],"accesstime":"2021-05-03 09:20:15"}
{"index":{"_index":"attractions","_id":"4"}}
{"userid":"004","locationname":"北京大学","location":[116.319935,39.990219],"accesstime":"2021-05-04 16:55:10"}
{"index":{"_index":"attractions","_id":"5"}}
{"userid":"005","locationname":"香山公园","location":[116.191247,39.997232],"accesstime":"2021-05-05 13:00:45"}
{"index":{"_index":"attractions","_id":"6"}}
{"userid":"006","locationname":"圆明园","location":[116.309868,40.018307],"accesstime":"2021-05-06 19:10:20"}
GET /attractions/_search
{
"query": {
"query_string": {
"default_field": "userid",
"query": "005"
}
}
}
GET /attractions/_search
{
"query": {
"bool": {
"filter": {
"geo_distance": {
"distance": "10km",
"location": {
"lat": 39.997232,
"lon": 116.191247
}
}
}
}
}
}
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果