ElasticSearch學習過程

head插件 port:9100
用ik分詞器
創建一個索引(數據庫)
創建一個索引test2帶有mapping的
往test2里面添加數據,默認表是_doc
往test2修改數據,(還有一種方法是put直接覆蓋),post的好處是可以一處修改,別的不變
刪除一個索引
查詢
查詢一個索引test2的全部數據
查詢匹配,match關鍵字,查詢索引test2中name是mxdmxd的信息
復雜查詢
結果過濾
3.刪除數據,把匹配到的age=123的記錄刪除
match和term
2.Java中的api調用
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
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
Elasticsearch
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。