Java Code Examples for org.elasticsearch.search.SearchHit#getIndex()

The following examples show how to use org.elasticsearch.search.SearchHit#getIndex() . 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: JsonContent.java    From elasticsearch-dataformat with Apache License 2.0 5 votes vote down vote up
@Override
public void onResponse(final SearchResponse response) {
    final String scrollId = response.getScrollId();
    final SearchHits hits = response.getHits();
    final int size = hits.getHits().length;
    currentCount += size;
    if (logger.isDebugEnabled()) {
        logger.debug("scrollId: {}, totalHits: {}, hits: {}, current: {}",
                scrollId, hits.getTotalHits(), size, currentCount);
    }
    try {
        for (final SearchHit hit : hits) {
            final String index = bulkIndex == null ? hit.getIndex()
                    : bulkIndex;
            final String operation = "{\"index\":{\"_index\":\"" + index
                    + "\",\"_id\":\"" + hit.getId() + "\"}}";
            final String source = XContentHelper.convertToJson(
                    hit.getSourceRef(), true, false, XContentType.JSON);
            writer.append(operation).append('\n');
            writer.append(source).append('\n');
        }

        if (size == 0 || scrollId == null) {
            // end
            writer.flush();
            close();
            listener.onResponse(null);
        } else {
            client.prepareSearchScroll(scrollId)
                    .setScroll(RequestUtil.getScroll(request))
                    .execute(this);
        }
    } catch (final Exception e) {
        onFailure(e);
    }
}
 
Example 2
Source File: Select.java    From code with Apache License 2.0 4 votes vote down vote up
public void after() throws IOException {
    //3.获取查询结果
    SearchResponse searchResponse = restHighLevelClient.search(searchRequest,
            RequestOptions.DEFAULT);

    // 查询花费时间,单位是毫秒
    TimeValue took = searchResponse.getTook();
    // 分片信息
    int total = searchResponse.getTotalShards();
    int success = searchResponse.getSuccessfulShards();
    int skipped = searchResponse.getSkippedShards();
    int failed = searchResponse.getFailedShards();
    // 搜索结果总览对象
    SearchHits searchHits = searchResponse.getHits();
    // 搜索到的总条数
    long totalHits = searchHits.getTotalHits();
    // 所有结果中文档得分的最高分
    float maxScore = searchHits.getMaxScore();

    System.out.println("took:" + took);
    System.out.println("_shards:");
    System.out.println("        total:" + total);
    System.out.println("        success:" + success);
    System.out.println("        skipped:" + skipped);
    System.out.println("        failed:" + failed);
    System.out.println("hits:");
    System.out.println("        total:" + totalHits);
    System.out.println("        max_score:" + maxScore);
    System.out.println("        hits:");
    // 搜索结果的文档对象数组,每个元素是一条搜索到的文档信息
    SearchHit[] hits = searchHits.getHits();
    for (SearchHit hit : hits) {
        // 索引库
        String index = hit.getIndex();
        // 文档类型
        String type = hit.getType();
        // 文档id
        String id = hit.getId();
        // 文档得分
        float score = hit.getScore();
        // 文档的源数据
        String source = hit.getSourceAsString();
        System.out.println("            _index:" + index);
        System.out.println("            _type:" + type);
        System.out.println("            _id:" + id);
        System.out.println("            _score:" + score);
        System.out.println("            _source:" + source);
    }
    restHighLevelClient.close();
}
 
Example 3
Source File: SearchApiMain.java    From elasticsearch-pool with Apache License 2.0 4 votes vote down vote up
public static void searchApi() throws IOException {

        RestHighLevelClient client = HighLevelClient.getInstance();

        try {
            SearchRequest searchRequest = new SearchRequest("jingma2_test");//限定index
            searchRequest.types("testlog");//限定type

            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            /*查询所有记录*/

//        searchSourceBuilder.query(QueryBuilders.matchAllQuery());


            /*根据匹配查询*/
            QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name", "风雷");
            /*设置中文分词器*/
//            ((MatchQueryBuilder) matchQueryBuilder).analyzer("ik");
//            ((MatchQueryBuilder) matchQueryBuilder).analyzer("ik_max_word");
//            ((MatchQueryBuilder) matchQueryBuilder).analyzer("ik_smart");
//            ((MatchQueryBuilder) matchQueryBuilder).analyzer("standard");
            searchSourceBuilder.query(matchQueryBuilder);

            /*限定查询条件和查询条数*/
//            searchSourceBuilder.query(QueryBuilders.termQuery("name", "风雷"));
            searchSourceBuilder.from(0);
            searchSourceBuilder.size(5);
//            searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));

            /*限定查询结果排序*/
//            searchSourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
//            searchSourceBuilder.sort(new FieldSortBuilder("age").order(SortOrder.ASC));

            searchRequest.source(searchSourceBuilder);

            SearchResponse searchResponse = client.search(searchRequest);
            System.out.println(searchResponse);

            SearchHits hits = searchResponse.getHits();
            long totalHits = hits.getTotalHits();
            float maxScore = hits.getMaxScore();

            SearchHit[] searchHits = hits.getHits();
            for (SearchHit hit : searchHits) {
                String index = hit.getIndex();
                String type = hit.getType();
                String id = hit.getId();
                float score = hit.getScore();
                String sourceAsString = hit.getSourceAsString();
                System.out.println(sourceAsString);
//                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            }

        }finally {
            HighLevelClient.close();
        }

    }
 
Example 4
Source File: ElasticsearchDocument.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public ElasticsearchDocument(SearchHit hit, Function<? super String, ? extends SpatialContext> geoContextMapper) {
	this(hit.getId(), hit.getType(), hit.getIndex(), hit.getVersion(), hit.getSourceAsMap(), geoContextMapper);
}
 
Example 5
Source File: KibanaExporter.java    From arctic-sea with Apache License 2.0 3 votes vote down vote up
private static KibanaConfigEntryDto parseSearchHit(SearchHit hit) {
    System.out.printf("Reading %s/%s/%s%n", hit.getIndex(), hit.getType(), hit.getId());

    String id = hit.getId();
    if (hit.getId().equals(statisticsIndex)) {
        id = KibanaImporter.INDEX_NEEDLE;
    }

    String source = hit.getSourceAsString().replace(statisticsIndex, KibanaImporter.INDEX_NEEDLE);

    return new KibanaConfigEntryDto(hit.getIndex(), hit.getType(), id, source);

}