java连接elasticsearch的两种方式

阅读数:319 评论数:0

跳转到新版页面

分类

python/Java

正文

一、简介

Java操作ES的Client有两种:TransportClient和Rest Client,但是ES7开始弃用TransportClient,故在实际使用过程中建议使用Java高级REST Client。

1、Rest client两种风格

(1)Low Level REST client

将请求编排和响应编排留给用户自己处理,它兼容所有的es版本。

(2)High Level Rest client

基于level rest client。

二、TransportClient连接ES

1、pom

      <!-- TransportClient 依赖包-->
<dependency>
  <groupId>org.elasticsearch.client</groupId>
  <artifactId>transport</artifactId>
  <version>7.6.2</version>
</dependency>

实际上transport中已经包含了elasticsearch依赖,不需要再次添加。

    <dependency>
      <groupId>org.elasticsearch</groupId>
      <artifactId>elasticsearch</artifactId>
      <version>7.6.2</version>
      <scope>compile</scope>
    </dependency>

2、建立es集群连接

package cn.zsm.es;
 
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Before;
import org.junit.Test;
 
import java.net.InetAddress;
import java.net.UnknownHostException;
 
public class JavaEsTest {
 
    private String IP;
    private int PORT;
 
    @Before
    public void init(){
        this.IP = "192.168.?.?";
        this.PORT = 9300;
    }
    
    @Test
    public void esClient(){
        try {
            Settings settings = Settings.builder().put("cluster.name", "my-application").build();
            TransportClient client = new PreBuiltTransportClient(settings)
                    .addTransportAddresses(new TransportAddress(InetAddress.getByName(IP), PORT));
            System.out.println(client.toString());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
 
}

三、RestClient连接ES

1、pom

        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.6.2</version>
        </dependency>

这里使用High Level Client,它已经包含了elasticsearch-rest-client,所以不需要再次添加。

        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
            <version>7.6.2</version>
        </dependency>

2、建立连接

    @Test
    public void highClient(){
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost(IP, PORT, "http")));
        System.out.println(client.toString());
    }

如果我们要连接集群中多个es节点时,只需要在restclient的builder方法中添加多个HttpPost对象即可。

    public static RestClientBuilder builder(HttpHost... hosts) {
        return new RestClientBuilder(hosts);
    }

ava配置多个ES节点时,请求的时候会随机选一个节点作为协调节点负责分发请求和处理结果,所以Java链接ES节点数量的多少,不会影响到Java请求ES查询结果的速度。

四、查询条件QueryBuilder的常见构建方法

1、精确查询(必须完全匹配上)

单个匹配termQuery

//不分词查询 参数1: 字段名,参数2:字段查询值,因为不分词,所以汉字只能查询一个字,英语是一个单词.
QueryBuilder queryBuilder=QueryBuilders.termQuery("fieldName", "fieldlValue");
//分词查询,采用默认的分词器
QueryBuilder queryBuilder2 = QueryBuilders.matchQuery("fieldName", "fieldlValue");

多个匹配
//不分词查询,参数1: 字段名,参数2:多个字段查询值,因为不分词,所以汉字只能查询一个字,英语是一个单词.
QueryBuilder queryBuilder=QueryBuilders.termsQuery("fieldName", "fieldlValue1","fieldlValue2...");
//分词查询,采用默认的分词器
QueryBuilder queryBuilder= QueryBuilders.multiMatchQuery("fieldlValue", "fieldName1", "fieldName2", "fieldName3");
//匹配所有文件,相当于就没有设置查询条件
QueryBuilder queryBuilder=QueryBuilders.matchAllQuery();

2、模糊查询(只要包含即可)

 //模糊查询常见的5个方法如下
        //1.常用的字符串查询
        QueryBuilders.queryStringQuery("fieldValue").field("fieldName");//左右模糊
        //2.常用的用于推荐相似内容的查询
        QueryBuilders.moreLikeThisQuery(new String[] {"fieldName"}).addLikeText("pipeidhua");//如果不指定filedName,则默认全部,常用在相似内容的推荐上
        //3.前缀查询  如果字段没分词,就匹配整个字段前缀
        QueryBuilders.prefixQuery("fieldName","fieldValue");
        //4.fuzzy query:分词模糊查询,通过增加fuzziness模糊属性来查询,如能够匹配hotelName为tel前或后加一个字母的文档,fuzziness 的含义是检索的term 前后增加或减少n个单词的匹配查询
        QueryBuilders.fuzzyQuery("hotelName", "tel").fuzziness(Fuzziness.ONE);
        //5.wildcard query:通配符查询,支持* 任意字符串;?任意一个字符
        QueryBuilders.wildcardQuery("fieldName","ctr*");//前面是fieldname,后面是带匹配字符的字符串
        QueryBuilders.wildcardQuery("fieldName","c?r?");

3、范围查询

        //闭区间查询
        QueryBuilder queryBuilder0 = QueryBuilders.rangeQuery("fieldName").from("fieldValue1").to("fieldValue2");
        //开区间查询
        QueryBuilder queryBuilder1 = QueryBuilders.rangeQuery("fieldName").from("fieldValue1").to("fieldValue2").includeUpper(false).includeLower(false);//默认是true,也就是包含
        //大于
        QueryBuilder queryBuilder2 = QueryBuilders.rangeQuery("fieldName").gt("fieldValue");
        //大于等于
        QueryBuilder queryBuilder3 = QueryBuilders.rangeQuery("fieldName").gte("fieldValue");
        //小于
        QueryBuilder queryBuilder4 = QueryBuilders.rangeQuery("fieldName").lt("fieldValue");
        //小于等于
        QueryBuilder queryBuilder5 = QueryBuilders.rangeQuery("fieldName").lte("fieldValue");

4、组合查询、多条件查询、布尔查询

QueryBuilders.boolQuery()
QueryBuilders.boolQuery().must();//文档必须完全匹配条件,相当于and
QueryBuilders.boolQuery().mustNot();//文档必须不匹配条件,相当于not
QueryBuilders.boolQuery().should();//至少满足一个条件,这个文档就符合should,相当于or

五、创建索引时指定settting和mapping

@Override
    public Boolean createIndex(String index) {
        XContentBuilder setting = packageSetting();
        XContentBuilder mapping = packageMapping();
        return createIndexSetting(index,setting,mapping);
    }

   private XContentBuilder packageMapping(){
        XContentBuilder mapping = null;
        try {
            //创建索引Mapping
            mapping = XContentFactory.jsonBuilder()
                    .startObject()
                    .field("dynamic", true)
                    .startObject("properties")
                    //id
                    .startObject("id")
                    .field("type", "long")
                    .field("index", false)
                    .endObject()
                    //账号:keyword忽略大小写
                    .startObject("markId")
                    .field("type", "keyword")
                    .field("normalizer", "lowercase")
                    .endObject()
                    //昵称:模糊搜索、忽略大小写
                    .startObject("nickName")
                    .field("type", "text")
                    .field("analyzer", "ngram")
                    .endObject()
                    //头像
                    .startObject("iconUrl")
                    .field("type", "text")
                    .field("index", false)
                    .endObject()
                    //性别
                    .startObject("sex")
                    .field("type", "keyword")
                    .endObject()
                    .startObject("mobile")
                    .field("type", "keyword")
                    .endObject()
                    //经纬度
                    .startObject("location")
                    .field("type", "geo_point")
                    .endObject()
                    //地址
                    .startObject("address")
                    .field("type", "text")
                    .endObject()
                    .startObject("openMobile")
                    .field("type", "keyword")
                    .endObject()
                    //
                    .startObject("birthday")
                    .field("type", "date")
                    .field("format","yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis")
                    .endObject()
                    //
                    .startObject("createTime")
                    .field("type", "date")
                    .field("format","yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis")
                    .endObject()
                    .endObject()
                    .endObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mapping;
    }

/**
     * ngram分词器配置
     * ngram:英文单词按字母分词
     * field("filter","lowercase"):大小写兼容搜索
     * index.max_ngram_diff: 允许min_gram、max_gram的差值
     * https://www.elastic.co/guide/en/elasticsearch/reference/6.8/analysis-ngram-tokenizer.html
     * normalizer:解决keyword区分大小写
     * https://www.elastic.co/guide/en/elasticsearch/reference/6.0/normalizer.html
     * @return
     */
    private XContentBuilder packageSetting() {
        XContentBuilder setting = null;
        try {
            //创建索引setting
            setting = XContentFactory.jsonBuilder()
                    .startObject()
                        .field("index.max_ngram_diff","5")
                        .startObject("analysis")
                            .startObject("analyzer")
                                .startObject("ngram")
                                    .field("tokenizer","my_tokenizer")
                                    .field("filter","lowercase")
                                .endObject()
                            .endObject()
                            .startObject("tokenizer")
                                .startObject("my_tokenizer")
                                    .field("type","ngram")
                                    .field("min_gram","1")
                                    .field("max_gram","3")
                                .endObject()
                            .endObject()
                            .startObject("normalizer")
                                .startObject("lowercase")
                                    .field("type","custom")
                                    .field("filter","lowercase")
                                .endObject()
                            .endObject()
                        .endObject()
                    .endObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return setting;
    }


  protected Boolean createIndexSetting(String indexName, XContentBuilder settings,XContentBuilder mapping) {
        Boolean is = false;
        try {
            CreateIndexRequest request = buildCreateIndexRequest(indexName);
            if (settings != null) {
                request.settings(settings);
            }
            if (mapping != null) {
                request.mapping(mapping);
            }
            //获取索引客户端
            IndicesClient indices = client.indices();
            //创建索引
            CreateIndexResponse response = indices.create(request, COMMON_OPTIONS);
            log.info("是否所有节点都已确认请求: " + response.isAcknowledged());
            log.info("指示是否在超时之前为索引中的每个分片启动了必要数量的分片副本: " + response.isShardsAcknowledged());
            is = response.isAcknowledged();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return is;
    }

对应的json格式

PUT member
{
  "settings": {
    "index.max_ngram_diff":"5"
    "analysis": {
      "analyzer": {
        "ngram": {
          "tokenizer": "my_tokenizer",
          "filter": "lowercase"
        }
      },
      "tokenizer": {
        "my_tokenizer": {
          "type": "ngram",
          "min_gram": 1,
          "max_gram": 3
        }
      }
    }
  }
}

 




相关推荐