1安装(阿里云服务器centos7,docker)
在云服务器上安装比较方便安装和部署,但是其稳定性不佳,经常请求失败。所以学习过程中不用急着去找bug,很多时候多试几次就成功了。
1elasticsearch
拉取镜像
docker pull elasticsearch:7.2.0 建议加上版本号
启动容器
docker run -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d --name es elasticsearch:7.2.0
-e ES_JAVA_OPTS="-Xms256m -Xmx256m" 指定运行内存,默认是4G,不指定服务器会崩
--name es 指定容器名称
elasticsearch:7.2.0 镜像
-e "discovery.type=single-node" 单节点
windows访问测试 浏览器输入 服务器ip:9200,出现以下画面则访问成功
2elasticsearch-head
eshead是管理Elasticsearch的可视化界面工具 windows下载 https://github.com/mobz/elasticsearch-head 解压,进入当前目录命令行
由于会出现跨域问题,这里先到服务器中,进入elasticsearch容器,修改配置文件
这里的es是容器名称
docker exec -it es /bin/bash
修改配置文件
在后面加上
到这里,解决跨域问题,回到windows下操作 安装依赖(需要安装node)
启动 image.png 访问http://localhost:9100/ 输入服务器ip地址:9200即可连接
3postman
浏览器无法发送POST,PUT等restful风格的请求,postman可以,并且可以以JSON格式发送数据 下载:https://www.postman.com/downloads/
4中文分词器
用来将一句中文分词多个词语 下载:https://github.com/medcl/elasticsearch-analysis-ik/releases 注意下载zip格式的,其他格式会出错 一定要下载与elasticsearch完全对应的版本比如,elasticsearch7.2.0就必须下载elastic 在服务器上新建一个文件夹ik,必须是ik,将下载好的zip文件上传到该文件夹,解压 upzip 文件名。 将文件夹复制到elasticsearch容器内, /usr/share/elasticsearch/plugins/ik/是固定的
docker cp /data/elk/es/ik es:/usr/share/elasticsearch/plugins/ik/
2结构
类型新版本逐渐废弃这个概念,只有一个类型_doc
3索引操作
PUT /name 创建索引 GET /name 获取索引 DELETE /name 删除索引 GET /_cat/indices?v获取所有索引
4文档操作
1创建文档/修改文档(指定id的方式) PUT/POST
http://localhost:9200/test/_doc/101/
2查找文档 GET
http://localhost:9200/test/_doc/101/
3删除文档 DELETE
http://localhost:9200/test/_doc/102
5springboot整合ES
1引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
2配置文件
spring.elasticsearch.rest.uris=localhost
3配置类
package com.csp.es;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ElasticSearchClientConfig {
@Value("${spring.elasticsearch.rest.uris}")
private String urls;
public static final RequestOptions COMMON_OPTIONS;
static {
RequestOptions.Builder builder=RequestOptions.DEFAULT.toBuilder();
COMMON_OPTIONS=builder.build();
}
@Bean
public RestHighLevelClient restHighLevelClient() {
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
RestClient.builder(
new HttpHost(urls,9200,"http")
)
);
return restHighLevelClient;
}
}
4基础操作
方式1
package com.csp.es;
import com.alibaba.fastjson.JSON;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
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.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.junit.jupiter.api.Test;
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;
@SpringBootTest
@RunWith(SpringRunner.class)
class EsApplicationTests {
@Test
void contextLoads() {
}
@Autowired
private RestHighLevelClient restHighLevelClient;
@Test
public void testCreateIndex1() throws IOException {
CreateIndexRequest createIndexRequest = new CreateIndexRequest("person");
CreateIndexResponse response = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
System.out.println(response);
}
@Test
public void existIndex() throws IOException {
GetIndexRequest getIndexRequest = new GetIndexRequest("p");
boolean exists = restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
System.out.println(exists);
}
@Test
public void deleteIndex() throws IOException {
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("test");
AcknowledgedResponse delete = restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
System.out.println(delete.isAcknowledged());
}
@Test
public void addDocument() throws IOException {
Person person=new Person("zhangsan cao",18);
IndexRequest indexRequest=new IndexRequest("person");
indexRequest.id("4");
indexRequest.source(JSON.toJSONString(person),XContentType.JSON);
IndexResponse index = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
System.out.println(index);
}
@Test
public void GetDocument() throws IOException {
GetRequest getRequest = new GetRequest("person", "1");
GetResponse documentFields = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
System.out.println(documentFields.getSourceAsString());
}
@Test
public void existDocument()throws IOException{
GetRequest request = new GetRequest("person", "1");
request.fetchSourceContext(new FetchSourceContext(false));
request.storedFields("_none_");
boolean exists = restHighLevelClient.exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
@Test
public void deleteDocument() throws IOException{
DeleteRequest person = new DeleteRequest("person", "1");
DeleteResponse res = restHighLevelClient.delete(person, RequestOptions.DEFAULT);
System.out.println(res.status());
}
@Test
public void updateDocument() throws IOException{
UpdateRequest person = new UpdateRequest("person", "1");
Person person1=new Person("lisi",18);
person.doc(JSON.toJSONString(person1),XContentType.JSON);
UpdateResponse update = restHighLevelClient.update(person, RequestOptions.DEFAULT);
System.out.println(update.status());
}
@Test
public void searchAll() throws IOException{
SearchRequest searchRequest=new SearchRequest("person");
SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = search.getHits();
System.out.println(JSON.toJSONString(hits));
for (SearchHit hit : hits.getHits()) {
System.out.println( hit.getSourceAsMap());
}
}
@Test
public void searchQuery() throws IOException{
SearchRequest searchRequest=new SearchRequest("person");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
TermQueryBuilder builder = QueryBuilders.termQuery("name", "zhangsan");
searchSourceBuilder.query(builder);
searchRequest.source(searchSourceBuilder);
SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = search.getHits();
System.out.println(JSON.toJSONString(hits));
for (SearchHit hit : hits.getHits()) {
System.out.println( hit.getSourceAsMap());
}
}
@AllArgsConstructor
@RequiredArgsConstructor
@Data
class Person{
private String name;
private int age;
}
方式2
注:该方法利用了es封装的一些方法,操作文档较为方便。 需要注意的一些问题: 1、需要通过maven进行clean,compile之后才能测试成功,不然会报无法创建dao的错误,这一点很坑,网上的很多方法都解决不了,我是无意点到了springboot启动类,才把这个问题解决 2、测试方法经常执行失败,不要着急找bug,可能是连接es失败,多试几次就成功了。 实体类
package com.csp.es;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
@Data
@Document(indexName = "p")
public class P {
@Id
private Integer id;
@Field(type = FieldType.Keyword)
private String name;
@Field(type = FieldType.Integer)
private int age;
}
dao类
package com.csp.es;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface PDao extends ElasticsearchRepository<P,Integer> {
}
测试
package com.csp.es;
import com.alibaba.fastjson.JSON;
import lombok.Data;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
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.common.xcontent.XContentType;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
@RunWith(SpringRunner.class)
class EsApplicationTests {
@Test
void contextLoads() {
}
@Test
public void createIndex(){
System.out.println("创建索引");
}
@Autowired
private PDao pdao;
@Test
public void save(){
P product = new P();
product.setId(1);
product.setName("c11");
product.setAge(181);
pdao.save(product);
}
@Test
public void findAll(){
Iterable<P> all = pdao.findAll();
for (P p : all) {
System.out.println(p);
}
}
@Test
public void findById(){
P p = pdao.findById(1).get();
System.out.println(p);
}
@Test
public void delete(){
pdao.deleteById(1);
}
@Test
public void addM(){
List<P> list=new ArrayList<>();
for(int i=0;i<10;i++){
P p=new P();
p.setId(i+3);
p.setName("name"+i+3);
p.setAge(i+3);
list.add(p);
}
pdao.saveAll(list);
}
@Test
public void pageSearch(){
Sort sort= Sort.by(Sort.Direction.DESC,"id");
int cur=1;
int limit=3;
PageRequest pageRequest=PageRequest.of(cur,limit,sort);
Page<P> all = pdao.findAll(pageRequest);
for (P p : all) {
System.out.println(p);
}
}
}
|