我有一个充满文件的Elasticsearch实例,其中包含电影和系列标题。
当我运行这个:
{
"query": {
"bool": {
"must": [
{
"multi_match": {
"fields": [
"Name^2",
"SeriesName^1.5",
"Description"
],
"fuzziness": "AUTO",
"prefix_length": 2,
"query": "game"
}
}
]
}
}
}
...我得到诸如“大游戏”,“饥饿游戏”,“ war 游戏”之类的标题。
但是,我想让标题以“game”开头,而标题仅包含“game”。
当用户搜索“游戏”时,他们期望在“模仿游戏”之前出现“权力的游戏”和“游戏改变”之类的标题。
我怎样才能使它更精确?谢谢!
最佳答案
尝试如下所示:
{ "query": {
"prefix" : { "Name" : "game" }
}
}
请引用相同Elasticsearch Documentation的文档
为此,您必须将字段/属性标记为作为关键字,请参见下面的查询。您还可以在映射中为您的字段/属性添加其他小写过滤器。
{
"settings": {
"index": {
"analysis": {
"analyzer": {
"analyzer_startswith": {
"tokenizer": "keyword",
"filter": "lowercase"
}
}
}
}
},
"mappings": {
"test_index": {
"properties": {
"Name": {
"search_analyzer": "analyzer_startswith",
"index_analyzer": "analyzer_startswith",
"type": "string"
}
}
}
}
}
关于elasticsearch - 如何在Elasticsearch中区分字符串开头的匹配优先级?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56664377/