Ticker

6/recent/ticker-posts

Ad Code

Responsive Advertisement

es查询结果中hits.total总是0

查询结果是有命中的,但是hits.total总是为0,应答如下

{
    "took":1,
    "timed_out":false,
    "_shards":{
        "total":1,
        "successful":1,
        "skipped":0,
        "failed":0
    },
    "hits":{
        "total":0,
        "max_score":null,
        "hits":[
            {
                "_index":"soc_project",
                "_type":"soc_project",
                "_id":"3-11",
                "_score":3.2188544,
                "_source":{
                    "id":11,
                    "name":"解耦is就二十大V",
                    "region":"1111",
                    "type":3
                },
                "sort":[
                    3.2188544,
                    11
                ]
            }
        ]
    }
}

查询方法

public ResultPage<Doc> page(String keyword, Integer pageNum, Integer pageSize) throws IOException {
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder()//
                .trackTotalHits(true)//
                .query(QueryBuilders.matchQuery("docName", keyword))//
                .from((pageNum - 1) * pageSize)//
                .size(pageSize)//
                .sort(SortBuilders.scoreSort())//
                .sort(SortBuilders.fieldSort("docRegion.keyword"));

        SearchResponse response = client.search(new SearchRequest()//
                .indices(INDEX_NAME)//
                .source(sourceBuilder), //
                RequestOptions.DEFAULT);

    }

添加方法

private void addDoc(String name, Byte type, Integer id, String region) {
        try {
            client.index(new IndexRequest()//
                    .id(String.format("%d-%d", type, id))//
                    .index(INDEX_NAME)//
                    .type(INDEX_NAME)
                    .source(JSON.toJSONString(new Doc(name, type, id, region)), XContentType.JSON)//
                    .setRefreshPolicy(RefreshPolicy.WAIT_UNTIL), //
                    RequestOptions.DEFAULT);
        } catch (IOException e) {
            log.error("向es添加doc失败", e);
            log.error("name={}, type={}, id={}", name, type, id);
        }
    }

实体类

@Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Doc {

        private String docName;
        private Byte docType;
        private Integer docId;
        private String docRegion;
        
    }

刚接触es遇到问题,哪位大神能帮忙看看

Enregistrer un commentaire

0 Commentaires