本文整理汇总了Java中org.elasticsearch.search.SearchHits.getHits方法的典型用法代码示例。如果您正苦于以下问题:Java SearchHits.getHits方法的具体用法?Java SearchHits.getHits怎么用?Java SearchHits.getHits使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.elasticsearch.search.SearchHits
的用法示例。
在下文中一共展示了SearchHits.getHits方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doubleNotTest
import org.elasticsearch.search.SearchHits; //导入方法依赖的package包/类
@Test
public void doubleNotTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException {
SearchHits response1 = query(String.format("SELECT * FROM %s/account WHERE not gender like 'm' and not gender like 'f'", TEST_INDEX));
Assert.assertEquals(0, response1.getTotalHits());
SearchHits response2 = query(String.format("SELECT * FROM %s/account WHERE not gender like 'm' and gender not like 'f'", TEST_INDEX));
Assert.assertEquals(0, response2.getTotalHits());
SearchHits response3 = query(String.format("SELECT * FROM %s/account WHERE gender not like 'm' and gender not like 'f'", TEST_INDEX));
Assert.assertEquals(0, response3.getTotalHits());
SearchHits response4 = query(String.format("SELECT * FROM %s/account WHERE gender like 'm' and not gender like 'f'", TEST_INDEX));
// assert there are results and they all have gender 'm'
Assert.assertNotEquals(0, response4.getTotalHits());
for (SearchHit hit : response4.getHits()) {
Assert.assertEquals("m", hit.getSource().get("gender").toString().toLowerCase());
}
SearchHits response5 = query(String.format("SELECT * FROM %s/account WHERE NOT (gender = 'm' OR gender = 'f')", TEST_INDEX));
Assert.assertEquals(0, response5.getTotalHits());
}
示例2: jsonquery
import org.elasticsearch.search.SearchHits; //导入方法依赖的package包/类
/**
* json查询
*/
public static void jsonquery() {
try {
Settings settings = Settings.settingsBuilder().put("cluster.name", "elasticsearch1").build();
TransportClient transportClient = TransportClient.builder().
settings(settings).build().addTransportAddress(
new InetSocketTransportAddress(InetAddress.getByName("172.16.2.93"), 9300));
SearchRequestBuilder searchRequestBuilder = transportClient.prepareSearch("service2");
SearchResponse searchResponse = searchRequestBuilder.setSource("{\n" +
"\"query\": {\n" +
"\"bool\": {\n" +
"\"must\": [\n" +
"{\n" +
"\"prefix\": {\n" +
"\"content\": \"oracle\"\n" +
"}\n" +
"}\n" +
"],\n" +
"\"must_not\": [ ],\n" +
"\"should\": [ ]\n" +
"}\n" +
"},\n" +
"\"from\": 0,\n" +
"\"size\": 10,\n" +
"\"sort\": [ ],\n" +
"\"aggs\": { }\n" +
"}")
.get();
SearchHits searchHits = searchResponse.getHits();
System.out.println();
System.out.println("Total Hits is " + searchHits.totalHits());
System.out.println();
for (int i = 0; i < searchHits.getHits().length; ++i) {
System.out.println("content is "
+ searchHits.getHits()[i].getSource().get("content"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
示例3: queryByEsQueryDo
import org.elasticsearch.search.SearchHits; //导入方法依赖的package包/类
public EsResponse queryByEsQueryDo(EsQueryDo esQueryObj) throws EsException {
validationEsQuery(esQueryObj.getIndexName(),esQueryObj.getTypeName());
//创建ES查询Request对象
SearchRequestBuilder esSearch=buildSearchRequest (esQueryObj);
//执行查询
SearchResponse response =esSearch.execute().actionGet();
JSONObject resObj = new JSONObject();
//获取facet结果
if(!Check.NuNObject(esQueryObj.aggregationFields())){
parseAggregationResult(response, esQueryObj.aggregationFields(), resObj);
}
//1、获取搜索的文档结果
SearchHits searchHits = response.getHits();
if (searchHits == null || searchHits.getTotalHits() == 0) {
return EsResponse.responseOK(null);
}
SearchHit[] hits = searchHits.getHits();
resObj.put("total", searchHits.getTotalHits());
//1.1、获取搜索结果
parseSearchResult(hits, esQueryObj.isHighLigth(), esQueryObj, resObj);
return EsResponse.responseOK(resObj);
}
示例4: complexConditionQuery
import org.elasticsearch.search.SearchHits; //导入方法依赖的package包/类
@Test
public void complexConditionQuery() throws IOException, SqlParseException, SQLFeatureNotSupportedException{
String errorMessage = "Result does not exist to the condition (gender='m' AND (age> 25 OR account_number>5)) OR (gender='f' AND (age>30 OR account_number < 8)";
SearchHits response = query(String.format("SELECT * FROM %s/account WHERE (gender='m' AND (age> 25 OR account_number>5)) OR (gender='f' AND (age>30 OR account_number < 8))", TEST_INDEX));
SearchHit[] hits = response.getHits();
for(SearchHit hit : hits) {
Map<String, Object> source = hit.getSource();
String gender = ((String)source.get("gender")).toLowerCase();
int age = (int)source.get("age");
int account_number = (int) source.get("account_number");
Assert.assertTrue(errorMessage, (gender.equals("m") && (age> 25 || account_number>5)) || (gender.equals("f") && (age>30 || account_number < 8)));
}
}
示例5: complexNotConditionQuery
import org.elasticsearch.search.SearchHits; //导入方法依赖的package包/类
@Test
public void complexNotConditionQuery() throws IOException, SqlParseException, SQLFeatureNotSupportedException {
String errorMessage = "Result does not exist to the condition NOT (gender='m' AND NOT (age > 25 OR account_number > 5)) OR (NOT gender='f' AND NOT (age > 30 OR account_number < 8))";
SearchHits response = query(String.format("SELECT * FROM %s/account WHERE NOT (gender='m' AND NOT (age > 25 OR account_number > 5)) OR (NOT gender='f' AND NOT (age > 30 OR account_number < 8))", TEST_INDEX));
SearchHit[] hits = response.getHits();
Assert.assertNotEquals(hits.length, 0);
for (SearchHit hit : hits) {
Map<String, Object> source = hit.getSource();
String gender = ((String) source.get("gender")).toLowerCase();
int age = (int) source.get("age");
int account_number = (int) source.get("account_number");
Assert.assertTrue(errorMessage, !(gender.equals("m") && !(age > 25 || account_number > 5)) || (!gender.equals("f") && !(age > 30 || account_number < 8)));
}
}
示例6: likeTest
import org.elasticsearch.search.SearchHits; //导入方法依赖的package包/类
@Test
public void likeTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException {
SearchHits response = query(String.format("SELECT * FROM %s WHERE firstname LIKE 'amb%%' LIMIT 1000", TEST_INDEX));
SearchHit[] hits = response.getHits();
// assert the results is correct according to accounts.json data.
Assert.assertEquals(1, response.getTotalHits());
Assert.assertEquals("Amber", hits[0].getSource().get("firstname"));
}
示例7: search
import org.elasticsearch.search.SearchHits; //导入方法依赖的package包/类
/**
* 查询
*
* @param queryBuilder
* @param type
* @param sortField 排序字段
* @param desc
* @param from 分页起始偏移量
* @param size 页面大小
* @return
*/
private static <T> Pagination<T> search(QueryBuilder queryBuilder, IndexType type, String sortField, boolean desc, int from, int size) {
List<T> results = new ArrayList<>();
//不同的索引 变量 代码通用
SearchRequestBuilder serachBuilder = EsClientManager.getClient().prepareSearch(esprop.getIndice())
.setTypes(type.getDataName())
.setQuery(queryBuilder);
if (!StringUtils.isEmpty(sortField)) {
if (sortField.equalsIgnoreCase(SortMode.ORDERVALUE_ASC.getSortField())) {
serachBuilder.addSort("_score", SortOrder.DESC);
}
serachBuilder.addSort(sortField, desc ? SortOrder.DESC : SortOrder.ASC);
}
if (0 != size) {
serachBuilder.setFrom(from).setSize(size);
}
SearchResponse response = serachBuilder.execute().actionGet();
SearchHits searchHits = response.getHits();
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits) {
results.add((T) EsDataUtil.readValue(hit.source(), type.getTypeClass()));
}
int total = (int) searchHits.getTotalHits();
Pagination pagination = new Pagination();
pagination.setDataList(results);
pagination.setTotalCount(total);
return pagination;
}
示例8: inTermsTestWithStrings
import org.elasticsearch.search.SearchHits; //导入方法依赖的package包/类
@Test
public void inTermsTestWithStrings() throws IOException, SqlParseException, SQLFeatureNotSupportedException{
SearchHits response = query(String.format("SELECT name FROM %s/gotCharacters WHERE name.firstname = IN_TERMS('daenerys','eddard') LIMIT 1000", TEST_INDEX));
SearchHit[] hits = response.getHits();
Assert.assertEquals(2, response.getTotalHits());
for(SearchHit hit : hits) {
String firstname = ((Map<String,Object>) hit.getSource().get("name")).get("firstname").toString();
assertThat(firstname, isOneOf("Daenerys", "Eddard"));
}
}
示例9: notMissFilterSearch
import org.elasticsearch.search.SearchHits; //导入方法依赖的package包/类
@Test
public void notMissFilterSearch() throws IOException, SqlParseException, SQLFeatureNotSupportedException{
SearchHits response = query(String.format("SELECT * FROM %s/phrase WHERE insert_time2 IS NOT missing", TEST_INDEX));
SearchHit[] hits = response.getHits();
// should be 2 according to the data.
Assert.assertEquals(response.getTotalHits(), 2);
for(SearchHit hit : hits) {
assertThat(hit.getSource(), hasKey("insert_time2"));
}
}
示例10: betweenTest
import org.elasticsearch.search.SearchHits; //导入方法依赖的package包/类
@Test
public void betweenTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException {
int min = 27;
int max = 30;
SearchHits response = query(String.format("SELECT * FROM %s WHERE age BETWEEN %s AND %s LIMIT 1000", TEST_INDEX, min, max));
SearchHit[] hits = response.getHits();
for(SearchHit hit : hits) {
int age = (int) hit.getSource().get("age");
assertThat(age, allOf(greaterThanOrEqualTo(min), lessThanOrEqualTo(max)));
}
}
示例11: inTermsTestWithNumbers
import org.elasticsearch.search.SearchHits; //导入方法依赖的package包/类
@Test
public void inTermsTestWithNumbers() throws IOException, SqlParseException, SQLFeatureNotSupportedException{
SearchHits response = query(String.format("SELECT name FROM %s/gotCharacters WHERE name.ofHisName = IN_TERMS(4,2) LIMIT 1000", TEST_INDEX));
SearchHit[] hits = response.getHits();
Assert.assertEquals(1, response.getTotalHits());
SearchHit hit = hits[0];
String firstname = ((Map<String,Object>) hit.getSource().get("name")).get("firstname").toString();
Assert.assertEquals("Brandon",firstname);
}
示例12: orderByDescTest
import org.elasticsearch.search.SearchHits; //导入方法依赖的package包/类
@Test
public void orderByDescTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException {
SearchHits response = query(String.format("SELECT age FROM %s/account ORDER BY age DESC LIMIT 1000", TEST_INDEX));
SearchHit[] hits = response.getHits();
ArrayList<Integer> ages = new ArrayList<Integer>();
for(SearchHit hit : hits) {
ages.add((int)hit.getSource().get("age"));
}
ArrayList<Integer> sortedAges = (ArrayList<Integer>)ages.clone();
Collections.sort(sortedAges, Collections.reverseOrder());
Assert.assertTrue("The list is not ordered descending", sortedAges.equals(ages));
}
示例13: termQueryWithStringLiteral
import org.elasticsearch.search.SearchHits; //导入方法依赖的package包/类
@Test
public void termQueryWithStringLiteral() throws IOException, SqlParseException, SQLFeatureNotSupportedException{
SearchHits response = query(String.format("SELECT name FROM %s/gotCharacters WHERE name.firstname = term('brandon') LIMIT 1000", TEST_INDEX));
SearchHit[] hits = response.getHits();
Assert.assertEquals(1, response.getTotalHits());
SearchHit hit = hits[0];
String firstname = ((Map<String,Object>) hit.getSource().get("name")).get("firstname").toString();
Assert.assertEquals("Brandon",firstname);
}
示例14: notInTest
import org.elasticsearch.search.SearchHits; //导入方法依赖的package包/类
@Test
public void notInTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException {
SearchHits response = query(String.format("SELECT age FROM %s WHERE age NOT IN (20, 22) LIMIT 1000", TEST_INDEX));
SearchHit[] hits = response.getHits();
for(SearchHit hit : hits) {
Map<String, Object> source = hit.getSource();
// ignore document which not contains the age field.
if(source.containsKey("age")) {
int age = (int) source.get("age");
assertThat(age, not(isOneOf(20, 22)));
}
}
}
示例15: notLikeTests
import org.elasticsearch.search.SearchHits; //导入方法依赖的package包/类
@Test
public void notLikeTests() throws IOException, SqlParseException, SQLFeatureNotSupportedException{
//cant use string.format cause of %d
SearchHits response = query("SELECT name FROM " +TEST_INDEX + "/gotCharacters where name.firstname not like '%d' LIMIT 1000");
Assert.assertEquals(3, response.getTotalHits());
for(SearchHit hit : response.getHits()) {
Map<String, Object> sourceAsMap = hit.sourceAsMap();
String name = ((HashMap<String, Object>) sourceAsMap.get("name")).get("firstname").toString();
Assert.assertFalse(name+" was in not like %d",name.startsWith("d"));
}
}