elasticsearch查询:获取所有前缀的结果与打字型,自动完整功能匹配的查询

发布于 2025-02-01 12:42:00 字数 502 浏览 5 评论 0原文

查询
我的名字是什么
Google是
什么是stackoverflow
如何搜索

,所以我想拥有一个具有上述文档的elasticsearch索引。我想获取与查询相匹配的所有前缀的结果。例如,当查询“什么”时,我需要{“我的名字”,“什么是Google”,“ stackoverflow”}作为结果,确切的前缀匹配。

如何创建索引?如果可能的话,示例查询。

提前致谢。

query
what is my name
what is google
what is stackoverflow
how to search

So, I would like to have an Elasticsearch Index which has documents as above. I would like to fetch all the results which prefix matches the query. For example, when queried for "what is", I need {"what is my name", "what is google", "what is stackoverflow"} as results, exact prefix match.

How do I go about creating an index? and Example queries, if possible.

Thanks in advance.

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

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

发布评论

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

评论(1

妞丶爷亲个 2025-02-08 12:42:00

多种方法 ,但最简单的是使用查询,如果您只将数据直接索引到eLasticsearch而不定义映射,则它将自动为您创建两个字段,并且在.keyword字段上,您可以使用前缀查询,如下所示。

索引示例文档

PUT <your-es-index>/_doc/1

{
    "title" : "what is my name"
}

PUT <your-es-index>/_doc/2

{
    "title" : "what is google"
}

PUT <your-es-index>/_doc/3

{
    "title" : "what is stackoverflow"
}

PUT <your-es-index>/_doc/4

{
    "title" : "how to search"
}

搜索查询

post/_search

{
    "query": {
        "prefix": {
            "title.keyword": {
                "value": "what is"
            }
        }
    }
}

您的预期搜索结果

"hits": [
            {
                "_index": "72391510",
                "_id": "1",
                "_score": 1.0,
                "_source": {
                    "title": "what is my name"
                }
            },
            {
                "_index": "72391510",
                "_id": "2",
                "_score": 1.0,
                "_source": {
                    "title": "what is google"
                }
            },
            {
                "_index": "72391510",
                "_id": "3",
                "_score": 1.0,
                "_source": {
                    "title": "what is stackoverflow"
                }
            }

There are multiple ways to achieve what you want, but simplest is to use the prefix query, if you just index your data directly to Elasticsearch without defining mapping, it will automatically create two fields for you, and on .keyword field you can use the prefix query as shown below.

Index sample documents

PUT <your-es-index>/_doc/1

{
    "title" : "what is my name"
}

PUT <your-es-index>/_doc/2

{
    "title" : "what is google"
}

PUT <your-es-index>/_doc/3

{
    "title" : "what is stackoverflow"
}

PUT <your-es-index>/_doc/4

{
    "title" : "how to search"
}

Search query

POST /_search

{
    "query": {
        "prefix": {
            "title.keyword": {
                "value": "what is"
            }
        }
    }
}

Your expected search results

"hits": [
            {
                "_index": "72391510",
                "_id": "1",
                "_score": 1.0,
                "_source": {
                    "title": "what is my name"
                }
            },
            {
                "_index": "72391510",
                "_id": "2",
                "_score": 1.0,
                "_source": {
                    "title": "what is google"
                }
            },
            {
                "_index": "72391510",
                "_id": "3",
                "_score": 1.0,
                "_source": {
                    "title": "what is stackoverflow"
                }
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文