Spring Boot 2.0 Spring Data ElasticSearch入门

首先安装好ElasticSearch,我安装的是elasticsearch-5.6.9, 并且安装了IK中文分词插件。

第一步,pom.xml里添加spring-boot-starter-data-elasticsearch

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.4.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-data-elasticsearch
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.projectlombok
            lombok
            1.16.20
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            spring-libs-release
            Spring Releases
            https://repo.spring.io/libs-release
            
                false
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
            
                org.projectlombok
                lombok-maven-plugin
                1.16.20.0
            
        
    

第二步,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 {
}

第五步

@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 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 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

你可能感兴趣的:(Spring Boot 2.0 Spring Data ElasticSearch入门)