ElasticSearch学习

ElasticSearch学习

1 安装运行

ElasticSearch是开箱即用,下载之后直接运行ElasticSearch.bat文件,默认端口是9200:,运行,然后打开请求9200端口,出现以下的描述说明安装成功:
在这里插入图片描述

2 前端展示:

ElasticSearch 需要前端界面进行展示交互:
需要先安装NodeJS:
https://github.com/mobz/elasticsearch-head,在这里我用的是git进行安装

git clone git://github.com/mobz/elasticsearch-head.git
cd elasticsearch-head
npm install
npm run start

然后打开 http://localhost:9100/
在这里插入图片描述
然后要解决跨域问题!

http.cors.enabled: true
http.cors.allow-origin: /.*/

这样子就可以解决跨域问题了

ES的基本操作,

这里借用狂神的笔记截图,上面不同的请求方法提供了不同的操作
在这里插入图片描述

ES put创建新索引:
PUT /zlx/testindex/textDocument
{
  "name":"zlx",
  "usedFor":"test",
  "id": "1"
}

在这里插入图片描述
实际上上面的Kibana的作用是在测试的时候写ES的测试数据用的。重点是在SpringBoot 上集成。要说到集成,就需要先配置依赖环境:

SpringBoot ElasticSearch 依赖环境
 <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.76</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

接下来就是快乐的CRUD以及批量插入和搜索:

增加:
 @Test
    void CreateIndex() throws IOException {
        CreateIndexRequest createIndexRequest=new CreateIndexRequest("zlx_index");
        CreateIndexResponse createIndexResponse=client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
        System.out.println(createIndexResponse);
    }

@Test
    void TestADDDocument() throws IOException {
        User user=new User();
        user.setName("zlx");
        user.setAge(25);
        IndexRequest request = new IndexRequest("zlx_index");
        request.id("1");
        request.timeout(TimeValue.timeValueSeconds(2));
        request.source(JSON.toJSONString(user), XContentType.JSON);
        //客户端发送请求
        IndexResponse response= client.index(request,RequestOptions.DEFAULT);
        System.out.println(response.toString());
        System.out.println(response.status());
    }
删除
oid testDeleteIndex() throws IOException {
        DeleteIndexRequest deleteIndexRequest =new DeleteIndexRequest("zlx_index");
        AcknowledgedResponse response=client.indices().delete(deleteIndexRequest,RequestOptions.DEFAULT);
        System.out.println(response.isAcknowledged());
    }

    public boolean deleteDoc(String index,String id) throws IOException {
        DeleteRequest request = new DeleteRequest(index,id);
        //timeout
        request.timeout(TimeValue.timeValueSeconds(1));
        request.timeout("1s");
        DeleteResponse deleteResponse = client.delete(
                request, RequestOptions.DEFAULT);
        return deleteResponse.status()== RestStatus.OK;
    }
修改
   @Test
    void updateMessage() throws IOException {
        UpdateRequest updateRequest =new UpdateRequest("zlx_index","1");
        updateRequest.timeout("1s");
        User user =new User(18,"zlx_update");
        updateRequest.doc(user,XContentType.JSON);
        UpdateResponse response=client.update(updateRequest,RequestOptions.DEFAULT);
        System.out.println(response.toString());

    }
在这里插入代码片
void GetRequest(){
        GetRequest getRequest = new GetRequest("zlx_index", "1");
        //只判断索引是否存在不需要获取_source
        getRequest.fetchSourceContext(new FetchSourceContext(false));
        getRequest.storedFields("_none_");
        boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);
        //System.out.println();
    }
   void ShowMessage() throws IOException {
        GetRequest request = new GetRequest("zlx_index","1");
        GetResponse getResponse = client.get(request,
                RequestOptions.DEFAULT);
        System.out.println(getResponse.toString());

    }

@Test
public void Search() throws IOException {

    SearchRequest searchRequest = new SearchRequest("zlx_index");

    SearchSourceBuilder searchSourceBuilder =new SearchSourceBuilder();

    TermQueryBuilder termQueryBuilder=new TermQueryBuilder("name","zlx");

    searchSourceBuilder.query(termQueryBuilder);

    searchSourceBuilder.timeout(TimeValue.timeValueNanos(60));

    searchRequest.source(searchSourceBuilder);
    SearchResponse searchResponse =client.search(searchRequest,RequestOptions.DEFAULT);
    System.out.println(JSON.toJSONString(searchResponse.getHits()));
    System.out.println("-------------------------------------------");
    for (SearchHit docu:searchResponse.getHits().getHits())
    {
        System.out.println(docu.getSourceAsMap());
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值