Skip to content

Instantly share code, notes, and snippets.

@zahra-ove
Last active May 2, 2025 18:33
Show Gist options
  • Save zahra-ove/b38459e6525f2492cb4314b8207201c9 to your computer and use it in GitHub Desktop.
Save zahra-ove/b38459e6525f2492cb4314b8207201c9 to your computer and use it in GitHub Desktop.

from book: Elasticsearch: The definitive guide

  1. Test it out by opening another terminal window and running the following:\
curl http://localhost:9200/?pretty
  1. A node is a running instance of Elasticsearch. A cluster is a group of nodes with the same cluster.name that are working together to share data and to provide failover and scale, although a single node can form a cluster all by itself.

  2. You should change the default cluster.name to something appropriate to you, like your own name, to stop your nodes from trying to join another cluster on the same network with the same name! You can do this by editing the elasticsearch.yml file in the config/ directory and then restarting Elasticsearch.

  3. When Elasticsearch is running in the foreground, you can stop it by pressing Ctrl-C; otherwise, you can shut it down with the shutdown API:

curl -XPOST http://localhost:9200/_shutdown
  1. The nodes in the cluster also communicate with each other over port 9300. If this port is not open, your nodes will not be able to form a cluster.

  2. All other languages can communicate with Elasticsearch over port 9200 using a RESTful API.

  3. Elasticsearch is document oriented, meaning that it stores entire objects or documents. It not only stores them, but also indexes the contents of each document in order to make them searchable. In Elasticsearch, you index, search, sort, and filter documents.

  4. The act of storing data in Elasticsearch is called indexing, but before we can index a document, we need to decide where to store it.

note: Relational DB vs Elasticsearch

RelationalDB ===> Database ===> Table ===> Row ====> Column
Elasticsearch ===> Indices ===> Types ===> Document ====> Fields

  1. A field without an inverted index is not searchable.

image

  1. Simple! There was no need to perform any administrative tasks first, like creating an index or specifying the type of data that each field contains. We could just index a document directly. Elasticsearch ships with defaults for everything, so all the neces‐ sary administration tasks were taken care of in the background, using default values.

  2. let’s try searching for employees who have “Smith” in their last name. To do this, we’ll use a lightweight search method that is easy to use from the command line. This method is often referred to as a query-string search, since we pass the search as a URL query-string parameter:

GET /megacorp/employee/_search?q=last_name:Smith

Query-string search is handy for ad hoc searches from the command line, but it has its limitations. Elasticsearch provides a rich, flexible, query language called the query DSL, which allows us to build much more compli‐ cated, robust queries.

image

note: in Query DSL, we are no longer using query-string parameters, but instead a request body. This request body is built with JSON, and uses a match query.

  1. By default, Elasticsearch sorts matching results by their relevance score, that is, by how well each document matches the query.

  2. full-text search:\

GET /megacorp/employee/_search
{
  "query" : {
    "match" : {
       "about" : "rock climbing"
    }
  }
}

this query will return all documents which has "rock" or "climbing" in their "about" field and Elasticsearch sorts matching results by their relevance score.

Phrase Search:
Finding individual words in a field is all well and good, but sometimes you want to match exact sequences of words or phrases. \

GET /megacorp/employee/_search
{
  "query" : {
    "match_phrase" : {
       "about" : "rock climbing"
    }
  }
}

Highlighting Our Searches:
Many applications like to highlight snippets of text from each search result so the user can see why the document matched the query.

GET /megacorp/employee/_search
{
  "query" : {
      "match_phrase" : {
        "about" : "rock climbing"
      }
    },
    "highlight": {
      "fields" : {
        "about" : {}
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment