使用Java操作es的研究

本文详述了使用Java操作Elasticsearch的过程,包括环境搭建、索引库的CRUD操作,以及搜索相关的各种查询方式,如精确查询、全文检索、多字段匹配和布尔查询。还介绍了过滤器、排序和高亮显示等高级特性。
摘要由CSDN通过智能技术生成

环境搭建部分

第一步:导入maven依赖

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch‐rest‐high‐level‐client</artifactId>
    <version>6.2.1</version>
</dependency>
<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>6.2.1</version>
</dependency>

第二步:由于项目中使用boot搭建,so开始编写配置文件application.yml

server:
  port: ${port:40100}
spring:
  application:
    name: xc‐search‐service
xuecheng:
  elasticsearch:
    hostlist: ${eshostlist:127.0.0.1:9200} #多个结点中间用逗号分隔

Boot的环境搭建就省略不写了,主要是API的使用以及es的配置信息

第三步:编写es配置类(es是restful风格接口,且可以访问多个集群节点)

package com.xuecheng.search.config;

import org.apache.http.HttpHost;
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;

/**
 * @author weilai
 * @version 1.0
 * @create 2018-07-08 8:53
 **/
@Configuration
public class ElasticsearchConfig {
    //配置了es的ip和端口,多个地址中间以逗号分隔
    @Value("${xuecheng.elasticsearch.hostlist}")
    private String hostlist;

    @Bean
    public RestHighLevelClient restHighLevelClient(){

        String[] hostlistarry = hostlist.split(",");
        HttpHost[] httpHosts  =new HttpHost[hostlistarry.length];
        int i=0;
        for(String host:hostlistarry){
            String[] server = host.split(":");
            HttpHost httpHost = new HttpHost(server[0],Integer.parseInt(server[1]));
            httpHosts[i] = httpHost;
            i++;
        }

        RestHighLevelClient restHighLevelClient= new RestHighLevelClient(RestClient.builder(httpHosts));
        return restHighLevelClient;

    }
}

编写启动引导类(博客中忽略...)

环境搭建完成....开始索引库CRUD的操作

索引库CRUD

编写springBoot测试类,开始CRUD操作

创建索引库:

原始API操作:

 put请求  http://localhost:9200/索引库名称 /类型名称/_mapping

例如:创建类型为xc_course的映射,共包括三个字段:name、description、studymondel

请求:http://localhost:9200/xc_course/doc/_mapping

请求体:

{
  "properties": {   
           "name": {
              "type": "text",
              "analyzer":"ik_max_word",
              "search_analyzer":"ik_smart"
           },
           "description": {
              "type": "text",
              "analyzer":"ik_max_word",
              "search_analyzer":"ik_smart"
           },
           "studymodel": {
              "type": "keyword"
           },
           "price": {
              "type": "float"
           },
           "timestamp": {
           "type":   "date",      
           "format": "yyyy‐MM‐dd HH:mm:ss||yyyy‐MM‐dd||epoch_millis"      
         }    
        }
}

JAVA操作的方式为

 @Test
    public void testCreateIndex() throws IOException {
        //用于创建索引库的对象,指定索引库名称
        CreateIndexRequest createIndexRequest = new CreateIndexRequest("xc_a");

        //设置索引库的分片数量及副本的数量
        createIndexRequest.settings(
                Settings.builder().put("number_of_shards",1)//索引库的分片数量
                .put("number_of_replicas",0));
        //参数:String type, String source, XContentType xContentType
        String mapping_source = " {\n" +
                " \t\"properties\": {\n" +
                "           \"name\": {\n" +
                "              \"type\": \"text\",\n" +
                "              \"analyzer\":\"ik_max_word\",\n" +
                "              \"search_analyzer\":\"ik_smart\"\n" +
                "           },\n" +
                "           \"description\": {\n" +
                "              \"type\": \"text\",\n" +
                "              \"analyzer\":\"ik_max_word\",\n" +
                "              \"search_analyzer\":\"ik_smart\"\n" +
                "           },\n" +
                "           \"studymodel\": {\n" +
                "              \"type\": \"keyword\"\n" +
                "           },\n" +
                "           \"price\": {\n" +
                "              \"type\": \"float\"\n" +
                "           },\n" +
                "           \"timestamp\": {\n" +
                "          \t\t\"type\":   \"date\",\n" +
                "          \t\t\"format\": \"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis\"\n" +
                "        \t}\n" +
                "        }\n" +
                "}";
        createIndexRequest.mapping("doc",mapping_source, XContentType.JSON);

        //创建IndicesClient对象向ES提交请求
        IndicesClient indices = client.indices();
        //创建索引库
        CreateIndexResponse  createIndexResponse= indices.create(createIndexRequest);
        //得到响应的结果
        boolean acknowledged = createIndexResponse.isAcknowledged();
        System.out.println(acknowledged);

    }

  

添加文档

原始API操作:

put http://localhost:9200/xc_course/doc/3/

请求体:

{
 "name":"spring cloud实战",
 "description":"本课程主要从四个章节进行讲解: 1.微服务架构入门 2.spring cloud 基础入门 3.实战SpringBoot 4.注册中心eureka。",
 "studymodel":"201001"
 "price":5.6
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值