Elasticsearch快速入门

Elasticsearch快速入门,第1张

Elasticsearch快速入门

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引入依赖

    org.springframework.boot
    spring-boot-starter-data-elasticsearch

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");
        //设置文档id
        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");
        // 不获取返回的 _source的上下文了
        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 {
}

测试

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

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

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

all = pdao.findAll(pageRequest); for (P p : all) { System.out.println(p); } } }



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5652814.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存