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

The following examples show how to use org.elasticsearch.search.SearchHit#getScore() . 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: InfiniteScrollIterable.java    From vertexium with Apache License 2.0 6 votes vote down vote up
@Override
public Double getScore(Object id) {
    if (response == null) {
        return null;
    }
    for (SearchHit hit : response.getHits()) {
        Object hitId = ElasticsearchGraphQueryIdIterable.idFromSearchHit(hit, getIdStrategy());
        if (hitId == null) {
            continue;
        }
        if (id.equals(hitId)) {
            return (double) hit.getScore();
        }
    }
    return null;
}
 
Example 2
Source File: InfiniteScrollIterable.java    From vertexium with Apache License 2.0 6 votes vote down vote up
@Override
public Double getScore(Object id) {
    if (response == null) {
        return null;
    }
    for (SearchHit hit : response.getHits()) {
        Object hitId = ElasticsearchGraphQueryIdIterable.idFromSearchHit(hit, getIdStrategy());
        if (hitId == null) {
            continue;
        }
        if (id.equals(hitId)) {
            return (double) hit.getScore();
        }
    }
    return null;
}
 
Example 3
Source File: FeatureLoader.java    From pyramid with Apache License 2.0 6 votes vote down vote up
private static void loadNgramFeatureTFIFL(ESIndex index, DataSet dataSet, Ngram feature,
                                          IdTranslator idTranslator, String docFilter,
                                          Map<String, float[]> fieldLength){
    int featureIndex = feature.getIndex();
    SearchResponse response = index.spanNearFrequency(feature, docFilter, idTranslator.numData());
    SearchHit[] hits = response.getHits().getHits();
    String field = feature.getField();
    for (SearchHit hit: hits){
        String indexId = hit.getId();
        float score = hit.getScore();
        int algorithmId = idTranslator.toIntId(indexId);
        float docLength = fieldLength.get(field)[algorithmId];
        double s = score/docLength;

        dataSet.setFeatureValue(algorithmId,featureIndex,s);
    }
}
 
Example 4
Source File: FeatureLoader.java    From pyramid with Apache License 2.0 6 votes vote down vote up
private static void loadCodeDesFeatureTfidf(ESIndex index, DataSet dataSet, Feature feature,
                                            IdTranslator idTranslator, String docFilter){
    int featureIndex = feature.getIndex();
    CodeDescription codeDescription = (CodeDescription)(feature);

    SearchResponse response = index.match(codeDescription.getField(),codeDescription.getDescriptionString(),
                docFilter,
                codeDescription.getSize());
    SearchHit[] hits = response.getHits().getHits();
    for (SearchHit hit: hits){
        String indexId = hit.getId();
        float score = hit.getScore();
        int algorithmId = idTranslator.toIntId(indexId);
        dataSet.setFeatureValue(algorithmId,featureIndex,score);
    }

}
 
Example 5
Source File: FeatureLoader.java    From pyramid with Apache License 2.0 5 votes vote down vote up
private static void loadNgramFeatureOriginal(ESIndex index, DataSet dataSet, Ngram feature,
                                             IdTranslator idTranslator, String docFilter){
    int featureIndex = feature.getIndex();
    SearchResponse response = index.spanNear(feature, docFilter, idTranslator.numData());
    SearchHit[] hits = response.getHits().getHits();
    for (SearchHit hit: hits){
        String indexId = hit.getId();
        float score = hit.getScore();
        int algorithmId = idTranslator.toIntId(indexId);
        dataSet.setFeatureValue(algorithmId,featureIndex,score);
    }
}
 
Example 6
Source File: FeatureLoader.java    From pyramid with Apache License 2.0 5 votes vote down vote up
private static void loadNgramFeatureFrequency(ESIndex index, DataSet dataSet, Ngram feature,
                                              IdTranslator idTranslator, String docFilter){
    int featureIndex = feature.getIndex();
    SearchResponse response = index.spanNearFrequency(feature, docFilter, idTranslator.numData());
    SearchHit[] hits = response.getHits().getHits();
    for (SearchHit hit: hits){
        String indexId = hit.getId();
        float score = hit.getScore();
        int algorithmId = idTranslator.toIntId(indexId);
        dataSet.setFeatureValue(algorithmId,featureIndex,score);
    }
}
 
Example 7
Source File: FeatureLoader.java    From pyramid with Apache License 2.0 5 votes vote down vote up
public static void loadNgramFeatureBinary(ESIndex index, DataSet dataSet, Ngram feature,
                                          IdTranslator idTranslator, String docFilter){
    int featureIndex = feature.getIndex();
    SearchResponse response = index.spanNear(feature, docFilter, idTranslator.numData());
    SearchHit[] hits = response.getHits().getHits();
    for (SearchHit hit: hits){
        String indexId = hit.getId();
        float score = hit.getScore();
        int algorithmId = idTranslator.toIntId(indexId);
        if (score>0){
            score=1;
        }
        dataSet.setFeatureValue(algorithmId,featureIndex,score);
    }
}
 
Example 8
Source File: FeatureLoader.java    From pyramid with Apache License 2.0 5 votes vote down vote up
private static void loadCodeDesFeature(ESIndex index, DataSet dataSet, Feature feature,
                                       IdTranslator idTranslator, String docFilter){

    int featureIndex = feature.getIndex();
    CodeDescription codeDescription = (CodeDescription)(feature);
    SearchResponse response = index.minimumShouldMatch(codeDescription.getDescription(), codeDescription.getField(), codeDescription.getPercentage(), idTranslator.numData(), docFilter);
    SearchHit[] hits = response.getHits().getHits();
    for (SearchHit hit: hits){
        String indexId = hit.getId();
        float score = hit.getScore();
        int algorithmId = idTranslator.toIntId(indexId);
        dataSet.setFeatureValue(algorithmId,featureIndex,score);
    }

}
 
Example 9
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 10
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();
        }

    }