嘿~ 今天天气不错嘛

es实现标签搜索的功能

Elasticsearch | 作者 zhonghuaxb | 发布于2018年10月30日 | 阅读数:12584

有个需求各位帮忙看看es是怎么样实现的?
物品有标签属性,一个物品有多个标签,假设某个物品有5个标签:nice 颜值高 good 物美价廉 宅男专属 
用户可以选择一个或多个标签来匹配商品,需求如下:
nice 可以匹配到
颜值高 可以匹配到
nice 颜值高 物美价廉 可以匹配到
物美价廉 神器 匹配不到(多个标签只要有一个匹配不到就不行)
价廉 匹配不到(必须是‘物美价廉’才能匹配到)
神器 匹配不到
-------------------------------------------------------
尝试在创建文档时
{
"tags":["nice","颜值高","good","物美价廉","宅男专属 "]
}
搜索是使用:
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": [{
"terms": {
"tags": ["nice"]
}
}, {
"terms": {
"tags": ["good"]
}
}]
}
}
}
可以正常匹配
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": [{
"terms": {
"tags": ["nice"]
}
}, {
"terms": {
"tags": ["颜值高"]
}
}]
}
}
}


但是带中文的,这样匹配不上了,包括数字+英文的也存在这样的问题。


已邀请:

novia - 1&0

赞同来自:

这种场景是不是直接将业务字段用whitespace分析器就可以解决
 
{
"mappings": {
"data": {
"_all": {
"enabled": false
},
"properties": {
"feature_words": {
"type": "string",
"analyzer": "whitespace"
}
}
}
}
}

put数据 标签 百度 阿里 google
{
"feature_words": "百度 阿里 google"
}

查询一 百度+阿里,可以匹配到
{
"query": {
"bool": {
"must": [
{
"term": {
"feature_words": "百度"
}
},
{
"term": {
"feature_words": "阿里"
}
}
]
}
}
}
查询二 百度+腾讯 匹配不到
{
"query": {
"bool": {
"must": [
{
"term": {
"feature_words": "百度"
}
},
{
"term": {
"feature_words": "腾讯"
}
}
]
}
}
}

zqc0512 - andy zhou

赞同来自:

分词器。

要回复问题请先登录注册