gpt4 book ai didi

mysql - 查询中的索引匹配

转载 作者:行者123 更新时间:2023-12-03 02:23:58 25 4
gpt4 key购买 nike

我有一个字符串,我想用表行值搜索此字符串中最长的匹配项。在SQL中,此查询工作正常:

SELECT model, LENGTH(model) AS size 
FROM models
WHERE 'Acer Aspire 3 A315-42' LIKE CONCAT('%', model, '%')
ORDER BY size DESC
LIMIT 1;

但这很慢。 Elasticsearch在索引中找到查询匹配项,但是我需要在查询中找到索引匹配项。 Elasticsearch 有什么解决方案吗?

最佳答案

脚本字段可用于对文本长度进行排序。更好的选择是也索引文本的长度并在该字段上排序。

数据:

"hits" : [
{
"_index" : "index16",
"_type" : "_doc",
"_id" : "QNLN5HEB9UrRQjNWOLGw",
"_score" : 1.0,
"_source" : {
"model" : "Acer Aspire 3 A315-42 aa"
}
},
{
"_index" : "index16",
"_type" : "_doc",
"_id" : "QdLN5HEB9UrRQjNWQLFu",
"_score" : 1.0,
"_source" : {
"model" : "Acer Aspire 3 A315-42"
}
}
]

查询:
{
"query": {
"match": {
"model": "Acer Aspire 3 A315-42"
}
},
"sort": [
{
"_script":{
"script": "doc['model.keyword'].value.length()",-->script to return length
"type": "number",
"order": "desc"
}
}
],
"size": 1 --> top 1
}


结果:
"hits" : [
{
"_index" : "index16",
"_type" : "_doc",
"_id" : "QNLN5HEB9UrRQjNWOLGw",
"_score" : null,
"_source" : {
"model" : "Acer Aspire 3 A315-42 aa"
},
"sort" : [
25.0
]
}
]

关于mysql - 查询中的索引匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61612734/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com