springframework提供了方便的对接elasticsearch的包。spring-boot-elasticsearch支持spring-boot环境,spring-data-elasticsearch支持其他spring开发环境。
关于elasticsearch的安装和配置,请参考:elasticsearch搜索引擎的搭建注意事项
下面简单介绍spring-data-elasticsearch的配置和应用。
1、xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/elasticsearch
http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-4.0.xsd">
<bean class="com.cpicvip.web.config.ElasticsearchClientConfig">
<constructor-arg name="hostname" value="127.0.0.1"/>
<constructor-arg name="port" value="9200"/>
<property name="username" value="elastic"/>
<property name="password" value="123456"/>
</bean>
<bean id="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate">
<constructor-arg name="client" ref="esClient" />
</bean>
<elasticsearch:repositories base-package="com.example.fullsearch" />
</beans>
2、ElasticsearchRepository<T, ID>的应用
package com.example.fullsearch;
import com.example.model.fullsearch.CustomerIndex;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
public interface CustomerIndexDao extends ElasticsearchRepository<CustomerIndex,Long> {
List<CustomerIndex> findByName(String name, Pageable pageable);
List<CustomerIndex> findByNameAndManualVehicleCountGreaterThanAndCityIdBetween(String name, int manualVehicleCount,int startCityId,int endCityId, Pageable pageable);
List<CustomerIndex> findByAddress(String address,Pageable pageable);
List<CustomerIndex> findByAddressAndManualVehicleCountGreaterThanAndCityIdBetween(String address, int manualVehicleCount,int startCityId,int endCityId,Pageable pageable);
}
实体类:
package com.example.model.fullsearch;
import com.example.model.market.Customer;
import javax.persistence.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import java.util.Date;
@Document(indexName = "customer")
public class CustomerIndex {
public CustomerIndex(){}
public CustomerIndex(Long id,String name){
setId(id);
setName(id);
}
@Id
private Long id;
@Field(analyzer="ik_max_word",searchAnalyzer = "ik_smart")
private String name;//客户名称
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3、ElasticsearchRepository自定义查询方法关键字
Keyword | Sample | Elasticsearch Query String |
---|---|---|
And | findByNameAndPrice | {"bool" : {"must" : [ {"field" : {"name" : "?"}}, {"field" : {"price" : "?"}} ]}} |
Or | findByNameOrPrice | {"bool" : {"should" : [ {"field" : {"name" : "?"}}, {"field" : {"price" : "?"}} ]}} |
Is | findByName | {"bool" : {"must" : {"field" : {"name" : "?"}}}} |
Not | findByNameNot | {"bool" : {"must_not" : {"field" : {"name" : "?"}}}} |
Between | findByPriceBetween | {"bool" : {"must" : {"range" : {"price" : {"from" : ?,"to" : ?,"include_lower" : true,"include_upper" : true}}}}} |
LessThanEqual | findByPriceLessThan | {"bool" : {"must" : {"range" : {"price" : {"from" : null,"to" : ?,"include_lower" : true,"include_upper" : true}}}}} |
GreaterThanEqual | findByPriceGreaterThan | {"bool" : {"must" : {"range" : {"price" : {"from" : ?,"to" : null,"include_lower" : true,"include_upper" : true}}}}} |
Before | findByPriceBefore | {"bool" : {"must" : {"range" : {"price" : {"from" : null,"to" : ?,"include_lower" : true,"include_upper" : true}}}}} |
After | findByPriceAfter | {"bool" : {"must" : {"range" : {"price" : {"from" : ?,"to" : null,"include_lower" : true,"include_upper" : true}}}}} |
Like | findByNameLike | {"bool" : {"must" : {"field" : {"name" : {"query" : "?*","analyze_wildcard" : true}}}}} |
StartingWith | findByNameStartingWith | {"bool" : {"must" : {"field" : {"name" : {"query" : "?*","analyze_wildcard" : true}}}}} |
EndingWith | findByNameEndingWith | {"bool" : {"must" : {"field" : {"name" : {"query" : "*?","analyze_wildcard" : true}}}}} |
Contains/Containing | findByNameContaining | {"bool" : {"must" : {"field" : {"name" : {"query" : "**?**","analyze_wildcard" : true}}}}} |
In | findByNameIn(Collection<String>names) | {"bool" : {"must" : {"bool" : {"should" : [ {"field" : {"name" : "?"}}, {"field" : {"name" : "?"}} ]}}}} |
NotIn | findByNameNotIn(Collection<String>names) | {"bool" : {"must_not" : {"bool" : {"should" : {"field" : {"name" : "?"}}}}}} |
Near | findByStoreNear | Not Supported Yet ! |
True | findByAvailableTrue | {"bool" : {"must" : {"field" : {"available" : true}}}} |
False | findByAvailableFalse | {"bool" : {"must" : {"field" : {"available" : false}}}} |
OrderBy | findByAvailableTrueOrderByNameDesc | {"sort" : [{ "name" : {"order" : "desc"} }],"bool" : {"must" : {"field" : {"available" : true}}}} |
4、ElasticsearchRestTemplate的应用
ElasticsearchRestTemplate是高级类