1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 2 <modelVersion>4.0.0</modelVersion> 3 <groupId>elasticsearch</groupId> 4 <artifactId>elasticsearch</artifactId> 5 <version>0.0.1-SNAPSHOT</version> 6 7 <parent> 8 <groupId>org.springframework.boot</groupId> 9 <artifactId>spring-boot-starter-parent</artifactId> 10 <version>1.5.8.RELEASE</version> 11 </parent> 12 13 <properties> 14 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 15 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 16 <java.version>1.8</java.version> 17 </properties> 18 19 <dependencies> 20 <dependency> 21 <groupId>org.springframework.boot</groupId> 22 <artifactId>spring-boot-starter-data-elasticsearch</artifactId> 23 </dependency> 24 <dependency> 25 <groupId>org.springframework.boot</groupId> 26 <artifactId>spring-boot-starter-web</artifactId> 27 </dependency> 28 <dependency> 29 <groupId>net.java.dev.jna</groupId> 30 <artifactId>jna</artifactId> 31 <version>3.0.9</version> 32 </dependency> 33 34 <dependency> 35 <groupId>org.springframework.boot</groupId> 36 <artifactId>spring-boot-starter-test</artifactId> 37 <scope>test</scope> 38 </dependency> 39 </dependencies> 40 41 <build> 42 <resources> 43 <resource> 44 <directory>src/main/resources/${build.profile.id}</directory> 45 <filtering>false</filtering> 46 </resource> 47 </resources> 48 <plugins> 49 <!-- spring boot maven插件,可以将项目打包成一个可执行的jar文件 --> 50 <plugin> 51 <groupId>org.springframework.boot</groupId> 52 <artifactId>spring-boot-maven-plugin</artifactId> 53 <configuration> 54 <!-- 该配置使devtools生效 --> 55 <fork>true</fork> 56 </configuration> 57 </plugin> 58 </plugins> 59 <finalName>newtouch-ms-platform</finalName> 60 </build> 61 62 </project>
1 spring.data.elasticsearch.cluster-name=elasticsearch 2 spring.data.elasticsearch.cluster-nodes=170.20.1.115:9200 3 spring.data.elasticsearch.repositories.enable=true
package com.newtouch.elasticsearch.elasticsearch.repository; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.stereotype.Component; import com.newtouch.elasticsearch.elasticsearch.document.BookDocument; @Component public interface BookSearchRepository extends ElasticsearchRepository<BookDocument, Long>{ }
package com.newtouch.elasticsearch.elasticsearch.document; import org.springframework.data.annotation.Id; import org.springframework.data.elasticsearch.annotations.Document; @Document(indexName ="megacorp",type = "book", shards = 1,replicas = 0, refreshInterval = "-1") public class BookDocument { @Id private int id; private String name; private String title; private String auther; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuther() { return auther; } public void setAuther(String auther) { this.auther = auther; } }
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 }
@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; }
可以参考:http://www.tianshouzhi.com/api/tutorials/elasticsearch/159