当前位置:   article > 正文

05_es分布式搜索引擎1_es搜索引擎 查询索引库

es搜索引擎 查询索引库

一、初始elasticsearch

1.elasticsearch简单介绍

①什么是elasticsearch?

开源的分布式搜索引擎,实现海量数据搜索,日志统计,分析,系统监控等功能

②什么是elastic stack?

是以elasticsearch为核心的技术栈,包括beats,Logstash,kibana, elasticsearch

③什么是Lucene?

是Apache开源搜索引擎类库,提供搜索引擎的API

2.正向索引和倒排索引

①什么是文档document和词条term?

文档:每一条数据就是文档

词条:对文档的内容分词

②什么是正向索引?

基于文档id创建索引,查询词条时必须找到文档,然后判断该文档是否包含词条。

③什么是倒排索引?

对文档内容进行分词,对词条创建索引,并记录词条所在文档的信息。

搜索时要进行分词,然后去词条列表查询文档id,通过文档id查询文档,把结果存放在结果集。

④倒排索引的组成

词条词典:记录所有词条, 词条和倒排列表PostingList之间关系。给词条创建索引,提高查询和插入效率

倒排列表PostingList:记录词条所在的文档id,词条出现频率,词条在文档的位置

 

3.ES的基本概念

①索引index:

同类型文档的集合。类似Mysql的表table

②文档document:

一条数据就是一个文档,文档数据在es是Json格式。类似Mysql的行row

③字段field:

Json文档的字段。类似Mysql的列

④映射Mapping:

字段的类型约束信息,类似表的结构约束

4.ES-MySQL的架构

ES:海量数据搜索

Mysql:事务操作,数据安全性和一致性

二、索引库操作

1.索引库 (建表)的概念

①mapping是对文档字段的约束。

②mapping约束的属性

type:字段类型(

字符串:text(分词)、keyword(不分词)

数字:long、integer、short、byte、double、float

布尔:boolean

日期:date

对象:object

index:该字段是否用约束,默认true

analyzer:使用的分词器

properties:该字段下的子字段

2.创建索引库

ES通过Restful请求操作索引库和文档。请求内容通过DSL语句表示

 创建索引库:创建一个索引库heima,有字段info,email,name有子字段firstName, lastName

  1. # 创建索引库heima
  2. PUT /heima
  3. {
  4. "mappings": {
  5. "properties": {
  6. "info":{
  7. "type":"text",
  8. "analyzer":"ik_smart"
  9. },
  10. "email":{
  11. "type": "keyword",
  12. "index": false
  13. },
  14. "name":{
  15. "type": "object",
  16. "properties": {
  17. "firstName":{
  18. "type":"keyword"
  19. },
  20. "lastName":{
  21. "type":"keyword"
  22. }
  23. }
  24. }
  25. }
  26. }
  27. }

查看索引库:GET /索引库名

删除索引库:DELETE /索引库名

添加索引库字段:PUT /索引库名/_mapping

  1. PUT /heima/_mapping
  2. {
  3. "properties":{
  4. "age":{
  5. "type":"integer"
  6. }
  7. }
  8. }

3.文档操作(添加数据)

①增

  1. POST /heima/_doc/1
  2. {
  3. "age": 20,
  4. "info": "这是学生",
  5. "email": "1111@qq.com",
  6. "name": {
  7. "lastName": "wang",
  8. "firsrName":"zhi"
  9. }
  10. }

②删

DELETE /索引库名/_doc/文档id

③改

方式一:根据主键值id全量修改,删除旧文档,新增以下字段文档。此时只有age

  1. PUT /heima/_doc/1
  2. {
  3. "age": 10
  4. }

方式二:修改某个字段值,只把年龄修改

  1. POST /heima/_update/1
  2. {
  3. "doc":{
  4. "age": 10
  5. }
  6. }

④查

GET /索引库名/_doc/文档id

GET /heima/_doc/1

4.文档操作有哪些?

  • 创建文档:POST /索引库名/_doc/文档id  { json文档 }
  • 查询文档:GET /索引库名/_doc/文档id
  • 删除文档:DELETE /索引库名/_doc/文档id
  • 修改文档:

        全量修改:PUT /索引库名/_doc/文档id { json文档 }

        增量修改:POST /索引库名/_update/文档id { "doc": {字段}}

三、RestClient操作索引库

1.什么是RestClient

RestClient本质是组装DSL语句,通过http请求发送ES

2.利用JavaRestClient实现创建、删除索引库。判断索引库是否存在

步骤1:导入资料包hotel-demo

步骤2:分析数据结构

①mapping要考虑问题:字段名,数据类型type,是否搜索index,是否分词analyzer(分词器)

mapping映射

  1. # 酒店的mapping
  2. PUT /hotel
  3. {
  4. "mappings": {
  5. "properties": {
  6. "id":{
  7. "type": "keyword"
  8. },
  9. "name":{
  10. "type": "text",
  11. "analyzer": "ik_max_word"
  12. },
  13. "address":{
  14. "type": "keyword",
  15. "index": false
  16. },
  17. "price":{
  18. "type": "integer"
  19. },
  20. "score":{
  21. "type":"integer"
  22. },
  23. "brand":{
  24. "type":"keyword"
  25. },
  26. "city":{
  27. "type":"keyword"
  28. },
  29. "starName":{
  30. "type":"keyword"
  31. },
  32. "business":{
  33. "type":"keyword"
  34. },
  35. "location":{
  36. "type":"geo_point"
  37. },
  38. "pic":{
  39. "type":"keyword",
  40. "index":false
  41. }
  42. }
  43. }
  44. }

修改:把全部搜索的字段组合放在all字段

  1. # 酒店的mapping
  2. PUT /hotel
  3. {
  4. "mappings": {
  5. "properties": {
  6. "id":{
  7. "type": "keyword"
  8. },
  9. "name":{
  10. "type": "text",
  11. "analyzer": "ik_max_word",
  12. "copy_to": "all"
  13. },
  14. "address":{
  15. "type": "keyword",
  16. "index": false
  17. },
  18. "price":{
  19. "type": "integer"
  20. },
  21. "score":{
  22. "type":"integer"
  23. },
  24. "brand":{
  25. "type":"keyword",
  26. "copy_to": "all"
  27. },
  28. "city":{
  29. "type":"keyword"
  30. },
  31. "starName":{
  32. "type":"keyword"
  33. },
  34. "business":{
  35. "type":"keyword"
  36. },
  37. "location":{
  38. "type":"geo_point"
  39. },
  40. "pic":{
  41. "type":"keyword",
  42. "index":false
  43. },
  44. "all":{
  45. "type": "text",
  46. "analyzer": "ik_max_word"
  47. }
  48. }
  49. }
  50. }

步骤3:初始化JavaRestClient

①导入es依赖

  1. <dependency>
  2. <groupId>org.elasticsearch.client</groupId>
  3. <artifactId>elasticsearch-rest-high-level-client</artifactId>
  4. <version>7.12.1</version>
  5. </dependency>

②初始化RestHighLevelClient:

  1. public class HotelIndexTest {
  2. private RestHighLevelClient client;
  3. @Test
  4. void testInit(){
  5. }
  6. @BeforeEach
  7. void setUp() {
  8. this.client = new RestHighLevelClient(RestClient.builder(
  9. HttpHost.create("http://192.168.137.129:9200")
  10. ));
  11. }
  12. @AfterEach
  13. void tearDown() throws IOException {
  14. this.client.close();
  15. }
  16. }

所有的单元测试,先运行@BeforeEach再@Test,最后@AfterEach

步骤4:

MAPPING_TEMPLATE

  1. public class HotelConstants {
  2. public static final String MAPPING_TEMPLATE =
  3. "{\n" +
  4. " \"mappings\": {\n" +
  5. " \"properties\": {\n" +
  6. " \"id\":{\n" +
  7. " \"type\": \"keyword\"\n" +
  8. " },\n" +
  9. " \"name\":{\n" +
  10. " \"type\": \"text\",\n" +
  11. " \"analyzer\": \"ik_max_word\",\n" +
  12. " \"copy_to\": \"all\"\n" +
  13. " },\n" +
  14. " \"address\":{\n" +
  15. " \"type\": \"keyword\",\n" +
  16. " \"index\": false\n" +
  17. " },\n" +
  18. " \"price\":{\n" +
  19. " \"type\": \"integer\"\n" +
  20. " },\n" +
  21. " \"score\":{\n" +
  22. " \"type\":\"integer\"\n" +
  23. " },\n" +
  24. " \"brand\":{\n" +
  25. " \"type\":\"keyword\",\n" +
  26. " \"copy_to\": \"all\"\n" +
  27. " },\n" +
  28. " \"city\":{\n" +
  29. " \"type\":\"keyword\"\n" +
  30. " }, \n" +
  31. " \"starName\":{\n" +
  32. " \"type\":\"keyword\"\n" +
  33. " },\n" +
  34. " \"business\":{\n" +
  35. " \"type\":\"keyword\"\n" +
  36. " },\n" +
  37. " \"location\":{\n" +
  38. " \"type\":\"geo_point\"\n" +
  39. " },\n" +
  40. " \"pic\":{\n" +
  41. " \"type\":\"keyword\",\n" +
  42. " \"index\":false\n" +
  43. " },\n" +
  44. " \"all\":{\n" +
  45. " \"type\": \"text\",\n" +
  46. " \"analyzer\": \"ik_max_word\"\n" +
  47. " }\n" +
  48. " }\n" +
  49. " }\n" +
  50. "}\n";
  51. }

①创建索引库

  1. @Test
  2. void createHotelIndex() throws IOException {
  3. // 1.创建request对象(索引库)
  4. CreateIndexRequest request = new CreateIndexRequest("hotel");
  5. // 2.请求参数(mapping)
  6. request.source(MAPPING_TEMPLATE, XContentType.JSON);
  7. // 3.发送请求(创建)
  8. client.indices().create(request, RequestOptions.DEFAULT);
  9. }

②查询索引库是否存在

  1. @Test
  2. void getHotelIndex() throws IOException {
  3. // 1.创建查询对象
  4. GetIndexRequest request = new GetIndexRequest("hotel");
  5. // 2.执行查询
  6. Boolean response = client.indices().exists(request, RequestOptions.DEFAULT);
  7. System.out.println(response);
  8. }

③删除索引库

  1. @Test
  2. void deleteHotelIndex() throws IOException {
  3. // 1.创建删除对象
  4. DeleteIndexRequest request = new DeleteIndexRequest("hotel");
  5. // 2.执行删除
  6. client.indices().delete(request,RequestOptions.DEFAULT);
  7. }

索引库操作的基本步骤:

  • 初始化RestHighLevelClient
  • 创建XxxIndexRequest。XXX是Create、Get、Delete
  • 准备DSL( Create时需要)
  • 发送请求。调用RestHighLevelClient#indices().xxx()方法,xxx是create、exists、delete

四、RestClient操作文档

1.添加文档client.index()

①查询数据库里面ID号为38609酒店信息

②把查询的信息转换为json插入,id从信息里面获取

  1. @Test
  2. void testIndexDocument() throws IOException {
  3. // 1.根据id查询
  4. Hotel hotel = hotelService.getById(38609L);
  5. // 转换为文档数据
  6. HotelDoc hotelDoc = new HotelDoc(hotel);
  7. // 2.创建request对象
  8. IndexRequest request = new IndexRequest("hotel").id(hotelDoc.getId().toString());
  9. // 创建JSON文档
  10. request.source(JSON.toJSONString(hotelDoc), XContentType.JSON);
  11. // 发送请求
  12. client.index(request, RequestOptions.DEFAULT);
  13. }

2.查询文档client.get()

  1. @Test
  2. void testGetIndex() throws IOException {
  3. // 查询请求
  4. GetRequest request = new GetRequest("hotel","38609");
  5. // 发送请求
  6. GetResponse response = client.get(request, RequestOptions.DEFAULT);
  7. // 获取结果_source的JSON
  8. String json = response.getSourceAsString();
  9. // JSON转换为对象
  10. HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
  11. System.out.println(hotelDoc);
  12. }

3.更新文档client.update()

  1. @Test
  2. void testUpdate() throws IOException {
  3. // 更新请求
  4. UpdateRequest request = new UpdateRequest("hotel","38609");
  5. // 更新字段 K,V 逗号隔开
  6. request.doc(
  7. "price","666",
  8. "starName","四钻"
  9. );
  10. // 执行更新
  11. client.update(request,RequestOptions.DEFAULT);
  12. }

4.根据ID删除文档

  1. @Test
  2. void testDelete() throws IOException {
  3. // 删除请求
  4. DeleteRequest request = new DeleteRequest("hotel","38609");
  5. // 执行删除
  6. client.delete(request,RequestOptions.DEFAULT);
  7. }

5.批量导入数据BulkRequest

  1. @Test
  2. void testBulk() throws IOException {
  3. // 查询数据
  4. List<Hotel> hotels = hotelService.list();
  5. // 创建Request
  6. BulkRequest request = new BulkRequest();
  7. // 批量数据
  8. for (Hotel hotel : hotels) {
  9. HotelDoc hotelDoc = new HotelDoc(hotel);
  10. request.add(new IndexRequest("hotel")
  11. .id(hotelDoc.getId().toString())
  12. .source(JSON.toJSONString(hotelDoc),XContentType.JSON));
  13. }
  14. // 执行添加
  15. client.bulk(request,RequestOptions.DEFAULT);
  16. }

6.总结文档操作步骤

  • 初始化RestHighLevelClient
  • 创建XxxRequest。XXX是Index、Get、Update、Delete
  • 准备参数(Index和Update时需要)
  • 发送请求。调用RestHighLevelClient#.xxx()方法,xxx是index、get、update、delete
  • 解析结果(Get时需要)
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/784714
推荐阅读
相关标签
  

闽ICP备14008679号