一、Linux下安裝ElasticSearch
1、檢測是否安裝了Elasticsearch
1 ps aux |grep elasticsearch
2、安裝JDK
3、下載Elasticsearch
1 wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.0.0.tar.gz
解壓Elasticsearch
tar -zxvf elasticsearch-6.0.0.tar.gz
移動Elasticsearch到/usr/local/elasticsearch
1 mv elasticsearch-6.0.0 /usr/local/elasticsearch
4、修改配置文件
進入/usr/local/elasticsearch/config目錄,使用vi編輯器
1 vi elasticsearch.yml
1 network.host: 192.168.181.201 2 discovery.zen.ping.unicast.hosts: ["192.168.181.201"]
修改/etc/sysctl.conf否則會出現
max virutal memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
vm.max_map_count=262144
退出保存后執行如下命令:
1 sysctl -p
使用vi編輯器,修改/etc/security/limits.conf文件,在文件末尾添加如下代碼,否則會出現
max number of threads [3750] for user [xxx] is too low, increase to at least [4096]
1 # sanglp為登錄服務器的用戶名 2 3 sanglp soft nofile 65536 4 sanglp hard nofile 65536 5 sanglp soft nproc 4096 6 sanglp hard nproc 4096
切記退出重新登錄。
5、啟動Elasticsearch
進入/usr/local/elasticsearch目錄之中,輸入以下命令,開始Elasticsearch服務:
1 ./bin/elasticsearch
測試:
二、新建spring boot的elasticsearch項目
在start.spring.io中新建springboot項目,並導入到intellij中
pom.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>com.slp</groupId> 7 <artifactId>springboot-elasticsearch</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <name>springboot-elasticsearch</name> 12 <description>Demo project for Spring Boot</description> 13 14 <parent> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-parent</artifactId> 17 <version>2.1.0.BUILD-SNAPSHOT</version> 18 <relativePath/> <!-- lookup parent from repository --> 19 </parent> 20 21 <properties> 22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 24 <java.version>1.8</java.version> 25 </properties> 26 27 <dependencies> 28 <!--添加web的應用依賴--> 29 <dependency> 30 <groupId>org.springframework.boot</groupId> 31 <artifactId>spring-boot-starter-web</artifactId> 32 </dependency> 33 <!--添加spring-data-elasticsearch的依賴--> 34 <dependency> 35 <groupId>org.springframework.boot</groupId> 36 <artifactId>spring-boot-starter-data-elasticsearch</artifactId> 37 </dependency> 38 39 <dependency> 40 <groupId>org.springframework.boot</groupId> 41 <artifactId>spring-boot-starter-test</artifactId> 42 <scope>test</scope> 43 </dependency> 44 45 <dependency> 46 <groupId>net.java.dev.jna</groupId> 47 <artifactId>jna</artifactId> 48 <version>3.0.9</version> 49 </dependency> 50 </dependencies> 51 52 <build> 53 <plugins> 54 <plugin> 55 <groupId>org.springframework.boot</groupId> 56 <artifactId>spring-boot-maven-plugin</artifactId> 57 </plugin> 58 </plugins> 59 </build> 60 61 <repositories> 62 <repository> 63 <id>spring-snapshots</id> 64 <name>Spring Snapshots</name> 65 <url>https://repo.spring.io/snapshot</url> 66 <snapshots> 67 <enabled>true</enabled> 68 </snapshots> 69 </repository> 70 <repository> 71 <id>spring-milestones</id> 72 <name>Spring Milestones</name> 73 <url>https://repo.spring.io/milestone</url> 74 <snapshots> 75 <enabled>false</enabled> 76 </snapshots> 77 </repository> 78 </repositories> 79 80 <pluginRepositories> 81 <pluginRepository> 82 <id>spring-snapshots</id> 83 <name>Spring Snapshots</name> 84 <url>https://repo.spring.io/snapshot</url> 85 <snapshots> 86 <enabled>true</enabled> 87 </snapshots> 88 </pluginRepository> 89 <pluginRepository> 90 <id>spring-milestones</id> 91 <name>Spring Milestones</name> 92 <url>https://repo.spring.io/milestone</url> 93 <snapshots> 94 <enabled>false</enabled> 95 </snapshots> 96 </pluginRepository> 97 </pluginRepositories> 98 99 100 </project>
application.yml
1 spring: 2 data: 3 elasticsearch: 4 cluster-name: elasticsearch #默認為elasticsearch 5 cluster-nodes: 192.168.181.201:9300 #配置es節點信息,逗號分隔,如果沒有指定,則啟動ClientNode 6 properties: 7 path: 8 logs: ./elasticsearch/log #elasticsearch日志存儲目錄 9 data: ./elasticsearch/data #elasticsearch數據存儲目錄
Article.java
1 package com.slp.springbootelasticsearch.pojo; 2 3 import org.springframework.data.elasticsearch.annotations.Document; 4 5 import java.io.Serializable; 6 7 /** 8 * @author sanglp 9 * @create 2018-07-04 9:06 10 * @desc 文章實體類,默認情況下添加@Document注解會對實體中的所有屬性建立索引, 11 **/ 12 @Document(indexName = "projectname",type = "article",indexStoreType = "fs",shards = 5,replicas = 1,refreshInterval = "-1") 13 public class Article implements Serializable { 14 15 private Long id; 16 /** 17 * 標題 18 */ 19 private String title; 20 /** 21 * 摘要 22 */ 23 private String abstracts; 24 /** 25 * 內容 26 */ 27 private String content; 28 /** 29 * 發表時間 30 */ 31 private String postTime; 32 /** 33 * 點擊率 34 */ 35 private String clickCount; 36 /** 37 * 作者 38 */ 39 private Author author; 40 /** 41 * 所屬教程 42 */ 43 private Tutorial tutorial; 44 45 public Long getId() { 46 return id; 47 } 48 49 public void setId(Long id) { 50 this.id = id; 51 } 52 53 public String getTitle() { 54 return title; 55 } 56 57 public void setTitle(String title) { 58 this.title = title; 59 } 60 61 public String getContent() { 62 return content; 63 } 64 65 public void setContent(String content) { 66 this.content = content; 67 } 68 69 public String getPostTime() { 70 return postTime; 71 } 72 73 public void setPostTime(String postTime) { 74 this.postTime = postTime; 75 } 76 77 public String getClickCount() { 78 return clickCount; 79 } 80 81 public void setClickCount(String clickCount) { 82 this.clickCount = clickCount; 83 } 84 85 public Author getAuthor() { 86 return author; 87 } 88 89 public void setAuthor(Author author) { 90 this.author = author; 91 } 92 93 public Tutorial getTutorial() { 94 return tutorial; 95 } 96 97 public void setTutorial(Tutorial tutorial) { 98 this.tutorial = tutorial; 99 } 100 101 public String getAbstracts() { 102 return abstracts; 103 } 104 105 public void setAbstracts(String abstracts) { 106 this.abstracts = abstracts; 107 } 108 }
Author.java
1 package com.slp.springbootelasticsearch.pojo; 2 3 import java.io.Serializable; 4 5 /** 6 * @author sanglp 7 * @create 2018-07-04 9:04 8 * @desc 作者實體類 9 **/ 10 public class Author implements Serializable{ 11 /** 12 * 作者ID 13 */ 14 private Long id; 15 /** 16 * 作者姓名 17 */ 18 private String name; 19 /** 20 * 作者簡介 21 */ 22 private String remark; 23 24 public Long getId() { 25 return id; 26 } 27 28 public void setId(Long id) { 29 this.id = id; 30 } 31 32 public String getName() { 33 return name; 34 } 35 36 public void setName(String name) { 37 this.name = name; 38 } 39 40 public String getRemark() { 41 return remark; 42 } 43 44 public void setRemark(String remark) { 45 this.remark = remark; 46 } 47 }
Tutorial.java
1 package com.slp.springbootelasticsearch.pojo; 2 3 import java.io.Serializable; 4 5 /** 6 * @author sanglp 7 * @create 2018-07-04 9:03 8 * @desc 實體類 9 **/ 10 public class Tutorial implements Serializable { 11 private Long id; 12 //教程名稱 13 private String name ; 14 15 public Long getId() { 16 return id; 17 } 18 19 public void setId(Long id) { 20 this.id = id; 21 } 22 23 public String getName() { 24 return name; 25 } 26 27 public void setName(String name) { 28 this.name = name; 29 } 30 }
@Document注解里面的幾個屬性,類比mysql的話是這樣:
index –> DB
type –> Table
Document –> row
@Id注解加上后,在Elasticsearch里相應於該列就是主鍵了,在查詢時就可以直接用主鍵查詢,后面再看。其實和mysql非常類似,基本就是一個數據庫。
1 @Persistent 2 @Inherited 3 @Retention(RetentionPolicy.RUNTIME) 4 @Target({ElementType.TYPE}) 5 public @interface Document { 6 7 String indexName();//索引庫的名稱,個人建議以項目的名稱命名 8 9 String type() default "";//類型,個人建議以實體的名稱命名 10 11 short shards() default 5;//默認分區數 12 13 short replicas() default 1;//每個分區默認的備份數 14 15 String refreshInterval() default "1s";//刷新間隔 16 17 String indexStoreType() default "fs";//索引文件存儲類型 18 }
加上了@Document注解之后,默認情況下這個實體中所有的屬性都會被建立索引、並且分詞。
我們通過@Field注解來進行詳細的指定,如果沒有特殊需求,那么只需要添加@Document即可。
@Field注解的定義如下: @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) @Documented @Inherited public @interface Field { FieldType type() default FieldType.Auto;#自動檢測屬性的類型 FieldIndex index() default FieldIndex.analyzed;#默認情況下分詞 DateFormat format() default DateFormat.none; String pattern() default ""; boolean store() default false;#默認情況下不存儲原文 String searchAnalyzer() default "";#指定字段搜索時使用的分詞器 String indexAnalyzer() default "";#指定字段建立索引時指定的分詞器 String[] ignoreFields() default {};#如果某個字段需要被忽略 boolean includeInParent() default false; }
ArticleSearchRepository.java相當於dao
1 package com.slp.springbootelasticsearch.repository; 2 3 import com.slp.springbootelasticsearch.pojo.Article; 4 import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; 5 6 /** 7 * @author sanglp 8 * @create 2018-07-04 9:27 9 * @desc 文章reposiroty 泛型的參數分別是實體類型和主鍵類型 10 **/ 11 public interface ArticleSearchRepository extends ElasticsearchRepository<Article,Long> { 12 13 }
測試用例
package com.slp.springbootelasticsearch; import com.slp.springbootelasticsearch.pojo.Article; import com.slp.springbootelasticsearch.pojo.Author; import com.slp.springbootelasticsearch.pojo.Tutorial; import com.slp.springbootelasticsearch.repository.ArticleSearchRepository; import org.elasticsearch.index.query.QueryStringQueryBuilder; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.Iterator; @RunWith(SpringRunner.class) @SpringBootTest public class SpringbootElasticsearchApplicationTests { @Test public void contextLoads() { } @Autowired private ArticleSearchRepository articleSearchRepository; @Test public void testSaveArticleIndex(){ Author author = new Author(); author.setId(1L); author.setName("slp"); author.setRemark("test"); Tutorial tutorial = new Tutorial(); tutorial.setId(1L); tutorial.setName("elastic search"); Article article = new Article(); article.setId(1L); article.setTitle("spring boot integrate elasticsearch"); article.setAbstracts("elasticsearch is very easy"); article.setTutorial(tutorial); article.setAuthor(author); article.setContent("elasticsearch based on lucene"); article.setPostTime("20180704"); article.setClickCount("1"); articleSearchRepository.save(article); } @Test public void testSearch(){ String queryString="spring";//搜索關鍵字 QueryStringQueryBuilder builder=new QueryStringQueryBuilder(queryString); Iterable<Article> searchResult = articleSearchRepository.search(builder); Iterator<Article> iterator = searchResult.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next().getAbstracts()); } } }
問題:
1、Exception in thread "main" org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available:
先直接訪問,並查看是否是配置有誤,切記要關閉防火牆!!!
