仅返回匹配字段,而不是返回整个文档

时间:2019-07-18 16:28:24

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

仅返回匹配的源,而不返回包含该文本的整个弹性搜索文档

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

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中则只有标题是回报

1 个答案:

答案 0 :(得分:0)

通常,我们使用_source过滤来指定搜索后我们想要的字段,但这并不意味着所有_source字段都与我的查询字符串匹配。但是,由于只需要与搜索查询匹配的字段,在这种情况下,您可以将Elasticsearh的 highlight 功能与multi_match一起使用,

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

当我在elasticsearch索引上运行此命令时,这就是我得到的答复。如果您在highlight字段上仔细查看,它将显示哪个字段与您的查询字符串匹配,即 guide 在这种情况下,它也使用<em>Guide</em>标签来强调它。对于第一个文档,它匹配夏季字段,对于第二个文档,它匹配标题字段。

{
  "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>"
          ]
        }
      }
    ]
  }
}