Getting started with ElasticSearch on Node

“Searching,
Seek and Destroy
Searching,
Seek and Destroy” – Metallica

I recently had to set up ElasticSearch within a Node project.  I found the supporting documentation to be scattered and had a difficult time finding examples of what I would consider everyday production configurations.  So I’ve gathered a lot of what I learned here and hopefully this saves some of you a bit of time.

Get the elasticsearch-js client

First, use the elasticsearch-js npm package.  This seems to be actively developed.  Specifically, do not use the elasticsearchclient package as that seems to be end-of-lifed.  Create your client like:

ElasticSearch = require("elasticsearch")
client = new ElasticSearch.Client {host: 'localhost:9200'}

Create the index

Next, you’ll want to create your indices.  This is where it can get overwhelming.  ElasticSearch is great because it can be configured in countless ways, but understanding all the variants can be hard at the beginning.  Here is what I think would be a great place to start.  Using your ES client, create an index ‘users’:

client.indices.create
  index: "users"
   type: "user"
   body:
     settings:
       analysis:
        filter:
          mynGram:
             type: "nGram"
             min_gram: 2
             max_gram: 12
          analyzer:
             default_index:
               type: "custom"
               tokenizer: "standard"
               filter: [
                 "lowercase"
              ]
             ngram_indexer:
               type: "custom"
               tokenizer: "standard"
               filter: [
                "lowercase"
                "mynGram"
              ]
             default_search:
              type: "custom"
              tokenizer: "lowercase"
              filter: [
                "standard"
              ]
     mappings:
       dynamic: "true"
       properties:
         id:
           type: "string"
           index: "not_analyzed"
         name:
          type: "string"
          index_analyzer: "ngram_indexer"
         suggest:
           type: "completion"
           index_analyzer: "simple"
           search_analyzer: "simple"
           payloads : true

Here we have:

  • defined the default index and search analyzers (default_index, default_search).  These will be used to index all fields unless otherwise specified, such as in…
  • mappings.properties.name.  Here we want to use an ngram tokenizer on the user’s name so we can do fun things like search within names so searching on ‘ike’ would return Mike and Ike.
  • defined a completion suggester index in mappings.properties.suggest

Test the setup

That’s the bare bones getting started.  To test out what your indexer is doing, you can simply run a query, specify the indexer you want to test out, and you’ll get a list of all the tokens that are being generated from that indexer.

localhost:9200/users/_analyze?pretty&analyzer=ngram_indexer&text=Michael
Advertisements