SunnyYoona
SunnyYoona
采纳率12.5%
2016-07-05 03:36 阅读 12.9k

[ElasticSearch] 中文字符串精确搜索 term 搜不到结果

    public static void termQuery(Client client, String index, String type) {

        // Query
        QueryBuilder queryBuilder = QueryBuilders.termQuery("college", "计算机学院");

        // Search
        SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index);
        searchRequestBuilder.setTypes(type);
        searchRequestBuilder.setQuery(queryBuilder);

        // 执行
        SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();

        // 结果
        queryResult(searchResponse);
    }

搜索college为计算机学院 原始数据中有,却搜不到结果

原始数据:

11:34:22.674 [main] INFO  com.sjf.open.api.Search - ----------hit source: id 9 source {college=计算机学院, school=西安电子科技大学, sex=boy, name=廖力生, age=21}
11:34:22.677 [main] INFO  com.sjf.open.api.Search - ----------hit source: id 2 source {college=通信学院, school=西安电子科技大学, sex=boy, name=李源一, age=19}
11:34:22.677 [main] INFO  com.sjf.open.api.Search - ----------hit source: id 4 source {college=电子工程学院, school=中国科技大学, sex=girl, name=王俊辉, age=21}
11:34:22.678 [main] INFO  com.sjf.open.api.Search - ----------hit source: id 1 source {college=计算机学院, school=西安电子科技大学, sex=boy, name=徐欣, age=21}
11:34:22.678 [main] INFO  com.sjf.open.api.Search - ----------hit source: id 3 source {college=电子工程学院, school=西安电子科技大学, sex=boy, name=陈哲超, age=18}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • w651913943 w651913943 2018-12-27 08:50

    这个坑我用6.5 的时候踩过
    这样解决的

    QueryBuilder queryBuilder = QueryBuilders.termQuery("college.keyword", "计算机学院");

    别的版本没有试过不太清楚

    点赞 13 评论 复制链接分享
  • phpduang phpduang 2018-04-02 02:32

    这是因为:elasticsearch 里默认的IK分词器是会将每一个中文都进行了分词的切割,所以你直接想查一整个词,或者一整句话是无返回结果的。

    点赞 2 评论 复制链接分享
  • SunnyYoona SunnyYoona 2016-07-05 04:00

    现在解决方式如下:

         public static void termPhraseQuery(Client client, String index, String type) {
    
            // Query
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
            boolQueryBuilder.must(QueryBuilders.termQuery("college","计"));
            boolQueryBuilder.must(QueryBuilders.termQuery("college","算"));
            boolQueryBuilder.must(QueryBuilders.termQuery("college","机"));
            boolQueryBuilder.must(QueryBuilders.termQuery("college","学"));
            boolQueryBuilder.must(QueryBuilders.termQuery("college","院"));
            // Search
            SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index);
            searchRequestBuilder.setTypes(type);
            searchRequestBuilder.setQuery(boolQueryBuilder);
    
            // 执行
            SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();
    
            // 结果
            queryResult(searchResponse);
        }
    

    不知道有什么更好的办法?

    点赞 评论 复制链接分享
  • Richard_vi Richard_vi 2018-02-05 06:43

    试试match phrase

    点赞 评论 复制链接分享

相关推荐