spring data elastic search初识

首先pom文件里依赖:

		<!-- 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);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值