ElasticSearch流程图
ElasticSearch简介
Elasticsearch 是一个分布式可扩展的实时搜索和分析引擎,一个建立在全文搜索引擎 Apache Lucene™ 基础上的搜索引擎
ElasticSearch环境搭建
引入依赖
<dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>${elasticsearch.version}</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>x-pack-transport</artifactId> <version>${elasticsearch.version}</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>${log4j.version}</version> </dependency>
初始化客户端
跟操作数据库一样,写入地址、账号密码,获取一个客户端,有兴趣的可以看org.springframework.data.elasticsearch.client.ClusterNodesspring boot是怎么解析集群的
private TransportClient client; private String[] nodes = new String[]{"127.0.0.1:9200"}; @Before public void initClint() { Settings settings = Settings.builder() // es 集群的名称 .put("cluster.name", "elasticsearch") .put("client.transport.sniff", "true") //账号密码 .put("xpack.security.user", "elastic:123456") .build(); client = new PreBuiltXPackTransportClient(settings) //添加集群节点 .addTransportAddresses(parseAddress()); }
创建索引
可以理解为创建一个mysql数据库表
@Test public void createIndex() { client.admin() .indices() .prepareCreate("elastic").get(); client.close(); }
设置mappings
mappings可以理解为mysql的表字段(json数据)
对应的json格式:
{ "properties": { "id": { "type": "long", "store": true }, "name": { "type": "text", "store": true }, "age": { "type": "integer", "store": true } } }
对应的Java格式:
XContentBuilder builder = XContentFactory.jsonBuilder() .startObject() // 相当于json的'{' .startObject("properties") .startObject("id") .field("type", "long") //字段类型 .field("store", true) //是否存储 .endObject() //相当于json的'}' .startObject("name") .field("type", "text") .field("store", true) .field("analyzer", "ik_smart") //采用ik_smart分词 "search_analyzer": "ik_smart" .endObject() .startObject("age") .field("type", "integer") .field("store", true) .endObject() .startObject("desc") .field("type", "text") .field("store", true) .field("analyzer", "ik_max_word") .endObject() .startObject("registerTime") .field("type", "date") .field("store", true) .field("format", "yyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis") .endObject() .endObject() .endObject(); client.admin().indices() .preparePutMapping("elastic") .setType("user") //对应数据库的表名称 .setSource(builder) .get(); client.close();
基础语法
1.1常用查询
1、查询全部:match_all
2、模糊匹配: match (类似sql 的 like)
3、全句匹配: match_phrase (类似sql 的 = )
4、多字段匹配:muti_match (多属性查询)
5、语法查询:query_string (直接写需要配置的 关键字 )
6、字段查询 : term (针对某个属性的查询,这里注意 term 不会进行分词,比如 在 es 中 存了 “火锅” 会被分成 “火/锅” 当你用 term 去查询 “火时能查到”,但是查询 “火锅” 时,就什么都没有,而 match 就会将词语分成 “火/锅”去查)
7、范围查询:range ()
1.2 增删改查
1.2.1.插入操作
Java插入操作代码:
示例一:
String index = "test1"; String type = "_doc"; // 唯一编号 String id = "1"; IndexRequest request = new IndexRequest(index, type, id); String jsonString = "{" + "\"uid\":\"1234\","+ "\"phone\":\"12345678909\","+ "\"msgcode\":\"1\"," + "\"sendtime\":\"2019-03-14 01:57:04\"," + "\"message\":\"xuwujing study Elasticsearch\"" + "}"; request.source(jsonString, XContentType.JSON); IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
示例二:
String index = "test1"; String type = "_doc"; // 唯一编号 String id = "1"; IndexRequest request = new IndexRequest(index, type, id); Map<String, Object> jsonMap = new HashMap<>(); jsonMap.put("uid", 1234); jsonMap.put("phone", 12345678909L); jsonMap.put("msgcode", 1); jsonMap.put("sendtime", "2019-03-14 01:57:04"); jsonMap.put("message", "xuwujing study Elasticsearch"); request.source(jsonMap); IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
示例三:
String index = "test1"; String type = "_doc"; // 唯一编号 String id = "1"; IndexRequest request = new IndexRequest(index, type, id); XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject(); { builder.field("uid", 1234); builder.field("phone", 12345678909L); builder.field("msgcode", 1); builder.timeField("sendtime", "2019-03-14 01:57:04"); builder.field("message", "xuwujing study Elasticsearch"); } builder.endObject(); request.source(builder); IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
1> 使用自定义的id
使用put方式,并自己提供id
类似于下面的格式
PUT /{index}/{type}/{id} { "field": "value", ... }
请求
PUT /website/blog/123 { "title": "My first blog entry", "text": "Just trying this out...", "date": "2014/01/01" }
响应
{ "_index": "website", "_type": "blog", "_id": "123", "_version": 1, "created": true }
自动生成的 ID 是 URL-safe、 基于 Base64 编码且长度为20个字符的 GUID 字符串。 这些 GUID 字符串由可修改的 FlakeID 模式生成,这种模式允许多个节点并行生成唯一 ID ,且互相之间的冲突概率几乎为零。
1.2.2.更改操作
Java示例代码:
private static void update() throws IOException { String type = "_doc"; String index = "test1"; // 唯一编号 String id = "1"; UpdateRequest upateRequest = new UpdateRequest(); upateRequest.id(id); upateRequest.index(index); upateRequest.type(type); // 依旧可以使用Map这种集合作为更新条件 Map<String, Object> jsonMap = new HashMap<>(); jsonMap.put("uid", 12345); jsonMap.put("phone", 123456789019L); jsonMap.put("msgcode", 2); jsonMap.put("sendtime", "2019-03-14 01:57:04"); jsonMap.put("message", "xuwujing study Elasticsearch"); upateRequest.doc(jsonMap); // upsert 方法表示如果数据不存在,那么就新增一条 upateRequest.docAsUpsert(true); client.update(upateRequest, RequestOptions.DEFAULT); System.out.println("更新成功!"); }
控制台输入
PUT /website/blog/123 { "title": "My first blog entry", "text": "Just trying this out...", "date": "2014/01/01" }
在响应体中,我们能看到 Elasticsearch 已经增加了 _version 字段值,created 标志设置成 false ,是因为相同的索引、类型和 ID 的文档已经存在。
{ "_index": "website", "_type": "blog", "_id": "123", "_version": 2, "created": false }
1.2.3.删除操作
Java示例代码:
示例一(正常根据ID删除):
private static void delete() throws IOException { String type = "_doc"; String index = "test1"; // 唯一编号 String id = "1"; DeleteRequest deleteRequest = new DeleteRequest(); deleteRequest.id(id); deleteRequest.index(index); deleteRequest.type(type); // 设置超时时间 deleteRequest.timeout(TimeValue.timeValueMinutes(2)); // 设置刷新策略"wait_for" // 保持此请求打开,直到刷新使此请求的内容可以搜索为止。此刷新策略与高索引和搜索吞吐量兼容,但它会导致请求等待响应,直到发生刷新 deleteRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL); // 同步删除 DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT); }
示例二(条件删除):
private static void deleteByQuery() throws IOException { String type = "_doc"; String index = "test1"; DeleteByQueryRequest request = new DeleteByQueryRequest(index,type); // 设置查询条件 request.setQuery(QueryBuilders.termsQuery("uid",1234)); // 同步执行 BulkByScrollResponse bulkResponse = client.deleteByQuery(request, RequestOptions.DEFAULT); }
DELETE /website/blog/123
如果找到该文档,Elasticsearch 将要返回一个 200 ok 的 HTTP 响应码,和一个类似以下结构的响应体。注意,字段 _version 值已经增加:
{ "found" : true, "_index" : "website", "_type" : "blog", "_id" : "123", "_version" : 3 }
如果文档没有 找到,我们将得到 404 Not Found 的响应码和类似这样的响应体:
{ "found" : false, "_index" : "website", "_type" : "blog", "_id" : "123", "_version" : 4 }