9951 explained code solutions for 126 technologies


elasticsearchHow can I implement a full text search using Elasticsearch?


Full text search using Elasticsearch can be implemented using the following steps:

  1. Install Elasticsearch on the machine:
# Install Elasticsearch
$ sudo apt-get install elasticsearch
  1. Configure the Elasticsearch instance:
# Configure the Elasticsearch instance
$ vi /etc/elasticsearch/elasticsearch.yml
  1. Start the Elasticsearch instance:
# Start the Elasticsearch instance
$ service elasticsearch start
  1. Create an index:
# Create an index
$ curl -XPUT localhost:9200/myindex
  1. Index some documents:
# Index some documents
$ curl -XPUT 'localhost:9200/myindex/doc/1?pretty' -H 'Content-Type: application/json' -d'
{
  "title": "The quick brown fox",
  "text":  "The quick brown fox jumps over the lazy dog"
}'
  1. Query the index:
# Query the index
$ curl -XGET 'localhost:9200/myindex/_search?q=quick&pretty'
  1. Output:
# Output
{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "myindex",
        "_type" : "doc",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
          "title" : "The quick brown fox",
          "text" : "The quick brown fox jumps over the lazy dog"
        }
      }
    ]
  }
}

Helpful links

Edit this code on GitHub