Elasticsearch 一個字段精確和模糊搜索,一個字段多種分詞器的設置

1、前言

本文的環境情況,Elasticsearch 集羣 5 個節點,第 1 個節點有 5 個實例(1 個 master 角色,4 個 data 角色),後 4 個節點每個節點都有 4 個實例(都是 data 角色)。Elasticsearch 的版本爲 5.4.3,裝有 IK 分詞器,IK 的版本相應的也要選擇 5.4.3。

2、對於同一個字段要實現如下的條件的搜索,先介紹一下該字段的情況。

樣例:”ap_name_exact” :”ChinaNetRhaQ;hexicanyin;Tenda5582F8;WiFiEndoscope;WAVLINKBC41;cloud;WXSY68650888;TPLINKB42A;TPLINK9418B8;TPLINK5G557E;RMGSST2;signaltest;y123456d;林詩雨;妞妞;成都公積金中心”

2.1、以“;”分割詞的串。

2.2、有中文、有英文、還可以有中英文混合。

3、搜索添加如下:

3.1、模糊搜索,中文是可以分詞搜索的

3.2、詞精確搜索,詞是指以“;”分割出來的詞。

4、映射的如下:

curl -XPUT ip:9211/index_ap_info_m_test3 -d ‘{
“settings”: {
“number_of_shards” : 100,
“number_of_replicas” : 0,
“analysis” : {
“analyzer” : {
“semicolon_analyzer”: {
“type”: “pattern”,
“pattern”: “;”
},
“ik” : {
“tokenizer” : “ik_smart”
}
}
}
},
“mappings” : {
“ods_ap_info_m” : {
“dynamic” : true,
“properties” : {
“ap_name” : {“type” : “string”,”index”: “analyzed”,”analyzer” : “ik_smart”},
“ap_name_exact” : {“type” : “string”,”analyzer”:”semicolon_analyzer”},
“ap_mac”:{“type” : “string”,”index”: “not_analyzed”},
“ap_ip_list” : {“type” : “string”,”index”: “no”},
“lat_list” : {“type” : “string”,”index”: “no”},
“lng_list” : {“type” : “string”,”index”: “no”},
“geo” : {“type” : “geo_point”}
}
}
}
}’
從上面的 json 中可以看到指定了 2 種分詞器,而且對同一個字段出現兩次,ap_name 和 ap_name_exact,分別指定了不同的分詞器。

5、測試數據

curl -XPUT ‘http://ip:9211/index_ap_info_m_test/ods_ap_info_m/1’ -d’{
“ap_name” : “ChinaNetRhaQ”,
“ap_name_exact” :”ChinaNetRhaQ;hexicanyin;Tenda5582F8;WiFiEndoscope;WAVLINKBC41;cloud;WXSY68650888;TPLINKB42A;TPLINK9418B8;TPLINK5G557E;RMGSST2;signaltest;y123456d;林詩雨;妞妞;成都公積金中心”,
“ap_mac” : “48:28:2f:87:3f:6a”,
“ap_ip_list” : “36.149.215.94;”,
“lat_list” : “32.853764;32.848480”,
“lng_list” : “120.343597;120.326950”,
“geo” : “32.853764,120.343597”
}’

curl -XPUT ‘http://ip:9211/index_ap_info_m_test/ods_ap_info_m/2’ -d’{
“ap_name” : “ChinaNetRhaQ”,
“ap_name_exact” :”ChinaNet;hexicanyin;Tenda5582F8;WiFiEndoscope;WAVLINKBC41;cloud;WXSY68650888;TPLINKB42A;TPLINK9418B8;TPLINK5G557E;RMGSST2;signaltest;y123456d;林詩雨;妞妞;成都公積金中心”,
“ap_mac” : “48:28:2f:87:3f:6a”,
“ap_ip_list” : “36.149.215.94;”,
“lat_list” : “32.853764;32.848480”,
“lng_list” : “120.343597;120.326950”,
“geo” : “32.853764,120.343597”
}’

curl -XPUT ‘http://ip:9211/index_ap_info_m_test/ods_ap_info_m/3’ -d’{
“ap_name” : “ChinaNet;hexicanyin;Tenda5582F8;WiFiEndoscope;WAVLINKBC41;cloud;WXSY68650888;TPLINKB42A;TPLINK9418B8;TPLINK5G557E;RMGSST2;signaltest;y123456d;林詩雨;妞妞;成都公積金中心”,
“ap_name_exact” :”ChinaNet;hexicanyin;Tenda5582F8;WiFiEndoscope;WAVLINKBC41;cloud;WXSY68650888;TPLINKB42A;TPLINK9418B8;TPLINK5G557E;RMGSST2;signaltest;y123456d;林詩雨;妞妞;成都公積金中心”,
“ap_mac” : “48:28:2f:87:3f:6a”,
“ap_ip_list” : “36.149.215.94;”,
“lat_list” : “32.853764;32.848480”,
“lng_list” : “120.343597;120.326950”,
“geo” : “32.853764,120.343597”
}’

curl -XPUT http://ip:9211/index_ap_info_m_test/ods_ap_info_m/4’ -d’{
“ap_name” : “ChinaNet;hexicanyin;Tenda5582F8;WiFiEndoscope;WAVLINKBC41;cloud;WXSY68650888;TPLINKB42A;TPLINK9418B8;TPLINK5G557E;RMGSST2;signaltest;y123456d;林詩雨;妞妞;成都公積金”,
“ap_name_exact” :”ChinaNet;hexicanyin;Tenda5582F8;WiFiEndoscope;WAVLINKBC41;cloud;WXSY68650888;TPLINKB42A;TPLINK9418B8;TPLINK5G557E;RMGSST2;signaltest;y123456d;林詩雨;妞妞;成都公積金”,
“ap_mac” : “48:28:2f:87:3f:6a”,
“ap_ip_list” : “36.149.215.94;”,
“lat_list” : “32.853764;32.848480”,
“lng_list” : “120.343597;120.326950”,
“geo” : “32.853764,120.343597”
}’

curl -XPUT ‘http://ip:9211/index_ap_info_m_test/ods_ap_info_m/5’ -d’{
“ap_name” : “ChinaNet;hexicanyin;Tenda5582F8;WiFiEndoscope;WAVLINKBC41;cloud;WXSY68650888;TPLINKB42A;TPLINK9418B8;TPLINK5G557E;RMGSST2;signaltest;y123456d;林詩雨;妞妞;公積金中心”,
“ap_name_exact” :”ChinaNet;hexicanyin;Tenda5582F8;WiFiEndoscope;WAVLINKBC41;cloud;WXSY68650888;TPLINKB42A;TPLINK9418B8;TPLINK5G557E;RMGSST2;signaltest;y123456d;林詩雨;妞妞;成都公積金”,
“ap_mac” : “48:28:2f:87:3f:6a”,
“ap_ip_list” : “36.149.215.94;”,
“lat_list” : “32.853764;32.848480”,
“lng_list” : “120.343597;120.326950”,
“geo” : “32.853764,120.343597”
}’

6、查詢語句

curl ‘ip:9211/index_ap_info_m_test/ods_ap_info_m/_search?pretty’ -d ’
{
“query”:{
“query_string”:{
“default_field”:”ap_name_exact”,
“query”:”成都公積金中心”
}
}
}’

curl ‘ip:9211/index_ap_info_m_test/ods_ap_info_m/_search?pretty’ -d ’
{
“query”:{
“query_string”:{
“default_field”:”ap_name_exact”,
“query”:”成都公積金”
}
}
}’

curl ‘ip:9211/index_ap_info_m_test/ods_ap_info_m/_search?pretty’ -d ’
{
“query”:{
“query_string”:{
“default_field”:”ap_name_exact”,
“query”:”ChinaNet”
}
}
}’

curl ‘ip:9211/index_ap_info_m_test/ods_ap_info_m/_search?pretty’ -d ’
{
“query”:{
“query_string”:{
“default_field”:”ap_name_exact”,
“query”:”ChinaNet; ChinaNetRhaQ”
}
}
}’

curl -XGET ‘ip:9211/index_ap_info_m_test/ods_ap_info_m/_search?pretty’ -d ’
{
“query”:{
“match”:{
“ap_name”:”成都中心”
}
}
}’

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章