gpt4 book ai didi

elasticsearch - 只返回匹配字段而不是返回整个文档

转载 作者:行者123 更新时间:2023-11-29 02:57:25 25 4
gpt4 key购买 nike

只返回匹配的源而不是返回包含该文本的整个 Elasticsearch 文档

假设我有一个这种格式的数据,

POST /bookdb_index/book/_bulk
{ "index": { "_id": 1 }}
{ "title": "Elasticsearch: The Definitive Guide", "authors": ["clinton gormley", "zachary tong"], "summary" : "A distibuted real-time search and analytics engine", "publish_date" : "2015-02-07", "num_reviews": 20, "publisher": "oreilly" }
{ "index": { "_id": 2 }}
{ "title": "Taming Text: How to Find, Organize, and Manipulate It", "authors": ["grant ingersoll", "thomas morton", "drew farris"], "summary" : "organize text using approaches such as full-text search, proper name recognition, clustering, tagging, information extraction, and summarization", "publish_date" : "2013-01-24", "num_reviews": 12, "publisher": "manning" }
{ "index": { "_id": 3 }}
{ "title": "Elasticsearch in Action", "authors": ["radu gheorge", "matthew lee hinman", "roy russo"], "summary" : "build scalable search applications using Elasticsearch without having to do complex low-level programming or understand advanced data science algorithms", "publish_date" : "2015-12-03", "num_reviews": 18, "publisher": "manning" }
{ "index": { "_id": 4 }}
{ "title": "Solr in Action", "authors": ["trey grainger", "timothy potter"], "summary" : "Comprehensive guide to implementing a scalable search engine using Apache Solr", "publish_date" : "2014-04-05", "num_reviews": 23, "publisher": "manning" }

我想搜索其中包含指南的文本。我不想返回整个文档,而是只希望包含那些包含文本查询的字段

GET /bookdb_index/book/_search?q=guide

当我运行它时,它会返回整个文档。但是我只想返回那些包含指南的字段,例如摘要指南中的 id 4 那里只有摘要字段返回,而标题指南中的 id 1 中只有标题是返回

最佳答案

通常我们使用_source过滤以指定搜索后我们想要的字段,但这并不意味着所有 _source字段匹配我的查询字符串。但是由于您只需要与搜索查询匹配的字段,因此对于这种情况,您可以使用 highlight Elasticsearch 的特性以及 multi_match这样,

GET /bookdb_index/book/_search
{
"query": {
"multi_match": {
"query": "guide",
"fields": ["title", "summary"]
}
},
"highlight": {
"fields": {
"title": {},
"summary": {}
}
},
"size": 10
}

当我在我的 elasticsearch 索引上运行它时,这就是我得到的响应,如果你仔细看 highlight field 它将显示哪个字段与您的查询字符串匹配,即 guide 在这种情况下,它还会强调它和 <em>Guide</em>标签。对于第一个文档,它与 summery 字段匹配,对于第二个文档,它与 title 字段匹配。

{
"took": 84,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1.3278645,
"hits": [
{
"_index": "bookdb_index",
"_type": "book",
"_id": "4",
"_score": 1.3278645,
"_source": {
"title": "Solr in Action",
"authors": [
"trey grainger",
"timothy potter"
],
"summary": "Comprehensive guide to implementing a scalable search engine using Apache Solr",
"publish_date": "2014-04-05",
"num_reviews": 23,
"publisher": "manning"
},
"highlight": {
"summary": [
"Comprehensive <em>guide</em> to implementing a scalable search engine using Apache Solr"
]
}
},
{
"_index": "bookdb_index",
"_type": "book",
"_id": "1",
"_score": 1.2871116,
"_source": {
"title": "Elasticsearch: The Definitive Guide",
"authors": [
"clinton gormley",
"zachary tong"
],
"summary": "A distibuted real-time search and analytics engine",
"publish_date": "2015-02-07",
"num_reviews": 20,
"publisher": "oreilly"
},
"highlight": {
"title": [
"Elasticsearch: The Definitive <em>Guide</em>"
]
}
}
]
}
}

关于elasticsearch - 只返回匹配字段而不是返回整个文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57099027/

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