/**
* @author Geloin
*/
package com.geloin.esample.util;
import java.util.UUID;
import junit.framework.Assert;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.junit.Test;
import com.geloin.esample.BaseTest;
import com.geloin.esample.entity.Person;
/**
* @author Geloin
*
*/
public class MatchAllQueryTest extends BaseTest {
@Test
public void matchAllQuery() {
try {
BulkRequestBuilder builder = client.prepareBulk();
// 创建索引
for (int i = 0; i < 10; i++) {
Person p = new Person();
p.setAge(20 + i);
p.setId(UUID.randomUUID().toString());
p.setIsStudent(true);
p.setName("小别克听老别克讲别克的故事" + i);
p.setSex("男");
String source = ElasticSearchUtil.BeanToJson(p);
IndexRequest request = client
.prepareIndex(index, type, p.getId()).setSource(source)
.request();
builder.add(request);
}
BulkResponse response = builder.execute().actionGet();
if (response.hasFailures()) {
Assert.fail("创建索引失败!");
}
// 检索
QueryBuilder qb = QueryBuilders.matchAllQuery();
SearchResponse sr = client.prepareSearch(index).setTypes(type)
// .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(qb).setFrom(0).setSize(12).execute().actionGet();
SearchHits hits = sr.getHits();
if (null != hits && hits.totalHits() > 0) {
for (SearchHit hit : hits) {
String json = hit.getSourceAsString();
Person newPerson = mapper.readValue(json, Person.class);
System.out.println("name\t\t" + newPerson.getName());
System.out.println("sex\t\t" + newPerson.getSex());
System.out.println("age\t\t" + newPerson.getAge());
System.out.println("isStudent\t\t"
+ newPerson.getIsStudent());
}
} else {
log.error("没有查询到任何内容!");
return;
}
// 查询总数量
long count = client.prepareCount(index).setTypes(type).setQuery(qb).execute().actionGet().count();
log.info("符合条件的总数量为:" + count);
Thread.sleep(1000000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
esample.rar (29个子文件)
esample
.project 559B
src
test
resources
java
com
geloin
esample
BaseTest.java 687B
util
TermQueryTest.java 2KB
MatchQueryTest.java 2KB
MatchAllQueryTest.java 3KB
main
resources
config
config.properties 48B
paoding
paoding-analyzer.properties 1KB
log4j.properties 1KB
java
com
geloin
esample
util
ElasticSearchUtil.java 2KB
PropertyManager.java 1KB
LoggerFactory.java 1KB
entity
Person.java 1000B
target
classes
com
geloin
esample
util
TermQueryTest.class 4KB
PropertyManager.class 2KB
ElasticSearchUtil.class 3KB
MatchQueryTest.class 5KB
MatchAllQueryTest.class 6KB
LoggerFactory.class 2KB
entity
Person.class 1KB
BaseTest.class 1KB
config
config.properties 48B
paoding
paoding-analyzer.properties 1KB
log4j.properties 1KB
test-classes
com
geloin
esample
AppTest.class 623B
.settings
org.eclipse.m2e.core.prefs 90B
org.eclipse.jdt.core.prefs 243B
org.eclipse.core.resources.prefs 88B
pom.xml 2KB
.classpath 955B
共 29 条
- 1
KorbinLuo
- 粉丝: 590
- 资源: 25
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- C语言-leetcode题解之70-climbing-stairs.c
- C语言-leetcode题解之68-text-justification.c
- C语言-leetcode题解之66-plus-one.c
- C语言-leetcode题解之64-minimum-path-sum.c
- C语言-leetcode题解之63-unique-paths-ii.c
- C语言-leetcode题解之62-unique-paths.c
- C语言-leetcode题解之61-rotate-list.c
- C语言-leetcode题解之59-spiral-matrix-ii.c
- C语言-leetcode题解之58-length-of-last-word.c
- 计算机编程课程设计基础教程
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页