es-多字段定制搜索

elastic index 数据结构

mapping = {
    "mappings": {
        "black_goods_test_v2":  {
    "properties": {
        "active": {
            "type": "integer"
        },
        "author": {
            "type": "text",
            "analyzer": "ik_max_word",
            "norms": false
        },
        "classify": {
            "type": "text",
            "analyzer": "ik_max_word",
            "norms": false

        },
        "content": {
            "type": "text",
            "analyzer": "ik_max_word",
            "norms": false
        },
        "data_insert_time": {
            "type": "integer"
        },
        "data_source": {
            "type": "integer"
        },
        "data_uid": {
            "type": "keyword"
        },
        "data_update_time": {
            "type": "integer"
        },
        "diy_classify": {
            "type": "keyword"
        },
        "goods": {
            "type": "text",
            "fields": {
                "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                }
            },
            "norms": false
        },
        "goods_id": {
            "type": "integer"
        },
        "link": {
            "type": "keyword"
        },
        "price": {
            "type": "keyword"
        },
        "publish_time": {
            "type": "integer"
        },
        "shop_id": {
            "type": "integer"
        },
        "shop_link": {
            "type": "keyword"
        },
        "shop_name": {
            "type": "text",
            "analyzer": "ik_max_word",
            "norms": false
        },
        "title": {
            "type": "text",
            "analyzer": "ik_max_word",
            "norms": false
        }
    }
}
}
}

需求一

简介:多字段搜索,字段权重不同,排序:相关性商品 大于 发布时间

具体需求如下(优先级从上到下)

  • 关键词维度:
    • 1,完整关键词,
    • 2,分词数目最多
  • 字段维度:
    • 1,标题
    • 2,分类
    • 3,商品内容
  • 时间维度:
    • 1,优先展示最新时间的商品内容
  • 命中关键词高亮

搜索语句中的特性

  • filter: 不计分对数据进行过滤(可嵌套bool)
  • multi_match: 对多字段进行搜索
  • fields^0.99: 对某个字段的权重进行提高
  • tie_breake: 提高除开最佳字段其他字段是否积分
  • highlight: 对于命中的关键词进行高亮

搜索语句

{
    "query": {
        "bool": {
            "filter": {
                "term": {
                    "data_source": 0
                }
            },
            "must": [
                {
                    "multi_match": {
                        "query": "小红书",
                        "fields": [
                            "title^0.99",
                            "classify^0.98",
                            "content^0.97"
                        ],
                        "tie_breaker": 1 # 除开最佳字段以外其他字段是否计分
                    }
                }
            ]
        }
    },
    "highlight": {
        "fields": {
            "title": {},
            "classify": {},
            "content": {}
        }
    },
    "size": 300,
    "sort": [
        {
            "_score": {
                "order": "desc"
            },
            "publish_time": {
                "order": "desc"
            }
        }
    ]
}

需求二

简介:多字段搜索,字段权重一致,排序:发布时间 大于相关性

具体需求如下(优先级从上到下)

  • 时间维度:优先展示最新时间的商品内容
  • 关键词维度:
    • 1.完整关键词
    • 2.分词数目最多
  • 字段维度:
    • 1.标题
    • 2.分类
    • 3.内容的商品内容

搜索语句中的关键词特性:

  • filter: 不计分对数据进行过滤(可嵌套bool)
  • constant_score: 固定这个里面的搜索不受es自身评分影响
  • highlight: 对于命中的关键词进行高亮
  • minimum_should_match: 同级别的shold列表中最少要命中一个条件

搜索语句

  • 比如搜索“京东 优惠”
    • 第一:优先展示title,content,classify这三个字段中同时命中京东和优惠的
    • 第二:优先展示title,content,classify这三个字段命中京东或者优惠的
    • 第三:es自己对“京东 优惠”分词,然后按照es的关联性算分进行排序
{
    "query": {
        "bool": {
            "filter": {
                "bool": {
                    "must": [
                        {
                            "term": {
                                "active": 1
                            }
                        }
                    ],
                    "should": [
                        {
                            "term": {
                                "diy_classify": "账号"
                            }
                        }
                    ],
                    "minimum_should_match": 1
                }
            },
            "must": [
                {
                    "function_score": {
                        "query": {
                            "bool": {
                                "should": [
                                    {
                                        "constant_score": {
                                            "filter": {
                                                "bool": {
                                                    "should": [
                                                        {
                                                            "bool": {
                                                                "must": [
                                                                    {
                                                                        "match_phrase": {
                                                                            "title": {
                                                                                "query": "京东"
                                                                            }
                                                                        }
                                                                    },
                                                                    {
                                                                        "match_phrase": {
                                                                            "title": {
                                                                                "query": "优惠"
                                                                            }
                                                                        }
                                                                    }
                                                                ]
                                                            }
                                                        },
                                                        {
                                                            "bool": {
                                                                "must": [
                                                                    {
                                                                        "match_phrase": {
                                                                            "content": {
                                                                                "query": "京东"
                                                                            }
                                                                        }
                                                                    },
                                                                    {
                                                                        "match_phrase": {
                                                                            "content": {
                                                                                "query": "优惠"
                                                                            }
                                                                        }
                                                                    }
                                                                ]
                                                            }
                                                        },
                                                        {
                                                            "bool": {
                                                                "must": [
                                                                    {
                                                                        "match_phrase": {
                                                                            "classify": {
                                                                                "query": "京东"
                                                                            }
                                                                        }
                                                                    },
                                                                    {
                                                                        "match_phrase": {
                                                                            "classify": {
                                                                                "query": "优惠"
                                                                            }
                                                                        }
                                                                    }
                                                                ]
                                                            }
                                                        }
                                                    ]
                                                }
                                            },
                                            "boost": 1000
                                        }
                                    },
                                    {
                                        "multi_match": {
                                            "query": "京东 优惠",
                                            "fields": [
                                                "title",
                                                "classify",
                                                "content"
                                            ],
                                            "tie_breaker": 1
                                        }
                                    },
                                    {
                                        "constant_score": {
                                            "filter": {
                                                "bool": {
                                                    "should": [
                                                        {
                                                            "bool": {
                                                                "must": [
                                                                    {
                                                                        "match_phrase": {
                                                                            "title": {
                                                                                "query": "京东"
                                                                            }
                                                                        }
                                                                    }
                                                                ]
                                                            }
                                                        },
                                                        {
                                                            "bool": {
                                                                "must": [
                                                                    {
                                                                        "match_phrase": {
                                                                            "content": {
                                                                                "query": "京东"
                                                                            }
                                                                        }
                                                                    }
                                                                ]
                                                            }
                                                        },
                                                        {
                                                            "bool": {
                                                                "must": [
                                                                    {
                                                                        "match_phrase": {
                                                                            "classify": {
                                                                                "query": "京东"
                                                                            }
                                                                        }
                                                                    }
                                                                ]
                                                            }
                                                        }
                                                    ]
                                                }
                                            },
                                            "boost": 100
                                        }
                                    },
                                    {
                                        "constant_score": {
                                            "filter": {
                                                "bool": {
                                                    "should": [
                                                        {
                                                            "bool": {
                                                                "must": [
                                                                    {
                                                                        "match_phrase": {
                                                                            "title": {
                                                                                "query": "优惠"
                                                                            }
                                                                        }
                                                                    }
                                                                ]
                                                            }
                                                        },
                                                        {
                                                            "bool": {
                                                                "must": [
                                                                    {
                                                                        "match_phrase": {
                                                                            "content": {
                                                                                "query": "优惠"
                                                                            }
                                                                        }
                                                                    }
                                                                ]
                                                            }
                                                        },
                                                        {
                                                            "bool": {
                                                                "must": [
                                                                    {
                                                                        "match_phrase": {
                                                                            "classify": {
                                                                                "query": "优惠"
                                                                            }
                                                                        }
                                                                    }
                                                                ]
                                                            }
                                                        }
                                                    ]
                                                }
                                            },
                                            "boost": 100
                                        }
                                    }
                                ],
                                "minimum_should_match": 1
                            }
                        },
                        "functions": [
                            {
                                "script_score": {
                                    "script": "if (_score>1000) {return 1000} if (_score>100) {return 100} if (_score<100) {return _score}"
                                }
                            }
                        ],
                        "score_mode": "sum"
                    }
                }
            ]
        }
    },
    "highlight": {
        "fields": {
            "title": {},
            "classify": {},
            "content": {}
        }
    },
    "sort": [
        {
            "_score": {
                "order": "desc"
            },
            "publish_time": {
                "order": "desc"
            }
        }
    ]
}

参考链接

  • https://www.elastic.co/guide/en/elasticsearch/reference/6.3/query-dsl-terms-set-query.html
  • https://www.cnblogs.com/reycg-blog/p/10055039.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值