[ElasticSearch]Term精确匹配中文字符串短语

1. 给定数据

curl -XPUT 'localhost:9200/test-index/stu/1' -d'
{
    "name":"陈泽鹏",
    "sex":"boy",
    "college":"计算机学院"
}';
curl -XPUT 'localhost:9200/test-index/stu/2' -d'
{
    "name":"廖力生",
    "sex":"boy",
    "college":"通信学院"
}';
curl -XPUT 'localhost:9200/test-index/stu/3' -d'
{
    "name":"李源一",
    "sex":"girl",
    "college":"计算机学院"
}';
curl -XPUT 'localhost:9200/test-index/stu/4' -d'
{
    "name":"陈哲超",
    "sex":"girl",
    "college":"计算机学院"
}';
curl -XPUT 'localhost:9200/test-index/stu/5' -d'
{
    "name":"AA",
    "sex":"girl",
    "college":"计算机学院"
}';
curl -XPUT 'localhost:9200/test-index/stu/6' -d'
{
    "name":"bb",
    "sex":"girll",
    "college":"通信学院"
}';
curl -XPUT 'localhost:9200/test-index/stu/7' -d'
{
    "name":"方镜淇",
    "sex":"boy",
    "college":"电子工程学院"
}';
curl -XPUT 'localhost:9200/test-index/stu/8' -d'
{
    "name":"吴兴涵",
    "sex":"boy",
    "college":"计算机学院"
}';

2. 需求

我们想精确匹配出来自计算机学院的学生,所以我们就实现如下语句:

QueryBuilder queryBuilder = QueryBuilders.termQuery("college", "计算机学院");
// Search
SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index);
searchRequestBuilder.setTypes(type);
searchRequestBuilder.setQuery(queryBuilder);
// 执行
SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();

返回结果:

20:30:46.815 [main] INFO  com.sjf.open.api.TermLevelQuery - ----------termMatch size 0

表示没有找到来自计算机学院的同学,这咋可能。。。。

3. 解决方案

3.1 解决方案一 

把计算机学院拆分成多个汉字,再利用bool查询查找:

// 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();

返回结果:

20:34:53.022 [main] INFO  com.sjf.open.api.Search - ----------hit source: id 5 source {college=计算机学院, sex=girll, name=AA}
20:34:53.028 [main] INFO  com.sjf.open.api.Search - ----------hit source: id 8 source {college=计算机学院, sex=boy, name=吴兴涵}
20:34:53.029 [main] INFO  com.sjf.open.api.Search - ----------hit source: id 1 source {college=计算机学院, sex=boy, name=陈泽鹏}
20:34:53.029 [main] INFO  com.sjf.open.api.Search - ----------hit source: id 7 source {college=计算机学院, sex=boy, name=陈哲超}
20:34:53.029 [main] INFO  com.sjf.open.api.Search - ----------hit source: id 3 source {college=计算机学院, sex=girll, name=李源一}

3.2 解决方案二

设置字段属性,设置为not_analyzed,再插入上面数据:

curl -XPUT 'localhost:9200/test-index' -d'
{
    "mappings":{
        "stu":{
            "properties":{
                "name":{
                    "type":"string"
                },
                "sex":{
                    "type":"string"
                },
                "college":{
                    "type":"string",
                    "index":"not_analyzed"
                }
            }
        }
    }
}';

具体分析:https://www.elastic.co/guide/en/elasticsearch/reference/2.3/query-dsl-term-query.html

再次查询:

QueryBuilder queryBuilder = QueryBuilders.termQuery("college", "计算机学院");
// Search
SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index);
searchRequestBuilder.setTypes(type);
searchRequestBuilder.setQuery(queryBuilder);
// 执行
SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();

输出结果:

20:34:53.022 [main] INFO  com.sjf.open.api.Search - ----------hit source: id 5 source {college=计算机学院, sex=girll, name=AA}
20:34:53.028 [main] INFO  com.sjf.open.api.Search - ----------hit source: id 8 source {college=计算机学院, sex=boy, name=吴兴涵}
20:34:53.029 [main] INFO  com.sjf.open.api.Search - ----------hit source: id 1 source {college=计算机学院, sex=boy, name=陈泽鹏}
20:34:53.029 [main] INFO  com.sjf.open.api.Search - ----------hit source: id 7 source {college=计算机学院, sex=boy, name=陈哲超}
20:34:53.029 [main] INFO  com.sjf.open.api.Search - ----------hit source: id 3 source {college=计算机学院, sex=girll, name=李源一}


发布了624 篇原创文章 · 获赞 889 · 访问量 211万+
展开阅读全文

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

07-05

``` 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} ``` 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览