1、springboot 集成elasticsearch
1.1、pom文件引入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
1.2、配置文件application.xml加入
# 集群名
spring.data.elasticsearch.cluster-name=elasticsearch
# 连接节点,注意在集群中通信都是9300端口,否则会报错无法连接上!
spring.data.elasticsearch.cluster-nodes=10.1.109.163:9300
# 是否本地连接
spring.data.elasticsearch.local=false
# 仓库中数据存储
spring.data.elasticsearch.repositories.enabled=true
2、springboot 调用elasticsearch的多种方式
2.1、使用对象方式,类似mybatis的处理
- 2.1.1、新建DAO对象
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName = "es-customer", type = "customer", shards = 2, replicas = 1, refreshInterval = "-1")
@Getter
@Setter
public class Customer {
@Id
private String id;
private String firstName;
private String lastName;
@Override
public String toString() {
return String.format("Customer[id=%s, firstName='%s', lastName='%s']", this.id,
this.firstName, this.lastName);
}
}
-
2.1.2、新建Repository对象
ElasticsearchRepository接口常用的接口包含save,findAll,delete,index,search等常用接口
接口中自定义方法,在方法名中包含T中字段名
查询关键字包括find…By, read…By, query…By, count…By, and get…By,熟悉直接可以用And and Or连接
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
public interface CustomerRepository extends ElasticsearchRepository<Customer, String> {
public List<Customer> findByFirstName(String firstName);
public Page<Customer> findByLastName(String lastName, Pageable page);
public List<Customer> findByFirstNameAndLastName(String firstName,String lastName);
}
其中,分页Pageable的构造方法是:Pageable pageable = new PageRequest(pageNumber, pageSize);
更多使用方法参考:
https://github.com/spring-projects/spring-data-elasticsearch
深入浅出 spring-data-elasticsearch – 实战案例详解(四)
这样,就可以使用autowired来注入Repository实例
@Autowired
private CustomerRepository repository;
2.2、使用TransportClient操作ES
@Autowired
private TransportClient transportClient;
- 2.2.1、新增索引
IndexResponse indexResponse = transportClient
.prepareIndex(index, type, "3").setSource(source).get();
System.out.println(indexResponse.getVersion());
其中source可以是jsonString,Map,XContentBuilder等对象
- 2.2.2、更新索引
UpdateResponse updateResponse = transportClient
.prepareUpdate(index, type, "6").setDoc(source).get();
System.out.println(updateResponse.getVersion());
- 2.2.3、删除索引
DeleteResponse deleteResponse = transportClient.prepareDelete(index,
type, id).get();
System.out.println(deleteResponse.getVersion());
- 2.2.4、查询索引
根据Id查询
GetResponse getResponse = transportClient.prepareGet(index, type, "1").get();
System.out.println(getResponse.getSourceAsString());
复杂的条件查询SearchResponse
SearchResponse searchResponse = transportClient.prepareSearch("es-customer")
.setTypes("customer")
.setQuery(QueryBuilders.matchAllQuery()) //查询所有
//.setQuery(QueryBuilders.matchQuery("name", "tom").operator(Operator.AND)) //根据tom分词查询name,默认or
//.setQuery(QueryBuilders.multiMatchQuery("tom", "name", "age")) //指定查询的字段
//.setQuery(QueryBuilders.queryString("name:to* AND age:[0 TO 19]")) //根据条件查询,支持通配符大于等于0小于等于19
//.setQuery(QueryBuilders.termQuery("name", "tom"))//查询时不分词
.setSearchType(SearchType.QUERY_THEN_FETCH)
.setFrom(0).setSize(10)//分页
.addSort("lastName", SortOrder.DESC)//排序
.get();
SearchHits hits = searchResponse.getHits();
long total = hits.getTotalHits();
System.out.println(total);
SearchHit[] searchHits = hits.hits();
for(SearchHit s : searchHits)
{
System.out.println(s.getSourceAsString());
}
参考
详解spring-boot集成elasticsearch及其简单应用
https://github.com/whiney/springboot-elasticsearch
https://github.com/adalaohu/springboot-elasticSearch
https://github.com/spring-projects/spring-data-elasticsearch