> For the complete documentation index, see [llms.txt](https://shoppinpal.gitbook.io/docs-shoppinpal-com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://shoppinpal.gitbook.io/docs-shoppinpal-com/6.-elasticsearch/ngram-with-elasticsearch.md).

# NGram with Elasticsearch

## Setup a sandbox

***Note**: Even without the video, you can learn all the crucial details from the steps that are documented below*

{% embed url="<https://youtu.be/vinrUb8vab8>" %}

1. Login to your [cloud-box](https://github.com/shoppinpal/docs-shoppinpal-com/tree/8cdd099563d11c1fe1df4d584b504337850c59eb/setup-a-machine-in-the-cloud.html) over `ssh`
2. Create a directory for running an elasticsearch sandbox:

   ```
   mkdir -p ~/dev/elasticsearch-sandbox
   ```
3. Step into the working directory:

   ```
   cd ~/dev/elasticsearch-sandbox
   ```
4. Create a `docker-compose.yml` file to install and run elasticsearch:

   ```
   ## Version Selection for compose file
   # https://docs.docker.com/compose/compose-file/#/versioning
   version: '2'
   services:
      es_v2:
       image: elasticsearch:2
       ports:
        - "9202:9200"
       volumes:
        - ./docker-entrypoint-es2-plugins.sh:/apps/docker-entrypoint-es2-plugins.sh
       entrypoint: /apps/docker-entrypoint-es2-plugins.sh
   ```
5. Create an entrypoint file named `docker-entrypoint-es2-plugins.sh` to install useful plugins:

   ```
   #!/bin/bash
   # setting up prerequisites

   # re-runs will give an error that is harmless:
   #   > ERROR: plugin directory /usr/share/elasticsearch/plugins/delete-by-query already exists.
   #   > To update the plugin, uninstall it first using 'remove delete-by-query' command
   #plugin install delete-by-query

   # https://github.com/mobz/elasticsearch-head/#running-as-a-plugin-of-elasticsearch
   plugin install mobz/elasticsearch-head

   # access it at /_plugin/elasticsearch-inquisitor/
   plugin install polyfractal/elasticsearch-inquisitor

   #exec /docker-entrypoint.sh elasticsearch
   exec elasticsearch -Des.insecure.allow.root=true
   ```
6. Make sure to change the permissions to execute `sh` files:

   ```
   chmod 744 *.sh
   ```
7. Start the service: `docker-compose up`
8. Open a browser to view the two plugins running on ES:
   1. esHead - <http://your-cloud-box-ip:9202/_plugin/head/>
   2. esInq - <http://your-cloud-box-ip:9202/_plugin/elasticsearch-inquisitor/>

## Play Around

1. Use the `Any Request` tab in `/_plugin/head/` to create an index with a custom analyzer for a 3 by 3 ngram:

   ```
   PUT testing_ngram_3_by_3
   {
     "settings": {
       "analysis": {
         "analyzer": {
           "ngram_3_by_3": {
             "tokenizer": "ngram_3_by_3"
           }
         },
         "tokenizer": {
             "ngram_3_by_3": {
             "type": "ngram",
             "min_gram": 3,
             "max_gram": 3,
             "token_chars": [
               "letter",
               "digit"
             ]
           }
         }
       }
     }
   }
   ```
2. Go to `/_plugin/elasticsearch-inquisitor/#/analyzers` to see the `ngram_3_by_3` analyzer at the bottom of the page.
   * click the checkbox and then use the top most input field to see how the analyzer breaks down the input into tokens

     ![](/files/-LBPkSRCZNifiXZF26zQ)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://shoppinpal.gitbook.io/docs-shoppinpal-com/6.-elasticsearch/ngram-with-elasticsearch.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
