1
1
---
2
- typora-root-url : ./
2
+
3
3
---
4
4
5
5
# SpringBoot的高级教程
@@ -1236,29 +1236,25 @@ docker pull registry.docker-cn.com/library/elasticsearch
1236
1236
1237
1237
4 、运行ElasticSearch
1238
1238
1239
- > - e ES_JAVA_OPTS = " -Xms256m -Xmx256m" 表示占用的最大内存为256m, 默认是2G
1239
+ > - e ES_JAVA_OPTS = " -Xms256m -Xmx256m" 表示占用的最大内存为256m, 默认是2G
1240
1240
1241
1241
```shell
1242
- [root@node1 ~ ]# docker images
1243
- REPOSITORY TAG IMAGE ID CREATED SIZE
1244
- registry. docker- cn. com/ library/ elasticsearch latest 671bb2d7da44 32 hours ago 486 MB
1245
- [root@node1 ~ ]#
1246
- [root@node1 ~ ]# docker run - e ES_JAVA_OPTS = " -Xms256m -Xmx256m" - d - p 9200 : 9200 - p 9300 : 9300 -- name ES01 671bb2d7da44
1242
+ [root@node1 ~ ]# docker run - e ES_JAVA_OPTS = " -Xms256m -Xmx256m" - d - p 9200 : 9200 - p 9300 : 9300 -- name ES01 5acf0e8da90b
1247
1243
```
1248
1244
1249
1245
5 、测试是否启动成功
1250
1246
1251
- 访问9200 端口:http: // 192.168.179.131 :9200/ 查看是否返回json数据
1247
+ 访问9200 端口:http: // 10.138.223.126 :9200/ 查看是否返回json数据
1252
1248
1253
1249
```json
1254
1250
{
1255
- " name" : " onB-EUU " ,
1251
+ " name" : " DNF7ndJ " ,
1256
1252
" cluster_name" : " elasticsearch" ,
1257
- " cluster_uuid" : " j3SXX6tdThWUomW3tAvDFg " ,
1253
+ " cluster_uuid" : " t-JMNI1LRgGf62ctJqiGYQ " ,
1258
1254
" version" : {
1259
- " number" : " 5.6.9 " ,
1260
- " build_hash" : " 877a590 " ,
1261
- " build_date" : " 2018-04-12T16:25:14.838Z " ,
1255
+ " number" : " 5.6.12 " ,
1256
+ " build_hash" : " cfe3d9f " ,
1257
+ " build_date" : " 2018-09-10T20:12:43.732Z " ,
1262
1258
" build_snapshot" : false ,
1263
1259
" lucene_version" : " 6.6.1"
1264
1260
},
@@ -1268,7 +1264,7 @@ registry.docker-cn.com/library/elasticsearch latest 671bb2d7da44
1268
1264
1269
1265
### 3 、Elastic 的快速入门
1270
1266
1271
- > 最好的工具就是[官方文档](https: // www.elastic.co/guide/cn/elasticsearch/guide/current/getting-started .html),以下操作都在文档中进行操作。
1267
+ > 最好的工具就是[官方文档](https: // www.elastic.co/guide/cn/elasticsearch/guide/current/index .html),以下操作都在文档中进行操作。
1272
1268
1273
1269
#### 1 、基础概念
1274
1270
@@ -1288,6 +1284,10 @@ registry.docker-cn.com/library/elasticsearch latest 671bb2d7da44
1288
1284
1289
1285
** 属性** :相当于数据库中的列,即文档的属性
1290
1286
1287
+ ** 结构图** :
1288
+
1289
+ ! [1538037936258 ](/ images2/ 1538037936258. png)
1290
+
1291
1291
#### 2 、测试
1292
1292
1293
1293
下载[POSTMAN ](https: // www.getpostman.com/apps),并使用POSTMAN测试
@@ -1309,7 +1309,7 @@ PUT /megacorp/employee/1
1309
1309
}
1310
1310
```
1311
1311
1312
- ! [01 . postman](/ images2/ 01 . postman . jpg )
1312
+ ! [01 . postman](/ images2/ 1538038492112 . png )
1313
1313
1314
1314
##### 2 、检索文档
1315
1315
@@ -1321,7 +1321,7 @@ PUT /megacorp/employee/1
1321
1321
GET / megacorp/ employee/ 1
1322
1322
```
1323
1323
1324
- ! [02 . postmanget](/ images2/ 02 . postmanget . jpg )
1324
+ ! [02 . postmanget](/ images2/ 1538038658381 . png )
1325
1325
1326
1326
##### 3 、轻量检索
1327
1327
@@ -1720,18 +1720,15 @@ GET /megacorp/employee/_search
1720
1720
1 、注释SpringDataElasticSearch 的依赖,并导入Jest 【5. xx】的相关依赖
1721
1721
1722
1722
```xml
1723
- < ! -- SpringData 管理ElasticSearch -- >
1724
- < ! -- < dependency>
1725
- < groupId> org. springframework. boot< / groupId>
1726
- < artifactId> spring- boot- starter- data- elasticsearch< / artifactId>
1727
- < / dependency> -- >
1728
-
1729
- < ! -- https: // mvnrepository.com/artifact/io.searchbox/jest -->
1730
- < dependency>
1731
- < groupId> io. searchbox< / groupId>
1732
- < artifactId> jest< / artifactId>
1733
- < version> 5.3 . 3 < / version>
1734
- < / dependency>
1723
+ < ! -- < dependency> -- >
1724
+ < ! -- < groupId> org. springframework. boot< / groupId> -- >
1725
+ < ! -- < artifactId> spring- boot- starter- data- elasticsearch< / artifactId> -- >
1726
+ < ! -- < / dependency> -- >
1727
+ < dependency>
1728
+ < groupId> io. searchbox< / groupId>
1729
+ < artifactId> jest< / artifactId>
1730
+ < version> 5.3 . 3 < / version>
1731
+ < / dependency>
1735
1732
```
1736
1733
1737
1734
2 、修改配置文件application. yml
@@ -1740,7 +1737,7 @@ GET /megacorp/employee/_search
1740
1737
spring:
1741
1738
elasticsearch:
1742
1739
jest:
1743
- uris: http: // 192.168.179.131 :9200
1740
+ uris: http: // 10.138.223.126 :9200
1744
1741
```
1745
1742
1746
1743
3 、创建 bean. Article
@@ -1799,54 +1796,51 @@ public class Article {
1799
1796
> 向wdjr- article中插入数据
1800
1797
1801
1798
```java
1802
- @Autowired
1803
- JestClient jestClient;
1804
-
1805
1799
@Test
1806
1800
public void contextLoads() {
1807
- // 1、 给Es中索引(保存)一个文档
1801
+ // 给Es中索引(保存)一个文档
1808
1802
Article article = new Article ();
1809
- article. setId(2 );
1810
- article. setTitle(" 好消息 " );
1811
- article. setAutor(" zhangsan " );
1803
+ article. setId(1 );
1804
+ article. setTitle(" Effect Java " );
1805
+ article. setAutor(" Joshua Bloch " );
1812
1806
article. setContent(" Hello World" );
1813
- // 构建一个索引功能
1814
- Index index = new Index .Builder (article). index(" wdjr " ). type(" article" ). build();
1807
+ // 构建一个索引功能
1808
+ Index index = new Index .Builder (article). index(" cuzz " ). type(" article" ). build();
1815
1809
1816
1810
try {
1817
1811
// 执行
1818
1812
jestClient. execute(index);
1819
1813
} catch (IOException e) {
1820
1814
e. printStackTrace();
1821
1815
}
1822
-
1823
1816
}
1824
1817
```
1825
1818
1819
+ ! [1538043139689 ](/ images2/ 1538043139689. png)
1820
+
1826
1821
> 查询数据
1827
1822
1828
1823
```java
1829
1824
@Test
1830
1825
public void search(){
1831
- // 查询表达式
1826
+ // 查询表达式
1832
1827
String json = " {\n " +
1833
- " \" query\" : {\n " +
1834
- " \" match\" : {\n " +
1835
- " \" content\" : \" Hello\"\n " +
1836
- " }\n " +
1837
- " }\n " +
1838
- " }" ;
1839
- // 构建搜索操作
1840
- Search search = new Search .Builder (json). addIndex(" wdjr " ). addType(" article" ). build();
1841
-
1842
- // 执行
1828
+ " \" query\" : {\n " +
1829
+ " \" match\" : {\n " +
1830
+ " \" content\" : \" Hello\"\n " +
1831
+ " }\n " +
1832
+ " }\n " +
1833
+ " }" ;
1834
+ // 构建搜索操作
1835
+ Search search = new Search .Builder (json). addIndex(" cuzz " ). addType(" article" ). build();
1836
+
1837
+ // 执行
1843
1838
try {
1844
1839
SearchResult result = jestClient. execute(search);
1845
1840
System . out. println(result. getJsonString());
1846
1841
} catch (IOException e) {
1847
1842
e. printStackTrace();
1848
1843
}
1849
-
1850
1844
}
1851
1845
```
1852
1846
@@ -1870,7 +1864,7 @@ public void search(){
1870
1864
1871
1865
```shell
1872
1866
docker pull elasticsearch: 2.4 . 6
1873
- docker run - e ES_JAVA_OPTS = " -Xms256m -Xmx256m" - d - p 9201 : 9200 - p 9301 : 9300 -- name ES02 elasticsearch : 2.4 . 6
1867
+ docker run - e ES_JAVA_OPTS = " -Xms256m -Xmx256m" - d - p 9201 : 9200 - p 9301 : 9300 -- name ES02 id
1874
1868
```
1875
1869
1876
1870
3 、编写配置文件
@@ -1880,10 +1874,19 @@ spring:
1880
1874
data:
1881
1875
elasticsearch:
1882
1876
cluster- name: elasticsearch
1883
- cluster- nodes: 192.168 . 179.131 : 9301
1877
+ cluster- nodes: 10.138 . 223.126 : 9301
1878
+ ```
1879
+
1880
+ 4 、修改pom文件,把使用data- elasticsearch,把刚才注释删除
1881
+
1882
+ ```xml
1883
+ < dependency>
1884
+ < groupId> org. springframework. boot< / groupId>
1885
+ < artifactId> spring- boot- starter- data- elasticsearch< / artifactId>
1886
+ < / dependency>
1884
1887
```
1885
1888
1886
- 4 、运行主程序
1889
+
1887
1890
1888
1891
5 、操作ElasticSearch 有两种方式
1889
1892
@@ -1893,12 +1896,17 @@ spring:
1893
1896
1894
1897
6 、ElasticsearchRepositry 的操作
1895
1898
1896
- 1 )、新建一个bean/ Book 类
1899
+ 1 )、新建一个bean/ Book 类, ** 注意: ** @Document ( indexName = " cuzz " , type = " book " )
1897
1900
1898
1901
```java
1899
- @Document (indexName = " wdjr" ,type = " book" )
1902
+ /**
1903
+ * @Author: cuzz
1904
+ * @Date: 2018/9/27 18:32
1905
+ * @Description:
1906
+ */
1907
+ @Document (indexName = " cuzz" ,type = " book" )
1908
+ @Data
1900
1909
public class Book {
1901
-
1902
1910
private Integer id;
1903
1911
private String bookName;
1904
1912
private String auto;
@@ -1914,47 +1922,21 @@ public class Book {
1914
1922
this . bookName = bookName;
1915
1923
this . auto = auto;
1916
1924
}
1917
-
1918
- public Integer getId () {
1919
- return id;
1920
- }
1921
-
1922
- public void setId (Integer id ) {
1923
- this . id = id;
1924
- }
1925
-
1926
- public String getBookName () {
1927
- return bookName;
1928
- }
1929
-
1930
- public void setBookName (String bookName ) {
1931
- this . bookName = bookName;
1932
- }
1933
-
1934
- public String getAuto () {
1935
- return auto;
1936
- }
1937
-
1938
- public void setAuto (String auto ) {
1939
- this . auto = auto;
1940
- }
1941
-
1942
- @Override
1943
- public String toString () {
1944
- return " Book{" +
1945
- " id=" + id +
1946
- " , bookName='" + bookName + ' \' ' +
1947
- " , auto='" + auto + ' \' ' +
1948
- ' }' ;
1949
- }
1950
1925
}
1951
1926
```
1952
1927
1953
1928
2 )、新建一个repositry/ BookRepositry
1954
1929
1930
+ 方法编写参考[官方文档](https: // docs.spring.io/spring-data/elasticsearch/docs/3.1.0.RELEASE/reference/html/#elasticsearch.repositories)
1931
+
1955
1932
```java
1956
- public interface BookRepositry extends ElasticsearchRepository<Book ,Integer > {
1957
- // 自定义查询方法
1933
+ /**
1934
+ * @Author: cuzz
1935
+ * @Date: 2018/9/27 18:33
1936
+ * @Description:
1937
+ */
1938
+ public interface BookRepository extends ElasticsearchRepository<Book , Integer > {
1939
+ // 自定义查询方法
1958
1940
public List<Book > findByBookNameLike (String bookName );
1959
1941
}
1960
1942
```
@@ -1966,10 +1948,9 @@ public interface BookRepositry extends ElasticsearchRepository<Book,Integer> {
1966
1948
BookRepositry bookRepositry;
1967
1949
@Test
1968
1950
public void testSearch(){
1969
- for (Book book : bookRepositry. findByBookNameLike(" 金 " )) {
1951
+ for (Book book : bookRepositry. findByBookNameLike(" Effect " )) {
1970
1952
System . out. println(book);
1971
1953
}
1972
-
1973
1954
}
1974
1955
```
1975
1956
0 commit comments