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”:”成都中心”
}
}
}’

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值