无法在QueryBuilders.termQuery

时间:2018-09-09 14:06:27

标签: spring-boot elasticsearch

我是弹性搜索的新手,我曾尝试与高级Rest Client合作

我能够使用CRUD操作,有了搜索功能,我就陷入了困境。

我的目标是使所有基于图书ID的数据都以E106搜索条件开头

http://localhost:5918/book-elastic/books/book/E106

我在下面添加了部分代码

我能够使用

获取所有数据

QueryBuilders.matchAllQuery()

但是我无法获得特定的字段值

QueryBuilders.termQuery(“ _ id”,bookId)

我还分享了两个结果的屏幕截图

有人可以帮我解决这个问题吗?

如有需要,请恢复。

预先感谢

public Page<BookEntity> findByBookId(String bookId, Pageable pageable) throws IOException{

        SearchRequest searchRequest = new SearchRequest(INDEX); 
        searchRequest.types(TYPE);

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.fetchSource(false);
        //searchSourceBuilder.fetchSource(null, new String[]{"excludedProperty"});

        /*MatchQueryBuilder matchQueryBuilder =  new MatchQueryBuilder("id",bookId);
        matchQueryBuilder.fuzziness(Fuzziness.AUTO); 
        matchQueryBuilder.prefixLength(3); 
        matchQueryBuilder.maxExpansions(7);*/ 

        searchSourceBuilder.from((int)pageable.getOffset());
        searchSourceBuilder.size(pageable.getPageSize());

        //searchSourceBuilder.query(matchQueryBuilder);
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        //searchSourceBuilder.query(QueryBuilders.termQuery("_id",bookId)); 
        searchRequest.source(searchSourceBuilder);

        SearchResponse searchResponse = restHighLevelClient.search(searchRequest,RequestOptions.DEFAULT);
        SearchHits hits = searchResponse.getHits();
        SearchHit[] objectHits = hits.getHits();
        for (SearchHit searchHit : objectHits) {
            System.out.println("***************************");
            System.out.println("Search Hit :: "+searchHit);
            System.out.println("***************************");
        }


        return null;

    }

结果截图

  1. matchAllQuery

输入:{“ from”:0,“ size”:20,“ query”:{“ match_all”:{“ boost”:1.0}},“ _ source”:false}

Input Screenshot

响应

响应值:{“ took”:5,“ timed_out”:false,“ _ shards”:{“ total”:5,“成功”:5,“跳过”:0:“失败”:0},“ hits“:{” total“:3,” max_score“:1.0,” hits“:[{” _ index“:” bookdata“,” _ type“:” books“,” _ id“:” E106401“,” _ score“: 1.0},{“ _ index”:“ bookdata”,“ _ type”:“ books”,“ _ id”:“ E106403”,“ _ score”:1.0},{“ _ index”:“ bookdata”,“ _ type”:“ books “,” _ id“:” E10640“,” _ score“:1.0}]}}

matchAllQuery

  1. 输入值:QueryBuilders.termQuery(“ _ id”,bookId)

{“ from”:0,“ size”:20,“ query”:{“ term”:{“ _ id”:{“ value”:“ E106”,“ boost”:1.0}}},“ __ source “:false}

Input Vaues

响应

响应为空

enter image description here

1 个答案:

答案 0 :(得分:0)

QueryBuilders中的

matchPhrasePrefixQuery解决了我的问题