首先安装好ElasticSearch,我安装的是elasticsearch-5.6.9, 并且安装了IK中文分词插件。
第一步,pom.xml里添加spring-boot-starter-data-elasticsearch
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-libs-release</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-maven-plugin</artifactId>
<version>1.16.20.0</version>
</plugin>
</plugins>
</build>
第二步,application.yml里添加如下配置:
spring:
data:
elasticsearch:
cluster-name: my-application
cluster-nodes: localhost:9300
cluster-name 是你安装的ElasticSearch配置里的集群名称,cluster-nodes是你安装的ElasticSearch节点,如果有多个,以逗号,分隔。
第三步,创建Document
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Document(indexName = ItemDocument.INDEX, type = ItemDocument.TYPE)
public class ItemDocument {
public static final String INDEX = "items";
public static final String TYPE = "item";
/**
* 商品唯一标识
*/
@Id
@Field(type = FieldType.Keyword)
private String id;
/**
* 类目id
*/
@Field(type = FieldType.Integer)
private Integer catId;
/**
* 商品名称
*/
@Field(type = FieldType.Text, index = false)
private String name;
/**
* 商品价格
*/
@Field(type = FieldType.Long)
private Long price;
/**
* 商品的描述
*/
@Field(type = FieldType.Text, searchAnalyzer = "ik_smart", analyzer = "ik_smart")
private String description;
}
第四步,创建Repository
public interface ItemDocumentRepository extends ElasticsearchRepository<ItemDocument, String> {
}
第五步
@SpringBootApplication
@EnableElasticsearchRepositories(basePackages = "com.example.elasticSearch.repository")
public class ElasticSearchApplication {
public static void main(String[] args) {
SpringApplication.run(ElasticSearchApplication.class, args);
}
}
最后测试一下
@RestController
@RequestMapping("/items")
public class SearchController {
@Autowired
private ItemDocumentRepository repository;
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@GetMapping(path = "/{id}")
public ResponseEntity getItem(@PathVariable("id") String id) {
ItemDocument com = repository.findById(id).get();
return new ResponseEntity(com.toString(), HttpStatus.OK);
}
@PostMapping
public ResponseEntity createItem(@RequestBody ItemDocument document) {
repository.save(document);
return new ResponseEntity(document.toString(), HttpStatus.OK);
}
@GetMapping(path = "/singleWord")
public List<ItemDocument> singleTitle(String word, @PageableDefault Pageable pageable) {
//使用queryStringQuery完成单字符串查询
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(queryStringQuery(word)).withPageable(pageable).build();
return elasticsearchTemplate.queryForList(searchQuery, ItemDocument.class);
}
@GetMapping(path = "/singleWord2")
public Page<ItemDocument> singleTitle2(String word, @PageableDefault Pageable pageable) {
//使用queryStringQuery完成单字符串查询
QueryStringQueryBuilder builder = new QueryStringQueryBuilder(word);
return repository.search(builder, pageable);
}
}
创建商品
POST http://localhost:8080/items
body:
{
"id":"1",
"catId":1,
"description":"商品,质量好,包邮,售后服务保障",
"name":"商品123",
"price":1000
}
第二个商品:
{
"id":"2",
"catId":1,
"description":"商品456,结实耐用,包邮,售后服务保障",
"name":"商品456",
"price":200
}
查询商品
GET http://localhost:8080/items/1
搜索商品
GET http://localhost:8080/items/singleWord?word=耐用&size=20
GET http://localhost:8080/items/singleWord2?word=耐用&size=20