设置Elasticsearch匹配预定

发布于 2025-01-23 17:07:33 字数 174 浏览 4 评论 0原文

因此,如果用户使用单词covid搜索,我想要所有这些结果,其中句子的标题以word covid开始,然后我想要所有这些项目标题的一部分具有单词covid。我该如何实现?

我想要更具体的答案,如何使用Searchkick做到这一点。

So if a user search with the word covid, i want all those results first, where title of the sentence starts with word covid and then I want all those items where in other parts of the title have the word have word covid. How can I achieve this?

I want more specific answer, how to do that with searchkick.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

舞袖。长 2025-01-30 17:07:33

如果使用默认映射,则可以使用bool与两个的查询子句,一个在text>上使用match> match /code>,另一个是前缀 .keyword子字段的查询,如下所示。

索引示例文档

{
    "name" : "foo bar"
}
{
    "name" : "bar foo"
} 

搜索查询

{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "name" : "foo"
                    }
                },
                {
                    "prefix": {
                        "name.keyword": "foo"
                    }
                }
            ]
        }
    }
}

搜索结果

 "hits": [
            {
                "_index": "71998426",
                "_id": "1",
                "_score": 1.1823215,
                "_source": {
                    "name": "foo bar"
                }
            },
            {
                "_index": "71998426",
                "_id": "2",
                "_score": 0.18232156,
                "_source": {
                    "name": "bar foo"
                }
            }
        ]

注意:首先,拥有foo bar得分高得多首先出现

If you are using the default mapping, You can use the bool query with two should clause, one with match on the text and another is prefix query on .keyword subfield as shown in below example.

Index sample documents

{
    "name" : "foo bar"
}
{
    "name" : "bar foo"
} 

Search query

{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "name" : "foo"
                    }
                },
                {
                    "prefix": {
                        "name.keyword": "foo"
                    }
                }
            ]
        }
    }
}

Search results

 "hits": [
            {
                "_index": "71998426",
                "_id": "1",
                "_score": 1.1823215,
                "_source": {
                    "name": "foo bar"
                }
            },
            {
                "_index": "71998426",
                "_id": "2",
                "_score": 0.18232156,
                "_source": {
                    "name": "bar foo"
                }
            }
        ]

Note: first result, having foo bar is scored much higher and comes first in the search hits.

寄人书 2025-01-30 17:07:33

您甚至可以使用Boost,我想对@Amit代码的修改很少

        "query": {
            "bool": {
                "should": [
                    {
                        "match": {
                            "name" : "covid",
                            "boost" : 0.5
                        }
                    },
                    {
                        "prefix": {
                            "name.keyword": "foo",
                            "boost" : 1.0
                        }
                    }
                ]
            }
        }
    }```

you can even use boost i guess little modification to @Amit code

        "query": {
            "bool": {
                "should": [
                    {
                        "match": {
                            "name" : "covid",
                            "boost" : 0.5
                        }
                    },
                    {
                        "prefix": {
                            "name.keyword": "foo",
                            "boost" : 1.0
                        }
                    }
                ]
            }
        }
    }```
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文