Querying data

To issue a query to Elasticsearch, you send a request in the form of a simple Lucene-based string or you can use the more extensive JSON-based query syntax, DSL. When you query Elasticsearch, you send it as a REST-ful request using any REST client, or as a URI in a browser:


      curl http://localhost:9200/beer-sample/_search?q=blueberry

    

Elasticsearch will return a result set as JSON as follows:


      {"took":2,
"timed_out":false,
....
        "hits" : 8,
    ....
        {
        ....
        "_index":"beer-sample",
        "_type":"couchbaseDocument",
        "_id":"dark_horse_brewing_co-tres_blueberry_stout",
        "_score":1.8963704,
        "_source": ....
        "
        .....
        "_index":"beer-sample",
        "_type":"couchbaseDocument",
        "_id":"yegua_creek_brewing_dallas-blueberry_blonde",
        "_score":1.2890494,
        "_source": ....
        ....
        }
}

    

For the sake of brevity we show just the first two results out of a result set containing eight hits. Each item has a “_score” field which Elasticsearch uses to indicate the level of relevance for search hits. Notice that source attribute will contain only metadata saved by Elasticsearch rather than the entire document contents. We do this because Couchbase Server provides incredibly fast access to the documents. So we use _id sent back by Elasticsearch to retrieve the document out of Couchbase Server. To start we view the document using Couchbase Web Console:

  1. Copy one of the document IDs returned by Elasticsearch, for instance dark_horse_brewing_co-tres_blueberry_stout.
  2. Click on the Data Bucket tab in Couchbase Web Console. A table appears with a list of all Couchbase Buckets.
  3. Click on the Documents button for the beer-sample bucket. A table appears which displays all documents in the bucket.
  4. In the Document ID field, paste the document ID dark_horse_brewing_co-tres_blueberry_stout. The JSON document for that beer will appear. You can click on the document name to view the entire JSON document:

Elasticsearch supports more complex queries using their REST API; for instance you can search the beer database for a style ‘lambic’ and for ‘blueberry’ in the description. In this case you send a HTTP POST request. The JSON request will appear as follows:


      {
"query": {
    "query_string": {
            "query_string": {
                "query": "style: lambic AND description: blueberry"
            }
        }
    }
}

    

Here we scope the search so that it looks for ‘lambic’ in the style field and ‘blueberry’ in the description and we get this result:


      {
    "name" : "Wild Blueberry Lager",
    "abv" : 8,
    "brewery_id" : "110f01",
    "description" : "....blueberry aroma....",
    "style" : "Belgian Fruity Lambic"
    ....
}

    

Rather than using the web console to retrieve a document, you would typically use a Couchbase SDK to retrieve the documents the IDs. There are specific methods and functions available in each SDK to retrieve one or more items based on the IDs. For more information about reading and writing data from an application with Couchbase SDKs, see Couchbase Developer Guides.

For more information about the JSON request and response documents for Elasticsearch, see Elastic Search, Search API.