ElasticSearch入门

1.概述

Elasticsearch是一个基于Lucene(java)的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口,区别于solr的webservice。

2.安装、启动

jdk1.8(最低)、ElasticSearch客户端、界面工具(Kibana)

2.1 ES客户端

官网地址 ​​https://www.elastic.co/cn/downloads/​

  • windows下 解压即可使用
  • 启动 elasticsearch-7.7.0\bin\elasticsearch.bat 访问地址 localhost:9200
#跨域配置  elasticsearch-7.7.0\config\elasticsearch.yml  增加如下代码
http.cors.enabled: true
http.cors.allow-origin: "*"


2.2 ES可视化工具 ES Head

官网地址 ​​https://github.com/mobz/elasticsearch-head​

#启动需要nodejs环境 启动命令    访问地址  localhost:9100
cd D:\sde\elasticsearch\elasticsearch-head-master
npm install
npm run start


2.3 ES可视化工具 Kibana

官网地址 ​​https://www.elastic.co/cn/downloads/​

  • windows下 解压即可使用
  • 启动 kibana-7.7.1-windows-x86_64\bin 访问地址 localhost:5601
#汉化 kibana-7.7.1-windows-x86_64\config\kibana.yml   增加如下代码
i18n.locale: "zh-CN"


3. IK分词器

官网地址 ​​https://github.com/medcl/elasticsearch-analysis-ik/releases​

  • 解压到 D:\sde\elasticsearch\elasticsearch-7.7.0\plugins\analysis-ik 目录没有新建analysis-ik 目录名称不重要,随意起
  • 检查是否加载分词器
cd D:\sde\elasticsearch\elasticsearch-7.7.0\bin
elasticsearch-plugin list


  • 算法 ik_smart(最少切分) ik_max_word(最细粒度切分) 及用kibana校验 默认keywork standard
GET _analyze
{
"analyzer": "ik_max_word",
"text": ["美国民主党"]
}
GET _analyze
{
"analyzer": "ik_smart",
"text": ["美国民主党"]
}


  • 扩充字典(人名、网络流行语)
<!--elasticsearch-7.7.0\plugins\analysis-ik\config\IKAnalyzer.cfg.xml   可定义自己的字典文件.dic 后缀-->
<entry key="ext_dict">xxx.dic</entry>


4. Kibana 基本语法 API

method

url

desc

PUT

localhost:9200/索引名称/类型名称/文档id

创建文档(指定文档id)

POST

localhost:9200/索引名称/类型名称

创建文档(随机id)

POST

localhost:9200/索引名称/_update/文档id

修改文档

DELETE

localhost:9200/索引名称/类型名称/文档id

删除文档 by id

GET

localhost:9200/索引名称/类型名称/文档id

查询文档 by id

POST

localhost:9200/索引名称/_search

查询所有文档

4.1 新建

#创建空库
PUT /test2
{

}
#创建索引  规定字段类型
PUT /test3
{
"mappings": {
"properties": {
"name":{
"type": "text"
},
"age":{
"type": "integer"
},
"birth":{
"type": "date"
}
}
}
}
#创建数据
PUT /wanghl/_doc/2
{
"name":"花木兰",
"age":67,
"tags":["战士","上单","女"]
}


4.2 删除

#删除索引
DELETE test2
#删除文档
DELETE test1/_doc/3


4.3修改

#修改文档
POST /test1/_update/4
{
"doc":{
"name":"红桃A"
}
}


4.4 查询

#获取索引库
GET test3
#获取文档by文档id
GET wanghl/_doc/2
#根据属性查询  简写
GET wanghl/_search?q=name:
#构建式查询   
#_source 字段过滤 不写默认 select *
# from size 分页
GET /wanghl/_search
{
"query": {
"match": {
"tags": "男"
}
},
"_source": ["name","tags"],
"from":0,
"size":1
}
#多条件查询   must 相当于 and     should 相当于 or     
GET wanghl/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"tags": "男 下路"
}
}
],
"must_not": [
{
"match": {
"age": "3"
}
}
]
}
}
}
# 查询过滤  +  高亮显示
GET wanghl/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"tags": "男"
}
}
] ,
"filter": [
{
"range": {
"age": {
"gte": 10,
"lte": 200
}
}
}
]
}
},
"highlight": {
"pre_tags": "<font>",
"post_tags": "</font>",
"fields": {
"tags": {}
}
}
}


5 SpringBoot集成 ES

  1. pom文件添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>


  1. 注册ES client对象
package com.nesc.esapi.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* 定义高版本ES实例对象
* @author wanghl
* @date 2020/7/17 9:23
**/
@Configuration
public class ElasticSearchClientConfig {

@Bean
public RestHighLevelClient restHighLevelClient() {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("127.0.0.1", 9200, "http")
)
);
return client;
}
}


  1. Junit测试代码
package com.nesc.esapi;

import com.alibaba.fastjson.JSONObject;
import com.nesc.esapi.domain.User;
import lombok.SneakyThrows;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.ArrayList;

/**
* ES client api测试
*/
@SpringBootTest
class EsApiApplicationTests {

@Autowired
RestHighLevelClient restHighLevelClient;

/**
* 新建索引
* @throws Exception
*/
@Test
void testCreateIndex() throws IOException {
//创建请求
CreateIndexRequest request = new CreateIndexRequest("testapi");
CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request,RequestOptions.DEFAULT);
}

/**
* 查询索引
* @throws IOException
*/
@Test
void testExistIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest("testapi");
boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}

/**
* 删除索引
* @throws IOException
*/
@Test
void testDeleteIndex() throws IOException {
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("testapi");
AcknowledgedResponse delete = restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
System.out.println(delete);
}

/**
* 创建文档
* @throws IOException
*/
@Test
void testCreateDocument() throws IOException {
IndexRequest indexRequest = new IndexRequest("testapi");
User user = new User("张飞","射手");
IndexRequest source = indexRequest.source(JSONObject.toJSONString(user), XContentType.JSON);
IndexResponse index = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
System.out.println(index.toString());
}

/**
* 文档是否存在
* @throws IOException
*/
@Test
void testExistDocument() throws IOException {
//testapi 索引中 是否存在 1 的文档
GetRequest getRequest = new GetRequest("testapi", "1");
boolean exists = restHighLevelClient.exists(getRequest, RequestOptions.DEFAULT);
System.out.println(exists);
}

/**
* 获取文档信息
* @throws IOException
*/
@Test
void testGetDocument() throws IOException {
GetRequest getRequest = new GetRequest("testapi", "gBd0W3MBYL0QvcF5Z9tv");
GetResponse documentFields = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
System.out.println(documentFields.getSource());
}

/**
* 获取文档信息
* @throws IOException
*/
@Test
void testUpdatDocument() throws IOException {
UpdateRequest updateRequest = new UpdateRequest("testapi", "jxeBW3MBYL0QvcF5idvD");
User user = new User("张飞","坦克");
updateRequest.doc(JSONObject.toJSONString(user),XContentType.JSON);
UpdateResponse update = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
System.out.println(update.status());
}

/**
* 删除文档信息
* @throws IOException
*/
@Test
void testDeleteDocument() throws IOException {
DeleteRequest deleteRequest = new DeleteRequest("testapi", "jxeBW3MBYL0QvcF5idvD");
DeleteResponse delete = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println(delete.status());
}

/**
* 查询文档
*/
@Test
void testSearchDocument() throws IOException {
SearchRequest searchRequest = new SearchRequest("testapi");
//匹配字段
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("username", "李白");
//构建查询器
searchRequest.source(new SearchSourceBuilder().query(matchQueryBuilder));
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(searchResponse.getHits().getTotalHits());
}
}