ElasticSearch学习过程
【摘要】
ElasticSearch
head插件 port:9100
用ik分词器创建一个索引(数据库)创建一个索引test2带有mapping的往test2里面添加数据,默认表是_doc往test2修改数据,(还有一种方法是put直接覆盖),post的好处是可以一处修改,别的不变 删除一个索引
查询查询一个索引test2的全部数据查询匹配,match关...
head插件 port:9100
14、重启es并启动grunt
nohup elasticsearch &
grunt server & --grunt前端启动工具
15、使用head插件
http://192.168.137.1:9100/
用ik分词器
两个分词方法:ik_max_word最细粒度划分
ik_smart最小划分,只划一个
GET _analyze
{
"analyzer": "ik_smart",
"text": "天生我材必有用"
}
GET _analyze
{
"analyzer": "ik_max_word",
"text": "天生我材必有用"
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
创建一个索引(数据库)
put 索引名/类型(以后会删除)/文档id
PUT mxd/type1/1
{
"name": "马向东",
"age": 23
}
- 1
- 2
- 3
- 4
- 5
创建一个索引test2带有mapping的
PUT /test2
{
"mappings": { "properties": { "name": { "type": "text" }, "age": { "type": "integer" }, "birthday": { "type": "date" } }
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
往test2里面添加数据,默认表是_doc
/doc是默认表名,id是1
PUT test2/_doc/1
{
"name": "马向东",
"age": 23,
"birthday" : "1911-11-11"
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
往test2修改数据,(还有一种方法是put直接覆盖),post的好处是可以一处修改,别的不变
POST test2/_doc/1/_update
{
"doc":{ "name": "咚咚咚"
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
删除一个索引
DELETE tute
- 1
- 2
- 3
查询
条件查询:查询test2中name列:包含马的记录
GET test2/_doc/_search?q=name:马
- 1
查询一个索引test2的全部数据
GET test2/_search
- 1
查询匹配,match关键字,查询索引test2中name是mxdmxd的信息
GET test2/_search
{
"query": { "match": { "name":"mxdmxd" }
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
复杂查询
GET test2/_search
{
"query": { "match": { "name":"mxdmxd" }
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
结果过滤
GET test2/_search
{
"query": { "match": { "name":"mxdmxd" }
} ,
"_source": "age" , #只显示age
"sort": [ #排序 { "age": { "order": "asc" } }
],
"from": 0 #分页:2个一页
, "size"0: 2 }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
3.删除数据,把匹配到的age=123的记录删除
POST test2/_delete_by_query
{
"query": { "match": { "age": 123 }
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
match和term
match在匹配时会对所查找的关键词进行分词,然后按分词匹配查找,而term会直接对关键词进行查找。一般模糊查找的时候,多用match,而精确查找时可以使用term。
2.Java中的api调用
@Configuration
public class ElasticConfig { @Bean public RestHighLevelClient restHighLevelClient() { RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost("192.168.56.22", 9200, "http"), new HttpHost("192.168.56.33", 9200, "http"), new HttpHost("192.168.56.22", 9200, "http"))); return client; } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
@SpringBootTest
class Elastic1ApplicationTests { @Autowired RestHighLevelClient restHighLevelClient; //创建索引请求 @Test void contextLoads() throws IOException { CreateIndexRequest test3 = new CreateIndexRequest("test3"); CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(test3, RequestOptions.DEFAULT); System.out.println(createIndexResponse); } //获取索引请求,判断是否存在 @Test void testExitisIndex() throws IOException { GetIndexRequest test2 = new GetIndexRequest("test2"); boolean exists = restHighLevelClient.indices().exists(test2, RequestOptions.DEFAULT); System.out.println(exists); } //删除索引请求,存在才可以删除 @Test void testDeleteIndex() throws IOException { DeleteIndexRequest request = new DeleteIndexRequest("mxd"); AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT); System.out.println(delete.isAcknowledged()); } //测试添加文档 @Test void testAddDoc() throws IOException { User mxd = new User("马22向东", 12223); IndexRequest request = new IndexRequest(("mxd_index")); request.id("2"); //data放入json中 request.source(JSON.toJSONString(mxd), XContentType.JSON); //client send request IndexResponse index = restHighLevelClient.index(request, RequestOptions.DEFAULT); System.out.println(index.toString()); System.out.println(index.status()); } //文档是否存在 @Test void getIsXistsDoc() throws IOException { GetRequest mxd_index = new GetRequest("mxd_index", "1"); //不拿上下文 mxd_index.fetchSourceContext(new FetchSourceContext(false)); mxd_index.storedFields("_none_"); boolean exists = restHighLevelClient.exists(mxd_index, RequestOptions.DEFAULT); System.out.println(exists); } //拿文档信息 @Test void getDoc() throws IOException { GetRequest mxd_index = new GetRequest("mxd_index", "1"); GetResponse getResponse = restHighLevelClient.get(mxd_index, RequestOptions.DEFAULT); String s = getResponse.getSourceAsString(); System.out.println(s); System.out.println(getResponse); } //update @Test void updateDoc() throws IOException { UpdateRequest mxd_index = new UpdateRequest("mxd_index", "1"); User mxd = new User("mxd", 186); mxd_index.doc(JSON.toJSONString(mxd), XContentType.JSON); UpdateResponse update = restHighLevelClient.update(mxd_index, RequestOptions.DEFAULT); System.out.println(update.status()); } //delete @Test void deleteDoc() throws IOException { DeleteRequest mxd_index = new DeleteRequest("mxd_index", "2"); DeleteResponse delete = restHighLevelClient.delete(mxd_index, RequestOptions.DEFAULT); System.out.println(delete.status()); } //批量插入 @Test void testBulkDoc() throws IOException { BulkRequest bulkRequest = new BulkRequest(); ArrayList<User> objects = new ArrayList<>(); objects.add(new User("mxd", 2)); objects.add(new User("mxd1", 2)); objects.add(new User("mxd2", 2)); objects.add(new User("mxd3", 2)); objects.add(new User("mxd4", 2)); objects.add(new User("mxd5", 2)); for (int i = 0; i < objects.size(); i++) { bulkRequest.add( new IndexRequest("mxd_index") .id("" + (i + 5)) .source(JSON.toJSONString(objects.get(i)), XContentType.JSON) ); } BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT); System.out.println(bulk.hasFailures()); //是否失败 } //query @Test void testSearch() throws Exception{ SearchRequest mxd_index = new SearchRequest("mxd_index"); //query 条件 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); TermQueryBuilder query = QueryBuilders.termQuery("name", "mxd1"); searchSourceBuilder.query(query); mxd_index.source(searchSourceBuilder); SearchResponse search = restHighLevelClient.search(mxd_index, RequestOptions.DEFAULT); System.out.println(JSON.toJSONString(search.getHits())); System.out.println("--------------"); for (SearchHit hit : search.getHits().getHits()) { System.out.println(hit.getSourceAsMap()); } }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
文章来源: blog.csdn.net,作者:East Horse,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/weixin_44613138/article/details/108885585
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)