1⃣️环境准备
- 准备 Java 环境:终端输入
java -version
命令来确认版本是否符合 Elasticsearch 要求 - 下载并解压 Elasticsearch:前往(https://www.elastic.co/downloads/elasticsearch)选择适合你的 Mac 系统的 Elasticsearch 版本进行下载
- 设置 Elasticsearch 配置:在
config
目录下找到并打开elasticsearch.yml
配置文件。根据你的需求,修改其中的一些设置,比如监听的端口、集群名称等 - 启动 Elasticsearch:打开终端,进入 Elasticsearch 解压目录,执行以下命令启动 Elasticsearch:
./bin/elasticsearch
- 验证 Elasticsearch 是否正常运行:打开浏览器,访问
http://localhost:9200
地址,如果能看到返回的 JSON 格式的信息,说明 Elasticsearch 已经成功安装并运行。
2⃣️使用
要使用Elasticsearch进行增删改查等操作,你可以通过Postman来实现。下面我将提供一个使用RESTful API来演示的示例:
💫创建索引库
💫添加文档(添加行)
💫查询索引库
💫修改索引库
💫删除索引库
3⃣️与Java使用
如何使用Java与Elasticsearch进行索引的创建、文档的增加、查询、修改和删除操作:
💠添加Elasticsearch Maven依赖:
在pom.xml
文件中添加以下依赖项:
<dependencies><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>7.13.3</version></dependency><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-client</artifactId><version>7.13.3</version></dependency>
</dependencies>
💠创建索引库:
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.xcontent.json.JsonXContent;// 创建RestHighLevelClient客户端
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder("localhost:9200"));// 创建索引请求
CreateIndexRequest request = new CreateIndexRequest("my_index");
request.settings(Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0));// 创建映射
XContentBuilder mapping = JsonXContent.contentBuilder().startObject().startObject("properties").startObject("user").field("type", "keyword").endObject().startObject("postDate").field("type", "date").endObject().startObject("message").field("type", "text").endObject().endObject().endObject();
request.mapping(mapping);// 执行创建索引请求
CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
if (response.isAcknowledged()) {System.out.println("索引创建成功");
} else {System.out.println("索引创建失败");
}// 关闭客户端
client.close();
💠添加文档:
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.client.RestClient;// 创建RestHighLevelClient客户端
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder("localhost:9200"));// 构建索引请求
IndexRequest request = new IndexRequest("my_index");
request.id("1");
String jsonString = "{" +"\"user\":\"John\"," +"\"postDate\":\"2023-09-25\"," +"\"message\":\"This is a sample document\"" +"}";
request.source(jsonString, XContentType.JSON);// 执行索引请求
IndexResponse response = client.index(request, RequestOptions.DEFAULT);// 获取响应结果
String index = response.getIndex();
String id = response.getId();
if (response.getResult() == DocWriteResponse.Result.CREATED) {System.out.println("文档创建成功,索引:" + index + ",id:" + id);
} else if (response.getResult() == DocWriteResponse.Result.UPDATED) {System.out.println("文档更新成功,索引:" + index + ",id:" + id);
}// 关闭客户端
client.close();
💠查询索引库
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.client.RestClient;// 创建RestHighLevelClient客户端
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder("localhost:9200"));// 构建搜索请求
SearchRequest request = new SearchRequest("my_index");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("message", "sample"));
request.source(searchSourceBuilder);// 执行搜索请求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);// 处理搜索结果
SearchHits hits = response.getHits();
for (SearchHit hit : hits.getHits()) {String sourceAsString = hit.getSourceAsString();System.out.println(sourceAsString);
}// 关闭客户端
client.close();
💠修改索引库:
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.client.RestClient;// 创建RestHighLevelClient客户端
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder("localhost:9200"));// 构建更新请求
UpdateRequest request = new UpdateRequest("my_index", "1").doc(XContentFactory.jsonBuilder().startObject().field("message", "Updated document").endObject());// 执行更新请求
UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
String index = response.getIndex();
String id = response.getId();
if (response.getResult() == DocWriteResponse.Result.UPDATED) {System.out.println("文档更新成功,索引:" + index + ",id:" + id);
} else if (response.getResult() == DocWriteResponse.Result.NOOP) {System.out.println("文档未发生更新,索引:" + index + ",id:" + id);
}// 关闭客户端
client.close();
💠删除索引库:
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.RestClient;// 创建RestHighLevelClient客户端
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder("localhost:9200"));// 构建删除请求
DeleteRequest request = new DeleteRequest("my_index", "1");// 执行删除请求
DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
String index = response.getIndex();
String id = response.getId();
if (response.getResult() == DocWriteResponse.Result.DELETED) {System.out.println("文档删除成功,索引:" + index + ",id:" + id);
} else if (response.getResult() == DocWriteResponse.Result.NOT_FOUND) {System.out.println("文档未找到,索引:" + index + ",id:" + id);
}// 关闭客户端
client.close();