赞
踩
下面会记录对ElasticSearch 的详细使用方法进,内容包括索引的增删,数据的增删查
一 创建开发环境
首先需要安装es集群 这里使用的是7.17.8
二 当es能正常启动的时候,开始对es进行做操作
1.索引操作
可以使用shell语法创建索引与删除索引,以及插入
- curl -X PUT "http://节点ip:9200/索引名称"
- curl -u 你的账号:你的密码 -XDELETE 'http://节点ip:9200/索引名称'
- curl -H "Content-Type: application/json" -XPUT 'http://note1:9200/index_test/doc/1' -d '{"age":128}'
也可以所使用的java代码发送http请求
- import com.alibaba.fastjson.JSONObject;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.HttpStatus;
- import org.apache.http.client.config.RequestConfig;
- import org.apache.http.client.methods.CloseableHttpResponse;
- import org.apache.http.client.methods.HttpDelete;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.client.methods.HttpPut;
- import org.apache.http.entity.StringEntity;
- import org.apache.http.impl.client.CloseableHttpClient;
- import org.apache.http.impl.client.HttpClientBuilder;
- import org.apache.http.impl.client.HttpClients;
- import org.apache.http.util.EntityUtils;
-
- import java.io.IOException;
- import java.util.Map;
-
- /* pom坐标
- <dependency>
- <groupId>org.elasticsearch</groupId>
- <artifactId>elasticsearch</artifactId>
- <version>7.17.8</version>
- </dependency>
- <dependency>
- <groupId>org.apache.httpcomponents</groupId>
- <artifactId>httpclient</artifactId>
- <version>4.5</version>
- </dependency>
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>fastjson</artifactId>
- <version>1.2.73</version>
- </dependency>
- */
- /**
- * 通过http对es进行索引的增删改
- */
- public class ESUtils {
-
- /**
- * 关闭资源
- *
- * @param client
- */
- public static void close(CloseableHttpClient client) {
- if (client != null) try {
- client.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- }
-
- public static void close(CloseableHttpResponse client) {
- if (client != null) try {
- client.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- }
-
- public static String sendDelete(String url, JSONObject headJson) throws Exception {
- String result = null;
-
- CloseableHttpClient client = null;
- try {
- client = HttpClientBuilder.create().build();
- HttpDelete put = new HttpDelete(url);
- if (headJson != null) {//设置请求头
- for (Map.Entry<String, Object> entry : headJson.entrySet()) {
- put.addHeader(entry.getKey(), String.valueOf(entry.getValue()));
- }
- }
- HttpResponse res = client.execute(put);
- if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
- result = EntityUtils.toString(res.getEntity());// 返回json格式:
- }
- } catch (Exception e) {
- throw new Exception(e);
- } finally {
- close(client);
- }
- return result;
- }
-
-
- public static String sendPost(String url, JSONObject headJson, String bodyJson) throws Exception {
- CloseableHttpClient client = null;
- String result = null;
- try {
- client = HttpClientBuilder.create().build();
- HttpPost post = new HttpPost(url);//支持get/post与put和delete - > HttpDelete
- if (headJson != null) {//设置请求头
- for (Map.Entry<String, Object> head : headJson.entrySet()) {
- post.addHeader(head.getKey(), head.getValue().toString());
- }
- }
- StringEntity body = new StringEntity(bodyJson, "utf-8");//设置请求体
- body.setContentEncoding("UTF-8");
- body.setContentType("application/json");//设置发送的请求体数据为json数据
- post.setEntity(body);
- HttpResponse res = client.execute(post);
- result = EntityUtils.toString(res.getEntity());// 返回json格式:
- } catch (Exception e) {
- throw new Exception(e);
- } finally {
- close(client);
- }
- return result;
- }
-
-
- public static String sendPut(String url, String jsonStr) {
-
- CloseableHttpClient httpClient = HttpClients.createDefault();
- HttpPut httpPut = new HttpPut(url);
- RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000).setConnectionRequestTimeout(35000).setSocketTimeout(60000).build();
- httpPut.setConfig(requestConfig);
- httpPut.setHeader("Content-type", "application/json");
- httpPut.setHeader("DataEncoding", "UTF-8");
-
- CloseableHttpResponse httpResponse = null;
- try {
- if (jsonStr != null) httpPut.setEntity(new StringEntity(jsonStr));
- httpResponse = httpClient.execute(httpPut);
- HttpEntity entity = httpResponse.getEntity();
- String result = EntityUtils.toString(entity);
- return result;
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- close(httpResponse);
- close(httpClient);
- }
- return null;
- }
-
-
- public static void main(String[] args) throws Exception {
-
- String respCreate = ESUtils.sendPut("http://note1:9200/index_test", null);//创建索引
- //映射最外层需要properties
- String mapping = "{\"properties\":{\"name\":{\"type\":\"text\",\"index\":true},\"sex\":{\"type\":\"text\",\"index\":false},\"age\":{\"type\":\"long\",\"index\":false}}}";
- String respUpdate = ESUtils.sendPut("http://note1:9200/index_test/doc/_mapping?include_type_name=true", mapping);//设置mapping映射
-
- String url = "http://note2:9200/index_test/doc?";//url后的路径表示: 索引/分类/id id的uri可以不要,即自动生成id
-
- String respInsert = sendPost(url, null, "{\"name\":\"张三123\",\"sex\":\"这是一段文本\",\"age\":128}");
-
- String respDelete = ESUtils.sendDelete("http://note1:9200/index_test", null);//删除索引
-
- System.out.println("respCreate创建索引响应: " + respCreate);
- System.out.println("respUpdate设置映射响应: " + respUpdate);
- System.out.println("respInsert插入数据响应: " + respInsert);
- System.out.println("respDelete删除索引响应: " + respDelete);
- }
-
- }
运行结果如下
使用代码进行批处理入库
- import org.apache.http.HttpHost;
- import org.elasticsearch.action.bulk.BulkRequest;
- import org.elasticsearch.action.bulk.BulkResponse;
- import org.elasticsearch.action.index.IndexRequest;
- import org.elasticsearch.client.RequestOptions;
- import org.elasticsearch.client.RestClient;
- import org.elasticsearch.client.RestHighLevelClient;
-
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.UUID;
-
- /*
- <dependency>
- <groupId>org.elasticsearch.client</groupId>
- <artifactId>elasticsearch-rest-high-level-client</artifactId>
- <version>7.17.7</version>
- <!-- 版本7.17.8有bug 使用7.17.7文件-->
- </dependency>
- */
-
- /**
- * 批处理
- */
- public class ESBatch {
- public static void main(String[] args) throws IOException {
- RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
- new HttpHost("note1", 9200),
- new HttpHost("note2", 9200),
- new HttpHost("note3", 9200)));
- BulkRequest bulkRequest = new BulkRequest();
- Map<String,Object> map = new HashMap();
- map.put("name", UUID.randomUUID().toString());
- bulkRequest.add(new IndexRequest().index("index_test").source(map));
- bulkRequest.add(new IndexRequest().index("index_test").source(map));
- bulkRequest.add(new IndexRequest().index("index_test").source(map));
- BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
- System.out.println("批量插,Took: " + bulkResponse.getTook());
- System.out.println("批量插入,Items: " + bulkResponse.getItems().length);
- client.close();
- }
- }
运行结果
字段类型与映射数据说明
2.查询
`````````````````````````````
2.获取操作elasticsearch的客户端
Settings settings = Settings.builder().put("cluster.name", "集群名称").build();
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("mini1"), 9300));
.addTransportAddress(new TransportAddress(InetAddress.getByName("mini2"), 9300));
.addTransportAddress(new TransportAddress(InetAddress.getByName("mini3"), 9300));
3.使用客户端client操作集群
(1)导入数据
处理数据
- String json = "{\"key\":\"value\"}";
- response = client.prepareIndex("索引名称", "类型", "id").setSource(json).get();
批处理
- String json = "{\"key\":\"value\"}"; //数据
- IndexRequestBuilder indexRequestBuilder = client.prepareIndex("索引名称", "doc", "100").setSource(json);//创建请求
- BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();//批处理对象
- bulkRequestBuilder.add(indexRequestBuilder);//添加到批处理
- bulkRequestBuilder.add(indexRequestBuilder);
- BulkResponse bulkItemResponses = bulkRequestBuilder.execute().actionGet();//执行处理获取结果集
- BulkItemResponse[] items = bulkItemResponses.getItems();//获取每一条数据的处理结果
(2)查询
全文检索
- QueryBuilder query = QueryBuilders.matchAllQuery();//查询类,查询全部
- SearchResponse searchResponse = client.prepareSearch("索引").setTypes("类型").setQuery(query).execute().actionGet();//创建请求并获取结果
- SearchHits hits = searchResponse.getHits();//获取数据集合
- for (SearchHit sh:hits) {//遍历
- System.out.println(sh.getSource());//获取单条数据
- }
搜索
- QueryBuilder query = QueryBuilders.matchQuery("字段名","字段值").operator(Operator.OR);//过滤查询,查询固定值
- ...
多字段检索
QueryBuilder query = QueryBuilders.multiMatchQuery("值","字段1","字段2");//同时指定多个field,不同field之间是或的关系
多条件查询
- QueryBuilder query1 = QueryBuilders.queryStringQuery("值1");//查询类1
- QueryBuilder query2 = QueryBuilders.queryStringQuery("值2");//查询类2
- SearchResponse searchResponse = client.prepareSearch("索引").setTypes("格式").setQuery(query1).setQuery(query2).get();//请求结果
前缀查询
QueryBuilder query = QueryBuilders.prefixQuery("字段", "值");
通用匹配
QueryBuilder query = QueryBuilders.wildcardQuery("字段", "值.*");
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。