gpt4 book ai didi

javascript - Elasticsearch-模糊搜索未给出建议

转载 作者:行者123 更新时间:2023-12-02 23:10:57 24 4
gpt4 key购买 nike

我正在尝试通过NodeJS在Elasticsearch中实现模糊/自动完成搜索。我已经通过索引"artist"为数据建立了索引。这是ES中存储的数据的示例。

{
"hits": [{
"_index": "artist",
"_type": "_doc",
"_id": "EyejqnAB2pHGVJHwV53Q",
"_score": 1,
"_source": {
"kind": "song",
"artistId": 111051,
"artistName": "Eminem",
"trackName": "Crack a Bottle (feat. Dr. Dre & 50 Cent)",
"collectionName": "Relapse (Deluxe Version)",
"collectionCensoredName": "Relapse (Deluxe Version)",
"artistViewUrl": "https://music.apple.com/us/artist/eminem/111051?uo=4",
"collectionViewUrl": "https://music.apple.com/us/album/crack-a-bottle-feat-dr-dre-50-cent-feat-dr-dre-50-cent/1440558626?i=1440558826&uo=4",
"trackViewUrl": "https://music.apple.com/us/album/crack-a-bottle-feat-dr-dre-50-cent-feat-dr-dre-50-cent/1440558626?i=1440558826&uo=4",
"previewUrl": "https://audio-ssl.itunes.apple.com/itunes-assets/AudioPreview128/v4/da/a5/c1/daa5c140-2c3d-1f74-40c3-b6e596e52b82/mzaf_7480202713407880256.plus.aac.p.m4a",
"artworkUrl100": "https://is1-ssl.mzstatic.com/image/thumb/Music128/v4/c5/f8/fd/c5f8fdf6-d4c9-85c9-d169-c5d349a44f1c/source/100x100bb.jpg",
"collectionPrice": 12.99,
"releaseDate": "2009-02-02T12:00:00Z",
"collectionExplicitness": "explicit",
"trackExplicitness": "explicit",
"discCount": 1,
"discNumber": 1,
"trackCount": 24,
"trackNumber": 18,
"country": "USA",
"currency": "USD"
}
}]
}

上面的 artistName具有作为 Eminem的值,问题是当我键入 'e'时,它没有显示任何内容,与 'em'emiemin相同。当我键入 emine时,它将开始发出结果。我要去哪里错了?

最佳答案

实现自动完成功能的方法有多种,而模糊搜索不是正确的方法(它主要用于搜索相关文档而不是标记(去重复),并且在拼写检查器中请引用this进行模糊搜索的应用)。

在您的情况下,如果您的索引大小不是很大,并且建议将最小字符长度限制为两个,则建议使用prefix query,即,不要搜索e,仅当用户键入两个或多个字符时才显示搜索结果即ememiemin等。

工作实例

索引映射

{
"mappings": {
"properties": {
"artistName": {
"type": "text"
}
}
}
}

索引文件
{
"artistName" : "Eminem"
}

{
"artistName" : "Emiten"
}

搜索查询
{
"query": {
"prefix": {
"artistName": {
"value": "em"
}
}
}
}

搜索结果
{
"_index": "so-60558525-auto",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"artistName": "Eminem"
}
},
{
"_index": "so-60558525-auto",
"_type": "_doc",
"_id": "2",
"_score": 1.0,
"_source": {
"artistName": "Emiten"
}
}

重要阅读

您可以选择大致四种方法来实现自动完成,并且每种方法都需要权衡取舍,您应该了解这些方法可以证明您的功能需求和非功能需求(性能,维护,实现方面的困难)是合理的。

考虑到Autocomplete功能在现代搜索引擎中的重要性和存在性,有一个detailed blog,其中详细提及了所有方法及其折衷方案。

关于javascript - Elasticsearch-模糊搜索未给出建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60558525/

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