es的优势

系列文章目录

提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
例如:第一章 Python 机器学习入门之pandas的使用


提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 系列文章目录
  • 前言
  • 一、es为什么比mysql快
  • 二、使用步骤
    • 1.引入库
    • 2. es查询语法
  • 三,api功能
  • 总结


前言

总结es优势


一、es为什么比mysql快

  1. es是一个基于Lucene引擎库,基于内存,查询速度比mysql,这个是在存储方式的比较
  2. 第二是数据存储方式,倒排索引,存储方式,可以快速找到数据的大概位置,文档列表,利用二叉查询方法进行寻找方式
  3. es支持复杂的语法格式,寻找附近酒店,进行分页
  4. 缺点
  5. 过于占内存

二、使用步骤

1.引入库

代码如下(示例):

package com.cn;import com.alibaba.fastjson.JSON;
import com.cn.mapper.ESMapper;
import com.cn.pojo.Hotel;
import com.cn.pojo.TbHotel;
import com.cn.pojo.vo.TbHotelVo;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;@RunWith(SpringRunner.class)
@SpringBootTest
public class EsTest {@Autowiredprivate RestHighLevelClient client;@Autowiredprivate ESMapper esMapper;@BeforeEachvoid setUp() {this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://192.168.36.128:9200")));}@AfterEachvoid tearDown() throws IOException {this.client.close();}/*** 判断索引是否存在* @throws IOException*/@Testpublic void  getIndexRequest() throws IOException {GetIndexRequest request = new GetIndexRequest("tiantian");boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);System.out.println(exists?"不能再":"我问");}/*** 批量导入文档* @throws IOException*/@Testpublic void c() throws IOException {List<Hotel> hotels = new ArrayList<>();for (int i = 0; i < 50; i++) {Hotel hotel = new Hotel();hotel.setId(Long.valueOf(i));hotel.setAge(i);hotel.setAddress("我的地址方式"+i);hotel.setTime(new Date());hotel.setName("将来"+i);hotel.setLatLon("23.5","3"+i);hotel.setLocation("23.5","3"+i);hotels.add(hotel);}//批量导入BulkRequest bulkRequest = new BulkRequest();//转化文档for (Hotel hotel : hotels) {bulkRequest.add(new IndexRequest("tiantian").id(hotel.getId().toString()).source(JSON.toJSONString(hotel), XContentType.JSON));}//发送请求BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);System.out.println(bulk);}/*** 导入数据信息* @throws IOException*/@Testpublic void bulkRequest() throws IOException {BulkRequest bulkRequest = new BulkRequest();List<TbHotel> tbHotels = esMapper.selectList(null);for (TbHotel tbHotel : tbHotels) {TbHotelVo tbHotelVo = new TbHotelVo(tbHotel);bulkRequest.add(new IndexRequest("hotel").id(tbHotelVo.getId().toString()).source(JSON.toJSONString(tbHotelVo),XContentType.JSON));}client.bulk(bulkRequest,RequestOptions.DEFAULT);}/*** 查询所有文档信息* @throws IOException*/@Testpublic void testMatchAll() throws IOException {SearchRequest request = new SearchRequest("hotel");request.source().query(QueryBuilders.matchAllQuery());SearchResponse response = client.search(request, RequestOptions.DEFAULT);for (SearchHit hit : response.getHits().getHits()) {String source = hit.getSourceAsString();System.out.println(source);}}/*** 准确查询* @throws IOException*/@Testpublic void matchQuery() throws IOException {SearchRequest request = new SearchRequest("hotel");request.source().query(QueryBuilders.termQuery("price","189"));SearchResponse response = client.search(request, RequestOptions.DEFAULT);for (SearchHit hit : response.getHits().getHits()) {String sourceAsString = hit.getSourceAsString();System.out.println(sourceAsString);}}/*** 布尔查询方式* 查询名字叫做如家* 价格200* 地址在上海*/@Testpublic void boolQuery() throws IOException {SearchRequest request = new SearchRequest("hotel");BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();boolQuery.mustNot(QueryBuilders.rangeQuery("price").gte("400"));boolQuery.must(QueryBuilders.matchQuery("city","上海"));boolQuery.must(QueryBuilders.matchQuery("name","如家"));request.source().query(boolQuery);for (SearchHit hit : client.search(request, RequestOptions.DEFAULT).getHits().getHits()) {String sourceAsString = hit.getSourceAsString();System.out.println(sourceAsString);}}/*** bool查询方式* @throws IOException*/@Testpublic void testBoole() throws IOException {SearchRequest request = new SearchRequest("hotel");BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();boolQuery.must(QueryBuilders.matchQuery("name","如家"));request.source().query(boolQuery);for (SearchHit hit : client.search(request, RequestOptions.DEFAULT).getHits()) {System.out.println(hit.getSourceAsString());}}/*** 根据经纬度查询方式* @throws IOException*/@Testpublic void geoPoint() throws IOException {SearchRequest request = new SearchRequest("hotel");request.source().sort(SortBuilders.geoDistanceSort("lonAndLat",new GeoPoint("31.2,121.5")).order(SortOrder.ASC).unit(DistanceUnit.KILOMETERS));SearchResponse response = client.search(request, RequestOptions.DEFAULT);for (SearchHit hit : response.getHits().getHits()) {TbHotel tbHotel = JSON.parseObject(hit.getSourceAsString(), TbHotel.class);System.out.println(tbHotel.getLonAndLat());}}@Testpublic void testMat() throws IOException {SearchRequest request = new SearchRequest("hotel");request.source().query(QueryBuilders.matchQuery("name","如家"));SearchResponse response = client.search(request, RequestOptions.DEFAULT);for (SearchHit hit : response.getHits().getHits()) {System.out.println(hit.getSourceAsString());}}
}

2. es查询语法

代码如下(示例):


//添加索引
PUT /tiantian
{"mappings": {"properties": {"name":{"type": "text", "analyzer": "ik_smart"},"age":{"type": "integer", "index": false}}}
}//查询索引
GET /tiantian//查询文档信息
GET /tiantian/_doc/1//添加字段信息
PUT /tiantian/_mapping
{"properties":{"address":{"type":"text","index":false}}
}//添加数据文档数据信息
POST /tiantian/_doc/1
{"address":"巴黎","name":"太牛龙","age":123
}//查询经纬度
GET /hotel/_search
{"query": {"match_all": {}},"sort": [{"price": {"order": "desc"}},{"_geo_distance": {"lonAndLat": {"lat": 31.2,"lon": 121.5},"order": "asc"}}]
}POST /hotel/_update/1902197537
{"doc": {"isAD": true}
}
POST /hotel/_update/2056126831
{"doc": {"isAD": true}
}
POST /hotel/_update/1989806195
{"doc": {"isAD": true}
}
POST /hotel/_update/2056105938
{"doc": {"isAD": true}
}GET /hotel/_search
{"query": {"function_score": {"query": {"match": {"name": "外滩"}},"functions": [{"filter": { "term": {"id": "1"}},"weight": 10}],"boost_mode": "multiply"}},"from": 1,"size": 10
}GET /hotel/_search
{"query": {"function_score": {"query": {"match": {"name": "如家"}},"functions": [{"filter": {"term": {"name": "339952837"}}, "weight": 10}],"boost_mode": "sum"}}
}GET /hotel/_search
{"query": {"match_all": {}}
}GET /hotelGET /hotel/_search
{"size": 0, "aggs": {"brandAgg": {"terms": {"field": "brand.keyword"}}}}GET /hotel/_search
{"query": {"match": {"name": "东方明珠"}}
}

三,api功能

@Overridepublic PageResult search(RequestParams params) throws IOException {//1.准备发送SearchRequest request = new SearchRequest("hotel");//2.准备布尔条件BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();//3.判断是否存在String key = params.getKey();if (key==null || "".equals(key)){boolQuery.must(QueryBuilders.matchAllQuery());}else {boolQuery.must(QueryBuilders.matchQuery("name",key));}Integer size = params.getSize();Integer page = params.getPage();request.source().from((page - 1) * size).size(size);request.source().query(boolQuery);String location = params.getLocation();//得出具体路径if (location!=null && !location.equals("")){request.source().sort(SortBuilders.geoDistanceSort("lonAndLat",new GeoPoint(location)).order(SortOrder.ASC).unit(DistanceUnit.KILOMETERS));}//相关性算法FunctionScoreQueryBuilder scoreQuery = QueryBuilders.functionScoreQuery(boolQuery,new FunctionScoreQueryBuilder.FilterFunctionBuilder[]{new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.termQuery("isAD", true),ScoreFunctionBuilders.weightFactorFunction(10))});request.source().query(scoreQuery);SearchResponse response = client.search(request, RequestOptions.DEFAULT);return  handleResponse(response);}

根据经纬度查询方式
在这里插入图片描述

//得出具体路径if (location!=null && !location.equals("")){request.source().sort(SortBuilders.geoDistanceSort("lonAndLat",new GeoPoint(location)).order(SortOrder.ASC).unit(DistanceUnit.KILOMETERS));}

相关性算法

请添加图片描述

       //相关性算法FunctionScoreQueryBuilder scoreQuery = QueryBuilders.functionScoreQuery(boolQuery,new FunctionScoreQueryBuilder.FilterFunctionBuilder[]{new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.termQuery("isAD", true),ScoreFunctionBuilders.weightFactorFunction(10))});

总结

  1. es高效,速度快,基于lu的内存数据库,采用倒序索引的方式,倒叙索引好处,我们之前数据库根据id找到值,倒排索引的方式,es默认创建索引,根据值找到,对应的文档列表,文档列表根据二分查找方式,找到对应的值
  2. es强大的api功能普通基于内存的数据库的方式,比如redis功能,适合做缓存,没有强大的api,不能做复杂的功能,es有强大api,分页,查询,联合查询,经纬度查询,相关性质查询方式

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/193491.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【设计模式】设计模式基础

设计模式基础 文章目录 设计模式基础一、七大设计原则1.1 概述1.2 单一职责原则1.3 接口隔离原则1.4 依赖倒转原则1.5 里氏替换原则1.6 开闭原则1.7 迪米特法则1.8 合成复用原则 二、UML类图2.1 概述2.2 依赖关系&#xff08;Dependence&#xff09;2.3 泛化关系(generalizatio…

庖丁解牛:NIO核心概念与机制详解 01

文章目录 Pre输入/输出Why NIO流与块的比较通道和缓冲区概述什么是缓冲区&#xff1f;缓冲区类型什么是通道&#xff1f;通道类型 NIO 中的读和写概述Demo : 从文件中读取1. 从FileInputStream中获取Channel2. 创建ByteBuffer缓冲区3. 将数据从Channle读取到Buffer中 Demo : 写…

『力扣刷题本』:环形链表(判断链表是否有环)

一、题目 给你一个链表的头节点 head &#xff0c;判断链表中是否有环。 如果链表中有某个节点&#xff0c;可以通过连续跟踪 next 指针再次到达&#xff0c;则链表中存在环。 为了表示给定链表中的环&#xff0c;评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置&am…

使用内网穿透解决支付宝回调地址在公网问题

使用natapp解决内网穿透问题 前言NATAPP使用购买隧道 支付宝回调地址测试之后的学习计划 前言 最近一个项目用到了支付宝&#xff0c;但是本地调试的时候发现支付宝的回调地址需要在公网上能够访问到。为了更加方便地调试&#xff0c;就使用了natapp内网穿透&#xff0c;将回调…

知道数字孪生发展的四个阶段,你就能明白数字孪生的真正价值了

数字孪生的发展经历了四个阶段。以下是每个阶段的详细描述&#xff1a; 1、数字孪生萌芽期&#xff1a;这个阶段以模型仿真驱动为特征。20世纪80年代以来&#xff0c;CAD、CAE、CAM等计算机建模、模拟仿真技术开始迅猛发展&#xff0c;并在制造业领域广泛应用。该阶段主要通过…

Spring Boot中使用MongoDB完成数据存储

我们在开发中用到的数据存储工具有许多种&#xff0c;我们常见的数据存储工具包括&#xff1a; 关系性数据库&#xff1a;使用表格来存储数据&#xff0c;支持事务和索引。&#xff08;如&#xff1a;MySQL&#xff0c;Oracle&#xff0c;SQL Server等&#xff09;。NoSQL数据…

鸿蒙:Harmony开发基础知识详解

一.概述 工欲善其事&#xff0c;必先利其器。 上一篇博文实现了一个"Hello Harmony"的Demo&#xff0c;今天这篇博文就以Demo "Hello Harmony" 为例&#xff0c;以官网开发文档为依据&#xff0c;从鸿蒙开发主要的几个方面入手&#xff0c;详细了解一下鸿…

大模型是怎么知道 “我赚了200万” 的?

今天在和 chatGPT 聊天时&#xff0c;我说“我赚了200万”&#xff0c;他立刻就根据这句话给我了一句。 我当然没有赚到200万&#xff0c;只是想引出一个话题&#xff1a;“大模型是如何识别出这句话&#xff0c;又是怎么知道该回答什么的呢&#xff1f;" 在学习自然语言…

vue之浏览器存储方法封装实例

我们在项目中通常会对缓存进行一些操作&#xff0c;为了便于全局调用&#xff0c;会对缓存的设置、获取及删除方法进行封装成一个工具类。 首先我们在src目录下创建一个plugins文件夹&#xff0c;在plugins下创建cache文件夹并创建index.js&#xff0c;代码如下&#xff1a; c…

腾讯云服务器带宽计费模式_按流量和带宽收费说明

腾讯云服务器带宽计费模式分为“按带宽计费”和“按使用流量”两种计费模式&#xff1a;按带宽计费是预付费&#xff0c;一次性购买固定带宽值&#xff0c;先付费&#xff1b;按使用流量计费是先使用后付费&#xff0c;根据云服务器公网出方向实际产生流量来计算。如何选择带宽…