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
# 連接節(jié)點(diǎn),注意在集群中通信都是9300端口,否則會報(bào)錯無法連接上煎谍!
spring.data.elasticsearch.cluster-nodes=10.1.109.163:9300
# 是否本地連接
spring.data.elasticsearch.local=false
# 倉庫中數(shù)據(jù)存儲
spring.data.elasticsearch.repositories.enabled=true
2、springboot 調(diào)用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中字段名
查詢關(guān)鍵字包括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);
}
其中枢步,分頁P(yáng)ageable的構(gòu)造方法是:Pageable pageable = new PageRequest(pageNumber, pageSize);
更多使用方法參考:
https://github.com/spring-projects/spring-data-elasticsearch
深入淺出 spring-data-elasticsearch – 實(shí)戰(zhàn)案例詳解(四)
這樣沉删,就可以使用autowired來注入Repository實(shí)例
@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、查詢索引
根據(jù)Id查詢
GetResponse getResponse = transportClient.prepareGet(index, type, "1").get();
System.out.println(getResponse.getSourceAsString());
復(fù)雜的條件查詢SearchResponse
SearchResponse searchResponse = transportClient.prepareSearch("es-customer")
.setTypes("customer")
.setQuery(QueryBuilders.matchAllQuery()) //查詢所有
//.setQuery(QueryBuilders.matchQuery("name", "tom").operator(Operator.AND)) //根據(jù)tom分詞查詢name,默認(rèn)or
//.setQuery(QueryBuilders.multiMatchQuery("tom", "name", "age")) //指定查詢的字段
//.setQuery(QueryBuilders.queryString("name:to* AND age:[0 TO 19]")) //根據(jù)條件查詢,支持通配符大于等于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及其簡單應(yīng)用
https://github.com/whiney/springboot-elasticsearch
https://github.com/adalaohu/springboot-elasticSearch
二、Elasticsearch的功能推正、使用場景以及特點(diǎn)
https://github.com/spring-projects/spring-data-elasticsearch