Transport Client 客户端的使用

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: Transport Client 客户端的使用

一、基本概念

这里描述操作elasticsearch采用TransportClient这种方式,官方明确表示在ES 7.0版本中将弃用TransportClient客户端,且在8.0版本中完全移除它.


整合springboot

properties配置

# es
spring.data.elasticsearch.repositories.enabled=true
spring.elasticsearch.rest.uris=192.168.60.181:9200
#spring.elasticsearch.rest.username=elastic
#spring.elasticsearch.rest.password=nE7jA%5m
# ???
#elasticsearch.cluster.name=${ELASTICSEARCH_NAME:tmxmall_cluster}
#elasticsearch.master.address=${ELASTICSEARCH_HOST:elasticsearch}
#elasticsearch.cluster.tcp-port=${ELASTICSEARCH_PORT:9300}


1)通过配置类

public class Config {
    @Bean("transportClient")
    public TransportClient genTransportClient(@Value("${elasticsearch.cluster.name}") String clusterName,
                                              @Value("${elasticsearch.master.address}") String masterAddress,
                                              @Value("${elasticsearch.cluster.tcp-port}") int tcpPort) throws UnknownHostException {
        System.setProperty("es.set.netty.runtime.available.processors", "false");
        Settings settings = Settings.builder()
                .put("cluster.name", clusterName)
                .put("client.transport.sniff", true)
                .build();
        return new PreBuiltTransportClient(settings)
                .addTransportAddress(new TransportAddress(InetAddress.getByName(masterAddress), tcpPort));
    }
}


2)使用@PostConstruct注解

如果整合其他客户端时使用这种方式

    private static TransportClient transportClient;
    private static String clusterName;
    private static String masterAddress;
    private static int tcpPort;
    @Value("${elasticsearch.cluster.name}")
    public void setClusterName(String clusterName) {
        ESDAO.clusterName = clusterName;
    }
    @Value("${elasticsearch.master.address}")
    public void setMasterAddress(String masterAddress) {
        ESDAO.masterAddress = masterAddress;
    }
    @Value("${elasticsearch.cluster.tcp-port}")
    public void setTcpPort(int tcpPort) {
        ESDAO.tcpPort = tcpPort;
    }
    @PostConstruct
    public void init() {
        System.setProperty("es.set.netty.runtime.available.processors", "false");
        Settings settings = Settings.builder()
                .put("cluster.name", clusterName)
                .put("client.transport.sniff", true)
                .build();
        try {
            transportClient = new PreBuiltTransportClient(settings)
                    .addTransportAddress(new TransportAddress(InetAddress.getByName(masterAddress), tcpPort));
        } catch (java.net.UnknownHostException e) {
        }
    }


3)测试代码的时候使用这种

  //获取client
Settings settings= Settings.builder().put("cluster.name","my-application").build();
TransportClient transportClient=new PreBuiltTransportClient(settings);
transportClient.addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"),9300));

二、常用操作

1、简单查询es( 指定index type id)

/**
* 简单查询es 指定index type id
*/
@Test
  public void search() throws UnknownHostException {
         //获取client
        Settings settings= Settings.builder().put("cluster.name","my-application").build();
        TransportClient transportClient=new PreBuiltTransportClient(settings);
        transportClient.addTransportAddress(new 
          TransportAddress(InetAddress.getByName("localhost"),9300));
        // 发起请求得到响应
        GetResponse  response=transportClient.prepareGet("index3","user3","10").get();
        System.out.println(response.getSource());
  }


2、创建索引

    public void createIndex(String indexName, String mappings){
        transportClient.admin().indices()
                .prepareCreate(indexName)
                .setSettings(Settings.builder()
                        .put("index.number_of_shards",5)
                        .put("index.max_result_window",100000000)
                        .put("index.analysis.analyzer.tmxenglish.type","english")
                        .put("index.analysis.analyzer.tmxenglish.stopwords_path","stopWords.txt")
                        .put("index.number_of_replicas",0))
                .addMapping("_doc", mappings, XContentType.JSON).get();
    }


3、判断索引是否存在

    public boolean indexExists(String index){
        IndicesExistsRequest request = new IndicesExistsRequest(index);
        logger.info("[indexExists] client {}  index {}", transportClient == null, index);
        IndicesExistsResponse response = transportClient.admin().indices().exists(request).actionGet();
        if (response.isExists()) {
            return true;
        }
        return false;
    }


4、批量查询mget

参考

//构建一个mget的查询
       MultiGetRequestBuilder multi_get = client.prepareMultiGet();
        //添加两条get数据
        multi_get.add("a_active","active","1");
        multi_get.add("b_active","active","2","3");
        //获取响应
        MultiGetResponse mgr= multi_get.get();
         //循环读取
        for (MultiGetItemResponse itemResponse : mgr) {
            GetResponse response = itemResponse.getResponse();
            //如果存在则打印响应消息
            if (response.isExists()) {
                String json = response.getSourceAsString();
                System.out.println(" source data: "+json);
            }
        }


5、bulk实现批量增删改操作。

  @Test
    public void bulk() throws Exception{
        Settings settings=Settings.builder().put("cluster.name","my-application").build();
        TransportClient client=new PreBuiltTransportClient(settings);
        client.addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"),9300));
        BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
        IndexRequest request=new IndexRequest();
        request.index("index3")
                .type("user3")
                .id("13")
                .source(XContentFactory.jsonBuilder()
                .startObject()
                .field("catid","85")
                .field("classify","85")
                .field("author","宇宙无敌")
                .field("id","漫威")
                .field("title","漫威")
                .endObject());
        IndexRequest request1=new IndexRequest();
        request1.index("index3")
                .type("user3")
                .id("14")
                .source(XContentFactory.jsonBuilder()
                        .startObject()
                        .field("catid","55")
                        .field("classify","85")
                        .field("author","宇宙无敌")
                        .field("id","漫威")
                        .field("title","漫威")
                        .endObject());
        bulkRequestBuilder.add(request1);
        bulkRequestBuilder.add(request);
        BulkResponse bulkItemResponses = bulkRequestBuilder.get();
        System.out.println(bulkItemResponses.status());
    }


6、使用query查询 match_all 查询所有

  @Test
    public void query() throws Exception{
        Settings settings=Settings.builder().put("cluster.name","my-application").build();
        TransportClient client=new PreBuiltTransportClient(settings);
        client.addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"),9300));
        MatchAllQueryBuilder builder= QueryBuilders.matchAllQuery();
        SearchRequestBuilder index3 = client.prepareSearch("index3")
                .setQuery(builder)
                .setSize(5);
        SearchResponse searchResponse = index3.get();
        SearchHits hits =  searchResponse.getHits();
        for (SearchHit hit:hits) {
            System.out.println(hit.getSourceAsString());
        }
    }


7、使用query查询 match 查询

  @Test
    public void matchQuery() throws Exception{
        Settings settings=Settings.builder().put("cluster.name","my-application").build();
        TransportClient client=new PreBuiltTransportClient(settings);
        client.addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"),9300));
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("title", "爱");
        SearchRequestBuilder index = client.prepareSearch("index3").setQuery(matchQueryBuilder).setSize(10);
        SearchHits hits = index.get().getHits();
        for (SearchHit hit:hits
             ) {
            System.out.println(hit.getSourceAsString());
        }
    }


8、使用query查询 mutilMatch 查询

@Test
    public void mutilMatchQuery() throws Exception{
        Settings settings=Settings.builder().put("cluster.name","my-application").build();
        TransportClient client=new PreBuiltTransportClient(settings);
        client.addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"),9300));
        MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery("周星驰影帝", "author","title");
        SearchRequestBuilder index = client.prepareSearch("index3").setQuery(multiMatchQueryBuilder).setSize(10);
        SearchHits hits = index.get().getHits();
        for (SearchHit hit:hits
        ) {
            System.out.println(hit.getSourceAsString());
        }
    }


9、使用query查询 term和terms查询

 @Test
    public void termsMatchQuery() throws Exception{
        Settings settings=Settings.builder().put("cluster.name","my-application").build();
        TransportClient client=new PreBuiltTransportClient(settings);
        client.addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"),9300));
        TermsQueryBuilder termsQueryBuilder = QueryBuilders.termsQuery("title", "周星驰", "影帝");
        SearchRequestBuilder index = client.prepareSearch("index3").setQuery(termsQueryBuilder).setSize(10);
        SearchHits hits = index.get().getHits();
        for (SearchHit hit:hits
        ) {
            System.out.println(hit.getSourceAsString());
        }
    }


10、使用query查询 范围 通配符 前缀 模糊查询


  @Test
    public void query1() throws Exception{
        Settings settings=Settings.builder().put("cluster.name","my-application").build();
        TransportClient client=new PreBuiltTransportClient(settings);
        client.addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"),9300));
        //范围查询
        //QueryBuilder queryBuilder = QueryBuilders.rangeQuery("classify").from("1").to("40");
        //通配符查询 * 零个或多个
//        QueryBuilder queryBuilder = QueryBuilders.wildcardQuery("author","周*");
        //前缀查询
//        QueryBuilder queryBuilder = QueryBuilders.prefixQuery("author","周");
        //模糊查询 查询类型的
//        QueryBuilder queryBuilder = QueryBuilders.fuzzyQuery("id","aac");
        //id查询
        QueryBuilder queryBuilder = QueryBuilders.idsQuery().addIds("1","2","4");
        SearchRequestBuilder index = client.prepareSearch("index3").setQuery(queryBuilder).setSize(10);
        SearchHits hits = index.get().getHits();
        for (SearchHit hit:hits
        ) {
            System.out.println(hit.getSourceAsString());
        }
    }


11、queryString 查询

  @Test
    public void queryString() throws Exception{
        Settings settings=Settings.builder().put("cluster.name","my-application").build();
        TransportClient client=new PreBuiltTransportClient(settings);
        client.addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"),9300));
        // + 代表必须有 -代表没有  
        QueryBuilder queryBuilder=QueryBuilders.queryStringQuery("+周星驰 -sss");
        SearchRequestBuilder index3 = client.prepareSearch("index3")
                .setQuery(queryBuilder)
                .setSize(10);
        SearchResponse searchResponse = index3.get();
        for (SearchHit hit:searchResponse.getHits()) {
            System.out.println(hit.getSourceAsString());
        }
    }


12、组合查询

 @Test
    public void boolQuery() throws Exception{
        Settings settings=Settings.builder().put("cluster.name","my-application").build();
        TransportClient client=new PreBuiltTransportClient(settings);
        client.addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"),9300));
        QueryBuilder queryBuilder=QueryBuilders.boolQuery()
                .must(QueryBuilders.matchQuery("author","周星驰"))
                .mustNot(QueryBuilders.matchQuery("title","梁朝伟"))
                .should(QueryBuilders.matchQuery("title","影帝"))
                .filter(QueryBuilders.rangeQuery("id").gte("1"));
        SearchRequestBuilder index3 = client.prepareSearch("index3")
                .setQuery(queryBuilder)
                .setSize(10);
        SearchResponse searchResponse = index3.get();
        for (SearchHit hit:searchResponse.getHits()) {
            System.out.println(hit.getSourceAsString());
        }
    }

三、聚合操作

1、聚合查询

相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
运维 网络协议 网络安全
Closed socket connection for client /39.103.162.230:56100 (no session established for client)
Closed socket connection for client /39.103.162.230:56100 (no session established for client)
506 0
Closed socket connection for client /39.103.162.230:56100 (no session established for client)
|
前端开发 Python
解决报错:Websocket connection to‘ws://127.0.0.1:5000/socket.io/?EIO=4&transport=websocket’failed:Error
报错全文为:Websocket connection to‘ws://127.0.0.1:5000/socket.io/?EIO=4&transport=websocket’failed:Error during Websocket handshake:Unexpected response code:400。如下图所示。
解决报错:Websocket connection to‘ws://127.0.0.1:5000/socket.io/?EIO=4&transport=websocket’failed:Error
|
网络协议
UDP Server/Client
UDP Server/Client
231 0
|
NoSQL API Redis
|
JavaScript 前端开发
|
开发工具
|
网络安全 CDN
|
开发工具