一、基本概念
这里描述操作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()); } }