SpringBoot整合es

文檔對像java

@Document(indexName = "bigdata",type = "tag")
public class User {

    @Id
	private String openid;
    
	private List<String> tags;

	public String getOpenid() {
		return openid;
	}

	public void setOpenid(String openid) {
		this.openid = openid;
	}

	public List<String> getTags() {
		return tags;
	}

	public void setTags(List<String> tags) {
		this.tags = tags;
	}

}

配置node

spring.data.elasticsearch.cluster-nodes = 10.136.15.122:9300spring

db層操做elasticsearch

package com.dongpeng.es;

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

public interface UserDao  extends ElasticsearchRepository<User,String>{

}

應用層ui

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class Test {

	@Resource
	private UserDao userDao;
	
	@Resource
	private ElasticsearchTemplate elasticsearchTemplate;
	
	@org.junit.Test
	public void test() {
		User user = new User();
		user.setOpenid("test1");
		user.setTags(Arrays.asList("test1","test2"));
		
		userDao.save(user);
		TermQueryBuilder termQueryBuilder = new TermQueryBuilder("tags", "test1");
		TermQueryBuilder termQueryBuilder2 = new TermQueryBuilder("tags", "test2");
		QueryBuilder queryBuilder = QueryBuilders.boolQuery().must(termQueryBuilder).must(termQueryBuilder2);
		
		SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(
                		queryBuilder
                )
                .withIndices("bigdata").withTypes("tag").build();

	
		System.out.println(elasticsearchTemplate.count(searchQuery));
		
		//System.out.println(JSON.toJSONString(userDao.search(queryBuilder)));
	}
}
相關文章
相關標籤/搜索