5

Return only matched source instead of returning the whole document of elastic search that contain that text

Suppose I have a data in this format,

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

I want to search text having guide in it .Instead of returning the whole document i want to have only those fields that contain the text query

GET /bookdb_index/book/_search?q=guide

when i run this it return me whole document .however i want to return only those fields that contain guide like in id 4 in summary guide is there so only summary field return and in id 1 in title guide is there so only title is return

A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
mansi
  • 73
  • 5
  • Can I also find out the count of searched text in the document .I mean if i search quide can i count how many times the quide occur in each document and in which field. – mansi Jul 18 '19 at 19:22
  • I've added an answer for you, if you want to count how may times the `guide` string occurs in each document then you can count the occurrence of `guide` on which the string matches. Hope it helps, best of luck – A l w a y s S u n n y Jul 19 '19 at 00:51

1 Answers1

2

Usually we use _source filtering to specify which field we want after search but that not implies that all of the _source field matches my query string. But as you want only the field that matches your search query, for this case you can use highlight feature of Elasticsearh along with multi_match this way,

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

When I run this on my elasticsearch index this is what I get as response, If you see carefully on the highlight field it will show which field is match with your query string i.e guide in this case and it also emphasis it with and <em>Guide</em> tag. For 1st document it matches the summery field and for 2nd one it matches the title field.

{
  "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>"
          ]
        }
      }
    ]
  }
}
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103