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

标签 elasticsearch kibana elastic-stack amazon-elastic-beanstalk elasticsearch-aggregation

只返回匹配的源而不是返回包含该文本的整个 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/

相关文章:

elasticsearch - ElasticSearch从查询自动完成功能开始

elasticsearch - kibana 中的 circuit_breaking_exception

elasticsearch - 仅将一定百分比的日志发送到logstash

elastic-stack - 微服务架构上的 ELK Stack

elasticsearch - Elasticsearch 7 track_total_hits 如何提高查询速度?

elasticsearch - 弹性重新编制索引的日期格式太短

bash - 使用Bash循环删除未分配的Graylog2索引/碎片

elasticsearch - 发现页-在搜索栏中找不到,但可以在过滤器中找到

elasticsearch - 如何在 Elasticsearch 中聚合每年基于特定日期的字段?

elasticsearch - 如何将我的 Elastic Search 服务器暴露在互联网上?