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

man*_*nsi 6 elasticsearch kibana amazon-elastic-beanstalk elastic-stack 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" }
Run Code Online (Sandbox Code Playgroud)

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

GET /bookdb_index/book/_search?q=guide
Run Code Online (Sandbox Code Playgroud)

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

Alw*_*nny 2

通常我们使用_source过滤来指定搜索后想要哪个字段,但这并不意味着所有字段都与_source我的查询字符串匹配。但由于您只需要与您的搜索查询匹配的字段,因此在这种情况下您可以使用Elasticsearh 的突出显示功能以及multi_match这种方式,

GET /bookdb_index/book/_search
{
    "query": {
        "multi_match": {
            "query": "guide",
            "fields": ["title", "summary"]
        }
    },
    "highlight": {
        "fields": {
            "title": {},
            "summary": {}
        }
    },
    "size": 10
}
Run Code Online (Sandbox Code Playgroud)

当我在我的elasticsearch索引上运行这个时,这就是我得到的响应,如果你仔细观察该字段,highlight它将显示哪个字段与你的查询字符串相匹配,即本例中的指南,并且它还用 和 标签强调它<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>"
          ]
        }
      }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)