目錄html
推薦去看官網的文檔,有中文的,我這裏僅僅是簡單的入門一下,作一下我的筆記 ElasticSearch官方文檔java
我使用Docker進行安裝,使用了中國加速node
docker pull registry.docker-cn.com/library/elasticsearch
而後,開啓鏡像,生成容器,這裏須要注意的是,ElasticSearch默認運行內存佔用2個G,我虛擬機整個纔給了2G內存,因此我要限制ElasticSearch的內存爲最小256M,最大256M,端口號是9200,在分佈式的狀況下,互通的端口號是9300程序員
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 --name myElasticSearch 5acf0e8da90b
在瀏覽器輸入你的Linux服務器IP+9200,當你看到下圖時就安裝成功了spring
ElasticSearch就是一個搜索的東西,維基百科,Github用的都是這個。簡單介紹一下,他的原理是這樣的,有索引,索引下有類型,類型下有文檔,文檔有屬性sql
能夠這麼理解,把ElasticSearch看做Mysql,索引就是數據庫,類型就是表,文檔就是數據,屬性就是數據的屬性類型docker
put 咱們來往ElasticSearch裏面存3條數據,ElasticSearch使用json存儲的,格式是shell
PUT /索引/類型/特定標識數據庫
我這裏提供幾個json給大家使用express
{ "first_name":"Vae", "age":32, "about":"許嵩是音樂家", "interests":["music","Photograph"] } { "first_name":"JJ", "age":32, "about":"林俊杰是音樂家", "interests":["music","Dota2"] } { "first_name":"shuyunquan", "age":23, "about":"蜀雲泉是程序員", "interests":["music","Photograph"] }
打開postman這個軟件,輸入http://193.112.28.104:9200/meizu/employee/1
我索引寫成魅族公司,注意必須小寫,類型寫成員工,標識先寫1
選擇PUT模式,Body,raw,send一下會有返回消息,如圖
咱們依次把2和3都存儲進ElasticSearch
把postman從PUT模式改爲HEAD模式,就能夠查有沒有數據了,例如咱們查3就顯示 status:200ok 這就代表有數據,咱們查4就顯示status:404 NotFound
把PUT改成Delete就是刪除,我這裏不演示
Get就是查詢,不演示
更新也是PUT,PUT第一次就是插入,後面全是更新
把id換成_search就能夠,例如:
好比我想查詢,about是和音樂相關的,我能夠這樣寫
http://193.112.28.104:9200/meizu/employee/_search?q=about:音樂
加了一個 ?q= 後面是屬性名:查詢關鍵字
看看結果:
上面的條件查詢加的是?q=,這裏使用條件表達式也是同樣的效果。
使用POST方式,http://193.112.28.104:9200/meizu/employee/_search
而後使用Body裏的raw,改成json數據,結果也是同樣的。這個也是全文搜索,只要有這個關鍵字的其中一個,就會出現
{ "query":{ "match":{ "about":"音樂" } } }
這個不是模糊的了,必須是和關鍵字如出一轍才能查出來
{ "query":{ "match_phrase":{ "about":"音樂" } } }
{ "query":{ "match_phrase":{ "about":"音樂" } }, "highlight":{ "fields":{ "about":{} } } }
SpringBoot與ElasticSearch的交互有兩種方式的,我把兩種方式的Maven依賴都寫出來,咱們兩種方式都測試一下。
ElasticSearch的Jest版Maven引用
<dependency> <groupId>io.searchbox</groupId> <artifactId>jest</artifactId> <version>5.3.4</version> </dependency>
ElasticSearch的SpringBoot版Maven引用,不是ElasticSearch,在Maven倉庫裏面須要搜索
spring-boot-starter-data-elasticsearch
出現的第一個就是,這個是SpringBoot版的ElasticSearch,以下
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> <version>2.1.3.RELEASE</version> </dependency>
咱們看看org.springframework.boot.autoconfigure這個包,發現下面有兩個ElasticSearch,一個是data.elasticsearch,一個是elasticsearch.jest,如圖:
這兩種方式都是能夠的,Jest是經過Http的方式,data.ElasticSearch是SpringBoot data整合的,總結一下
SpringBoot和ElasticSearch交互的兩種方式:
jest:默認不生效,須要導入一個io.searchbox.client.JestClient的包
SpringBoot Data:默認生效
把Jest的Maven依賴引入進項目
在application配置文件寫上咱們的ElasticSearch部署服務器的ip
spring: elasticsearch: jest: uris: http://193.112.28.104:9200
Jest存入ElasticSearch
@Autowired JestClient jestClient; @Test public void jest(){ //第一步,先新建文檔 Message message=new Message(); message.setId("1"); message.setCommand("音樂"); message.setDescription("音樂風格"); message.setContent("許嵩的音樂風格很是獨特"); //第二步,新建一個索引 Index index=new Index.Builder(message).index("Vae").type("Music").build(); //第三步,執行 try { jestClient.execute(index); } catch (IOException e) { e.printStackTrace(); } }
咱們這裏存儲一個Message對象,注意,Message對象的Id須要加一個註解
public class Message { @JestId private String id; ....
執行一下,成功後在瀏覽器或者PostMan輸入
http://193.112.28.104:9200/Vae/Article/1
照理說應該會出現咱們的Message對象的json數據,可是不知道爲何個人沒有出現....個人報了這個錯誤
{ "error": { "root_cause": [ { "type": "index_not_found_exception", "reason": "no such index", "resource.type": "index_expression", "resource.id": "Vae", "index_uuid": "_na_", "index": "Vae" } ], "type": "index_not_found_exception", "reason": "no such index", "resource.type": "index_expression", "resource.id": "Vae", "index_uuid": "_na_", "index": "Vae" }, "status": 404 }
我不知道爲何,也搜不出來答案
Jest讀取ElasticSearch
@Test public void jestsearch(){ String json="{\n" + " \"query\":{\n" + " \"match\":{\n" + " \"about\":\"音樂\"\n" + " }\n" + " }\n" + "}"; //構建搜索功能 Search search = new Search.Builder(json).addIndex("meizu").addType("employee").build(); //執行 try { SearchResult result=jestClient.execute(search); System.out.println(result.getJsonString()); } catch (IOException e) { e.printStackTrace(); } }
這個卻是成功了,我查找以前存儲的employee數據
咱們先引入SpringBoot data ElasticSearch的Maven依賴
配置文件
spring: data: elasticsearch: cluster-name: elasticsearch cluster-nodes: 193.112.28.104:9300
新建一個Java Bean的類吧
package com.example.bean; import io.searchbox.annotations.JestId; public class Article { @JestId private Integer id; private String title; private String auter; private String content; public Article() { } public Article(Integer id, String title, String auter, String content) { this.id = id; this.title = title; this.auter = auter; this.content = content; } @Override public String toString() { return "Article{" + "id=" + id + ", title='" + title + '\'' + ", auter='" + auter + '\'' + ", content='" + content + '\'' + '}'; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuter() { return auter; } public void setAuter(String auter) { this.auter = auter; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
再新建一個實現了ElasticsearchRepository接口的接口
package com.example.repository; import com.example.bean.Article; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import java.util.List; public interface ArticleRepository extends ElasticsearchRepository<Article,Integer> { public List<Article> findArticleContentLike(String Content); }
我本身寫了一個自定義方法,模糊查詢Content字段
@Test public void data1(){ Article article=new Article(); article.setId(1); article.setTitle("音樂"); article.setAuter("許嵩"); article.setContent("許嵩的音樂風格很獨特"); articleRepository.index(article); } @Test public void data2(){ for (Article article : articleRepository.findArticleContentLike("許嵩")) { System.out.println(article); } }
一個是存儲,一個是模糊查詢,可是!!我仍是執行保存......個人好像也不是版本問題.....先擱置吧,忙着找工做,沒時間解決這個報錯