1. 程式人生 > >[ElasticSearch]Term精確匹配中文字串短語

[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"
                }
            }
        }
    }
}';

再次查詢:

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=李源一}