Java Code Examples for io.searchbox.core.SearchResult#Hit

The following examples show how to use io.searchbox.core.SearchResult#Hit . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SearchServiceImpl.java    From light-reading-cloud with MIT License 5 votes vote down vote up
/**
 * ES 执行查询结果
 * @param query
 * @return
 */
private SearchBookResult getSearchResult(String query){
    SearchBookResult result = new SearchBookResult();
    // 封装查询对象
    Search search = new Search.Builder(query)
            .addIndex(aliasName)
            .addType(indexType).build();

    // 执行查询
    try {
        SearchResult searchResult = this.jestClient.execute(search);
        List<SearchBookItem> bookList;
        if (searchResult.isSucceeded()) {
            // 查询成功,处理结果项
            List<SearchResult.Hit<SearchBookItem, Void>> hitList = searchResult.getHits(SearchBookItem.class);
            bookList = new ArrayList<>(hitList.size());
            for (SearchResult.Hit<SearchBookItem, Void> hit : hitList) {
                bookList.add(hit.source);
            }
        } else {
            bookList = new ArrayList<>();
        }

        // 赋值
        result.setTotal(searchResult.getTotal());
        result.setBookList(bookList);
    } catch (IOException e) {
        LOGGER.error("查询图书异常,查询语句:{}", query, e);
    }
    return result;
}
 
Example 2
Source File: JestClient.java    From wES with MIT License 5 votes vote down vote up
@Override
public <T> WSearchResult<T> searchObj(String index, String type, SearchQuery query, Class<T> cls) throws
		Exception {
	Search.Builder builder = new Search.Builder(convert.toText(query));
	if (index != null) {
		builder.addIndex(index);
	}
	if (type != null) {
		builder.addType(type);
	}
	SearchResult result = _exec(builder.build());
	if (result != null) {
		WSearchResult<T> wresult = new WSearchResult<>();
		WSearchResultHits<T> hits = new WSearchResultHits<>();
		hits.setTotal(result.getTotal());
		List<SearchResult.Hit<T, Void>> allHist = result.getHits(cls);
		List<WEsDoc<T>> data = new ArrayList<>();
		for (SearchResult.Hit<T, Void> hit : allHist) {
			WEsDoc<T> doc = new WEsDoc<>();
			doc.setIndex(hit.index);
			doc.setType(hit.type);
			doc.setIndex(hit.index);
			doc.setSource(hit.source);
			doc.setScore(hit.score);
			data.add(doc);
		}
		hits.setHits(data);
		wresult.setHits(hits);
		return wresult;
	}
	return null;
}
 
Example 3
Source File: IndexerResultsMapper.java    From eplmp with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Map results from elasticsearch to document revisions
 *
 * @param searchResult
 * @param documentSearchQuery
 * @return
 */
public List<DocumentRevision> processSearchResult(SearchResult searchResult, DocumentSearchQuery documentSearchQuery) {
    List<SearchResult.Hit<Map, Void>> hits = searchResult.getHits(Map.class);
    Set<DocumentIterationKey> documentIterationKeys = new HashSet<>();

    if (hits != null) {
        for (SearchResult.Hit<Map, Void> hit : hits) {
            Map<?, ?> source = hit.source;
            documentIterationKeys.add(getDocumentIterationKey(source));
        }
    }

    LOGGER.log(Level.INFO, "Results: " + documentIterationKeys.size());
    return documentIterationKeysToDocumentRevisions(documentSearchQuery.isFetchHeadOnly(), documentIterationKeys);
}
 
Example 4
Source File: IndexerResultsMapper.java    From eplmp with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Map results from elasticsearch to part revisions
 *
 * @param searchResult
 * @param partSearchQuery
 * @return
 */
public List<PartRevision> processSearchResult(SearchResult searchResult, PartSearchQuery partSearchQuery) {
    List<SearchResult.Hit<Map, Void>> hits = searchResult.getHits(Map.class);
    Set<PartIterationKey> partIterationKeys = new HashSet<>();

    if (hits != null) {
        for (SearchResult.Hit<Map, Void> hit : hits) {
            Map<?, ?> source = hit.source;
            partIterationKeys.add(getPartIterationKey(source));
        }
    }

    LOGGER.log(Level.INFO, "Results: " + partIterationKeys.size());
    return partIterationKeysToPartRevisions(partSearchQuery.isFetchHeadOnly(), partIterationKeys);
}
 
Example 5
Source File: ElasticsearchService.java    From c2mon with GNU Lesser General Public License v3.0 5 votes vote down vote up
private List<Object[]> getRawHistory(Long id, Long min, Long max) {
  String query = String.format("{\n" +
      "  \"size\" : " + maxResults + ",\n" +
      "  \"query\" : {\n" +
      "    \"bool\" : {\n" +
      "      \"must\" : [ {\n" +
      "        \"term\" : {\n" +
      "          \"id\" : %d\n" +
      "        }\n" +
      "      }, {\n" +
      "        \"range\" : {\n" +
      "          \"timestamp\" : {\n" +
      "            \"from\" : %d,\n" +
      "            \"to\" : %d,\n" +
      "            \"include_lower\" : true,\n" +
      "            \"include_upper\" : true\n" +
      "          }\n" +
      "        }\n" +
      "      } ]\n" +
      "    }\n" +
      "  },\n" +
      "  \"sort\" : [ {\n" +
      "    \"timestamp\" : {\n" +
      "      \"order\" : \"asc\"\n" +
      "    }\n" +
      "  } ]\n" +
      "}", id, min, max);
  Search search = new Search.Builder(query).addIndex(timeSeriesIndex).build();
  try {
    SearchResult result = client.execute(search);
    List<Object[]> results = new ArrayList<>();
    for (SearchResult.Hit<Map, Void> hit : result.getHits(Map.class)) {
      results.add(new Object[]{hit.source.get("timestamp"), hit.source.get("value")});
    }
    return results;
  } catch (IOException e) {
    throw new RuntimeException("Error querying raw tag history", e);
  }

}
 
Example 6
Source File: IndexSearchDaoImpl.java    From herd with Apache License 2.0 5 votes vote down vote up
/**
 * Extracts highlighted content from a given {@link SearchHit}
 *
 * @param searchHit a given {@link SearchHit} from the elasticsearch results
 * @param preTag the specified pre-tag for highlighting
 * @param postTag the specified post-tag for highlighting
 *
 * @return {@link Highlight} a cleaned highlighted content
 */
private Highlight extractHighlightedContent(SearchResult.Hit<Map, Void> searchHit, String preTag, String postTag)
{
    Highlight highlightedContent = new Highlight();

    List<Field> highlightFields = new ArrayList<>();

    // make sure there is highlighted content in the search hit
    if (MapUtils.isNotEmpty(searchHit.highlight))
    {
        Set<String> keySet = searchHit.highlight.keySet();

        for (String key : keySet)
        {
            Field field = new Field();

            // Extract the field-name
            field.setFieldName(key);

            List<String> cleanFragments = new ArrayList<>();

            // Extract fragments which have the highlighted content
            List<String> fragments = searchHit.highlight.get(key);

            for (String fragment : fragments)
            {
                cleanFragments.add(HerdStringUtils.stripHtml(fragment, preTag, postTag));
            }
            field.setFragments(cleanFragments);
            highlightFields.add(field);
        }
    }

    highlightedContent.setFields(highlightFields);

    return highlightedContent;
}