好的想法是十分钱一打,真正无价的是能够实现这些想法的人。

5.4怎么根据名称模糊匹配查询

Elasticsearch | 作者 jnuc093 | 发布于2017年06月01日 | 阅读数:4520

根据名称精准查询得到结果如下
Snip20170601_1.png

 
现在要模糊查询,查不出结果

Snip20170601_5.png

 
_score是我后来开启的。目前值都是 1.模糊搜索不出来是不是这个原因?
 
已邀请:

kennywu76 - Wood

赞同来自: jnuc093

因为你的mapping设置里, name的类型是keyword,因此索引的时候,真个字段的内容是作为一个term索引起来的,范例里的“二次设备”是一个完整的term。  要想做“二次”这样的模糊查询,可以利用prefix query,或者wildcard query。 
 
最优的方案应该是将name设置为text类型,并且增加中文分词器,如ik,这样对于“二次设备”这样的文本可以做分词,可以做全文检索。

jnuc093

赞同来自:

GET tmxpmstreeinfo/unit/_search
{
"query": {
"match_phrase": {
"name": "二次设备"
}
}
}

{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 9,
"max_score": 6.6338444,
"hits": [
{
"_index": "tmxpmstreeinfo",
"_type": "unit",
"_id": "2814",
"_score": 6.6338444,
"_source": {
"id": "4028810b41dfba8f0141e0430f72002a",
"name": "二次设备",
"pid": "0"
}
},
{
"_index": "tmxpmstreeinfo",
"_type": "unit",
"_id": "4497",
"_score": 6.6338444,
"_source": {
"id": "2c905ee4567706f5015678a5e709000b",
"name": "二次设备",
"pid": "2c905ee4567706f5015678a5008e000a"
}
},
{
"_index": "tmxpmstreeinfo",
"_type": "unit",
"_id": "3358",
"_score": 6.609888,
"_source": {
"id": "2c905eb3593dba8a01593df701a2000b",
"name": "二次设备",
"pid": "0"
}
},

想要模糊查询
 
GET tmxpmstreeinfo/unit/_search
{
"query": {
"match_phrase": {
"name": "二次"
}
}
}

{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}

jnuc093

赞同来自:

Discovery
 
id:1023a08144ba8ac10144baa1de1b0003 name:配电分段线路柱上设备节点 pid:1023a08144ba8ac10144ba9f25b10002 _id:0 _type:unit _index:tmxpmstreeinfo _score:1
id:111081sbqshlsbqs01419b13964b0015 name:低压电缆段 pid:111081sbqshlsbqs01419b0a6b61000a _id:14 _type:unit _index:tmxpmstreeinfo _score:1
id:2c902aee4814f6820148167b4ddc0008 name:运行单位 pid:0 _id:19 _type:unit _index:tmxpmstreeinfo _score:1
id:101681bx5xf1155fx15xf129x915xxx6 name:直流充电装置分组 pid:101681bx5xf1155fx15xf127xc3fxxx3 _id:22 _type:unit _index:tmxpmstreeinfo _score:1

kennywu76 - Wood

赞同来自:

这个索引的mapping设置是怎样的

jnuc093

赞同来自:

GET tmxpmstreeinfo/_mapping/unit/

{
"tmxpmstreeinfo": {
"mappings": {
"unit": {
"properties": {
"id": {
"type": "keyword"
},
"name": {
"type": "keyword"
},
"pid": {
"type": "keyword"
}
}
}
}
}
}

jnuc093

赞同来自:

好的多谢。

要回复问题请先登录注册