spring data elastic search初识
2015-11-30 14:18
393 查看
首先pom文件里依赖:
<elasticsearch: 目前不全,可以改用常用注入bean方式:
定义的文档结构:
Dao
spring data会自动生成repository 的实现类。
也可以写查询的query:
结构bean:
DAO :
t调用查询:
另外一种查询方式:
<!-- Elastic search--> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-elasticsearch</artifactId> <version>1.1.3.RELEASE</version> </dependency>配置elastic
<elasticsearch:repositories base-package="com.*.dao" /> <elasticsearch:transport-client id="client" cluster-nodes="@{elasticsearch.clusterNodes}" cluster-name="@{elasticsearch.clusterName}" /> <bean name="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate"> <constructor-arg name="client" ref="client"/> </bean>
<elasticsearch: 目前不全,可以改用常用注入bean方式:
<bean id="client" class="org.springframework.data.elasticsearch.client.TransportClientFactoryBean" > <property name="clusterNodes" value="ip:port,ip:port"/> <property name="clusterName" value="myelasticsearch"/> <property name="properties" > <props> <prop key="sh" >cass</prop> <prop key=""></prop> </props> </property> </bean>
定义的文档结构:
@Document(indexName = "product-index",type = "PRODUCT",shards = 1, replicas = 1 ,refreshInterval = "-1") public class ProductDocument { private @Id String id; private @Field(type = Nested)List<CityBean> bookCityCode; }
Dao
public interface ProductDocumentRepository extends ElasticsearchRepository<ProductDocument,String> { public ProductDocument findByProductId(long productId); }
spring data会自动生成repository 的实现类。
也可以写查询的query:
结构bean:
@Data @Builder @Document(indexName = "conference-index", shards = 1, replicas = 0, indexStoreType = "memory", refreshInterval = "-1") public class Conference { private @Id String id; private String name; private @Field(type = Date) String date; private @GeoPointField String location; private List<String> keywords; private List<CityBean> cityBeans; // do not remove it public Conference() {} // do not remove it - work around for lombok generated constructor for all params public Conference(String id, String name, String date, String location, List<String> keywords,List<CityBean> cityBeans) { this.id = id; this.name = name; this.date = date; this.location = location; this.keywords = keywords; this.cityBeans=cityBeans; } } public class CityBean { private int code; private String name; }
DAO :
<pre name="code" class="java">public interface ConferenceRepository extends ElasticsearchRepository<Conference, String> { @Query("{\"bool\":{\"must\":[{\"nested\":{\"path\":\"cityBeans\",\"query\":{\"bool\":{\"must\":[{\"match\":{\"cityBeans.code\":?0}}]}}}}]}}" ) public List<Conference> findByCityCode(int code,Pageable pageable); }
t调用查询:
List<Conference> conferences=repository.findByCityCode(1602,new PageRequest(0,20));
另外一种查询方式:
QueryBuilder builder = QueryBuilders.nestedQuery("cityBeans", QueryBuilders.boolQuery().must(termQuery("cityBeans.code", 1602))); QueryBuilder builder1=QueryBuilders.boolQuery().must(builder); Iterable<Conference> result4 =repository.search(builder1);
相关文章推荐
- Eclipse中Outline里各种图标的含义
- java csv 逗号 excel 乱码
- Java集合-Collection整体框架
- 5、Myeclipse最全快捷键
- JavaSE(13)(异常和文件类)
- Spring面试笔记
- Struts面试笔记
- java基础部分
- Unable to execute dex: Java heap space Java heap space
- java.io.Serializable
- Java生产者与消费者模型
- Java进阶思考
- java用xstream实现xml与实体的互相转化
- ldap验证用户(java)<不能以simple协议来获取连接进行连接>
- Java 内部类中使用局部变量必须是final
- 运算符优先级-Java 运算符优先级
- spring开发中commons-logging.jar包的功能
- Eclipse SVN 插件使用
- spring DATA (jpa) @query 和JdbcTemplate
- Spring中初始化二