java curriculum design of --Elasticsearch articles

1. Course Design Team blog link

2. The individual responsible for the module or mission statement

2.1Elasticsearch Profile

Elasticsearch is written in Java, an open source search engine that uses Luence do indexing and searching within, through the package of Lucene, providing a simple and consistent RESTful API. Elasticsearch is a distributed search architecture that can easily be extended to hundreds of service nodes, and supports PB-level data query, the system has high availability and high concurrency.

2.2 personal mission statement

Task: use Elasticsearch retrieval, the need to achieve the following functions:

  • Importing data indexing library (including the use of ik word is Chinese word segmentation)
  • Save history search (for search autocomplete)
  • Search auto-complete (using completion suggester implementation, completion proposal from priority access to the search history, and then get recommendations from title index (title) in the period when an insufficient number)
  • Text retrieval (the first word is required for use ik word search, and then later retrieved according to the obtained word entry, search results pages need to be returned)

3. Record your own code to submit screenshots

github project Address: https://github.com/xingkyh/searchEngine






4. responsible for their own module or task details

Elasticsearch modules:

  • EsClient class: used to link elasticsearch
  • EsCreatIndex class: create an index, insert data index
  • EsSearch class: for performing a full text search request and return a search result
  • EsSuggest class: used to get search suggestions for auto-completion in search

(I will explain the following part of the code posted, please see the complete code on my github)

4.1EsClient class

4.1.1 Properties

  • indexName: Name Index
  • typeName: field name in the index (elasticsearch6.0 and above each index only allows one type)
  • Search for the name of preservation of historical library: suggestName
  • jestClient与transportClient:API连接elasticsearch之后用于操作的对象

4.1.2jest API连接elasticsearch

4.1.3transport API连接elasticsearch

4.2EsCreatIndex类

4.2.1索引的创建与mapping的写入



mapping关键属性:

  • type:字段的类型,在本次课设中字符串为text(旧版本为string),日期为data,设置为completion则代表此字段将用于搜索时自动补全(但也会导致此字段无法进行检索,若想此字段仍可用于检索,请在此字段中添加一个附加字段并将附加字段属性设置为completion,原字段属性设置为text,获取自动补全建议时使用附加字段)
  • analyzer:生成倒排索引时使用的分词方式,一般使用ik_max_word(最细拆分)
  • search_analyzer:搜索时,对搜索请求使用的分词器,一般使用ik_smart(最粗拆分)

对于elasticsearch来说maping就相当与mysql的表结构,elasticsearch内部是使用json文档来存储数据的,导入数据时elasticsearch会根据mapping来生成json文档来保存数据(若无mapping或mapping中无对应字段则elasticsearch会判断导入的数据类型并自动生成mapping),建立索引库和搜索时使用的分词方式也是在mapping进行设置的,可以说索引的建立的关键就在于mapping的设计与写入。
(在学elasticsearch时,最让我头疼最久的就是这个mapping)

4.2.2插入数据

单独插入

批量插入

4.2.3删除索引

4.3EsSearch类

4.3.1全文检索

不带日期限制的检索


带日期限制的检索

4.3.2保存历史搜索


由于历史搜索中可能存在相同的搜索,若不经处理就直接保存可能会导致自动补全时出现重复的建议,所以在此处插入数据的时候需要手动指定id,由于elasticsearch内部是使用唯一id来标识文档的,当插入的文档的id已经存在时,文档就不会插入,而是会更新原id标识的文档,因此我在此处使用了MD5来加密字符串生成一个唯一的32位id,当数据相同时生成的id也会相同,这样就不会有数据重复的问题了。(不过MD5加密字符串仍有概率会生成一样的32位散列,但这个概率极小,基本可以忽略不记)

4.4EsSuggest类

4.4.1返回搜索建议


搜索建议优先从历史搜索中获取,当建议不足10条时会从索引的标题(title)字段中获取其余的建议,以下代码是将获取建议的请求提交到elasticsearch的具体操作

ps:这一部分我是使用teansport API来写的,其它部分则是jest API

5.课程设计感想

从刚开始接触elasticsearch到完成这一部分代码断断续续差不多花了我一个月左右的时间(其实大部分时间都用来处理其它事情,比如复习准备考试之类的,真正用来打代码的时间并不多),最开始的时候,我从网上搜索java操作elasticsearch时,我便了解到了存在多种java操作elasticsearch的API,每种API的具体使用方式都有一定的区别,于是,经过一番对比之后我决定使用jest API来编写代码,但刚开始编写我便遇上了让我最为头疼的一块--mapping,为了能设计好mapping我在网上参考了很多资料,但大部分资料讲的都是直接使用Restful来直接操作的,其设计的mapping并不能直接用于jest操作使用,经过了一番努力,在我大致理解mapping中各个字段的意义之后,我终于设计好了mapping,但也因此又遇到了一个问题--mapping无法写入,为此我又到处去查找资料,最终在查看了请求的错误信息后我发现了是elasticsearch新版本新增的一个字段所导致的问题,由于我不知如何设置这个问题,所以只好将elasticsearch的版本由7.4.2更换为6.8.5才解决了这个问题,之后代码一直顺利写到了全文检索,但当我开始写自动补全时,又出现了一个问题,不知是我使用的jest的版本问题还是什么,其缺少了一个类,导致了我无法使用jest来编写自动补全这一部分的代码,所以我迫不得已只好使用teansport API来编写这一部分代码,但又由于我一开始设计mapping时没有考虑到这一部分功能,所以只好回去重写mapping,但重写之后我发现用于获取搜索建议的字段无法进行检索,为此我又去查找资料,最终我了解到了附加字段之后,在保持原字段不变利用附加字段来获取建议之后我才解决了这个问题,至此我才终于完成了这一模块的代码。

Guess you like

Origin www.cnblogs.com/xycm/p/12168554.html