Elasticsearch Aggregations - Building E-Commerce Facets

If you have ever shopped online, you have used facet filtering. When you search for “shoes” and then narrow down your results by clicking on “Brand: Nike,” “Size: 10,” and “Price: $50 - $100” in the sidebar, you are interacting with facets. In the world of Elasticsearch, building these sidebar filters requires using a feature called Aggregations. In this guide, we will explore how to successfully build e-commerce facet filtering using Elasticsearch aggregations. We’ll walk through how to construct these queries, how to handle user selections with post_filter, and finally, how purpose built search engines offer a different approach to managing this complexity as your application grows. ...

7 min · Nikhil Adiga

Elasticsearch Bool Query: must vs should Explained Simply

must and should are often described as Elasticsearch’s versions of AND and OR. That shorthand is useful until both clauses appear in the same bool query. A must clause is always required. A should clause may be required, or it may only improve the score of documents that already match. The difference depends on the other clauses in that particular bool query. The examples below use five products and were checked against Elasticsearch 8.19.0: ...

8 min · Nikhil Adiga

Elasticsearch Bulk API: Import a JSON File (with Examples)

You can use the _bulk API to bulk import a large JSON file into Elasticsearch. curl -s -H "Content-Type: application/json" -XPOST \ 'http://localhost:9200/_bulk' --data-binary @/path/to/products.json The products.json file must be formatted in the following way: { "index" : { "_index" : "products", "_id" : "1" } } { "title" : "Nike Running Shoes" } { "index" : { "_index" : "products", "_id" : "2" } } { "title" : "Adidas Basketball Shoes" } As you can see in the file snippet above, each record requires two lines: ...

1 min · Kishore Nallan

Elasticsearch Joins: Nested, Parent-Child and LOOKUP JOIN

If you’ve ever kept orders in one Elasticsearch index and products in another, you’ve probably looked for the JOIN that shows each order with its product’s name and price. Elasticsearch’s main search API, the Query DSL, has no join across indices. Instead, it has six options that each solve part of the problem. They are denormalization, nested fields, parent-child relationships, lookup runtime fields, and the ES|QL commands ENRICH and LOOKUP JOIN, which is the closest to a SQL join. ...

13 min · Nikhil Adiga

Elasticsearch must_not: Exclude Matches in a Bool Query

In Elasticsearch, to match a given text in a field, you will do it this way: GET /_search { "query":{ "match":{ "country": "France" } } } The must_not bool query Similarly, to identify records whose field value does NOT match a given query string, you can do so using a must_not boolean query: GET /_search { "query": { "bool": { "must_not": [ { "term": { "country": "France" } } ] } } } If this looks a bit verbose, you can also get the same results using a query_string query. ...

1 min · Kishore Nallan

Elasticsearch Not Equal: Query String Examples

Elasticsearch offers a versatile and powerful way to search for records using the query_string query. GET /_search { "query": { "query_string": { "query": "country: France" } } The NOT operator Similarly, to find documents whose field value is NOT equal to a given query string, you can do so using the NOT operator. GET /_search { "query": { "query_string": { "query": "NOT (country: France)" } } # or GET /_search { "query": { "query_string": { "query": "!(country: France)" } } If you want to find documents whose field value does not match multiple values, you need to use a space separated list. ...

1 min · Kishore Nallan

Elasticsearch Pagination: search_after and the 10,000 Limit

If you have ever clicked through multiple pages of search results, you have experienced pagination. While most users rarely click past the first page or two, background workers, data exporters, synchronization jobs, and power users often need to traverse tens of thousands or even millions of documents. If you attempt to do this in Elasticsearch using traditional offset-based pagination, you will quickly encounter this error: Result window is too large, from + size must be less than or equal to: [10000] This 10,000-document limit is not an arbitrary restriction. It is a safeguard built into Elasticsearch to protect your cluster from excessive memory consumption and expensive query execution. ...

7 min · Nikhil Adiga

Elasticsearch: Failed to connect to localhost port 9200: Connection refused

If you’re getting the following error message when you try to access your Elasticsearch service: $ curl http://localhost:9200/ Failed to connect to localhost port 9200: Connection refused This is likely because your Elasticsearch is not listening on the correct IP address of your network. For example, this is a common issue when you run Elasticsearch inside Docker. To fix this, open /etc/elasticsearch/elasticsearch.yml and add this: network.host: 0.0.0.0 This instructs Elasticsearch service to listen on all local addresses and will get you going. ...

1 min · Kishore Nallan

Elasticsearch: how to exclude fields in a query

While querying Elasticsearch, sometimes you might want to exclude certain large fields from the response. To exclude specific fields in an Elasticsearch query, use source filtering: GET /_search { "_source": { "exclude": ["description"] }, "query" : { "term" : { "country" : "france" } } } You can also do source filtering using a GET parameter. curl "localhost:9200/<index>/_doc/1?_source_excludes=description" NOTE: you can also include only specific fields in a search response. Elasticsearch can have a steep learning curve. If you're looking for a fast but easier-to-use alternative, take a look at Typesense.

1 min · Kishore Nallan

Elasticsearch: Return Only Specific Fields (_source Filtering)

While querying Elasticsearch, it’s a good practice to restrict your search results to only the fields you need. To include only specific fields in an Elasticsearch query, use source filtering: GET /_search { "_source": { "include": ["title", "country"] }, "query" : { "term" : { "country" : "france" } } } You can also do source filtering using a GET parameter. curl "localhost:9200/<index>/_doc/1?_source_includes=title,country" NOTE: you can also exclude specific fields in a search response. Elasticsearch can have a steep learning curve. If you're looking for a fast but easier-to-use alternative, take a look at Typesense.

1 min · Kishore Nallan

Full text search in MySQL (with examples)

While MySQL is not a fully featured text search engine, it has enough tricks up its sleeve to be useful for implementing basic search features in your application. Let’s take a quick walk through. First, let’s open up the mysql prompt and create a new Database and call it restaurant. Now, we can proceed to create a TABLE to store our records. We must be careful here as we need to clearly specify the fields that need to be ‘Full-text indexed’. Full-text search queries are only valid on fields that are full-text indexed. Full-text indexes can be created only for VARCHAR, CHAR, or TEXT columns. ...

7 min · Sabesh Bharathi

Fuzzy Search in Elasticsearch: A Guide to Typo Tolerance

Elasticsearch handles typos with the fuzziness parameter on match queries, or with the separate fuzzy query. Setting "fuzziness": "AUTO" allows zero, one or two character edits depending on the length of the term. Typo tolerance (or fuzzy search) ensures that your search engine can still return relevant results even when the user’s query contains spelling mistakes. For example, a search for “iphne” should return results for “iphone.” In this guide, we’ll dive into how to implement typo tolerance in Elasticsearch, the complexities involved, and an alternative, simpler approach. ...

6 min · Nikhil Adiga

Fuzzy Search in JavaScript with Fuse.js (with Examples)

Fuzzy search finds approximate string matches when a query contains a typo or spelling variation. In JavaScript, Fuse.js provides that behavior for small datasets that can be loaded into the browser. This guide builds a working Fuse.js search, then tunes how its matches are scored. Working in another language? The companion guide covers fuzzy string matching in Python. Let’s say you want to search a recipe database for szechuan noodles but you can’t remember how exactly szechuan is spelt. So you can type in a query that you think is the closest to the actual query: schezwan noodles and it will give you results for similar words and you will end up finding the intended recipe for Szechuan Noodles. ...

6 min · Sabesh Bharathi

Fuzzy string matching in Python (with examples)

Fuzzy string matching in Python helps identify text that is similar but not identical. In information systems, it is common to have the same entity represented by slightly varying strings. Consider the following: Joe Biden Joseph Biden Joseph R Biden All three strings refer to the same person, but in slightly different ways. Fuzzy search is the process of finding strings that approximately match a given string. If you need the same behavior in a browser, see the companion Fuse.js fuzzy-search guide. ...

4 min · Kishore Nallan

Levenshtein Distance: Algorithm, Calculator and Python & JS Code

Levenshtein distance is the smallest number of single-character insertions, deletions and substitutions that turn one string into another. It’s the most common form of edit distance. kitten and sitting are 3 edits apart, for example, and kiten and kitten are 1 edit apart. In this guide, we’ll walk through the algorithm with a worked example, give you a calculator to try your own strings, write it in Python and JavaScript, and cover its speed, accuracy and related algorithms. We’ll finish with how Typesense uses the same idea for typo tolerance. ...

12 min · Nikhil Adiga

MongoDB Full-Text Search with Text Indexes

This post will show you how to do a full-text search using text indexes on a MongoDB collection. I will use be using Nodejs code snippets in the examples. For other client libraries and mongo shell commands please refer to the documentation. Creating a Text Index To create a text index use createIndex() function, await client .db("<database-name>") .collection("<books>") .createIndex({ name: "text", description: "text", }); The above code creates an index from the name and description fields. ...

3 min · Harisaran

pgvector: Vector Search in PostgreSQL with HNSW and IVFFlat

If you’ve ever typed “the movie where the toys are alive when nobody is watching” into a search bar and got Toy Story back, you’ve used semantic search. None of those words has to appear in the movie’s plot, so keyword search can miss it. Semantic search compares meaning instead. A model turns each text into an embedding, a list of numbers, and texts with similar meanings get similar embeddings. ...

12 min · Nikhil Adiga

Postgres Full-Text Search with PostGIS: Fast Nearby Search

Location-aware search is one of the most useful features of modern applications. A user who searches for “coffee” expects coffee shops near them without typing a location, and they expect the results instantly. Postgres can handle this without any extra infrastructure. The PostGIS extension handles location, and full-text search handles the words. The difficult part is the query plan. The same SQL can take 0.2 ms or 36 ms, and the only difference is the word that the user typed. ...

13 min · Nikhil Adiga

vm.max_map_count Is Too Low: How to Fix It in Elasticsearch

If Elasticsearch fails to start with this error: [1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144] Raise the vm.max_map_count limit on the host machine with this command: sysctl -w vm.max_map_count=262144 This temporarily increases the limit, but to persist this change permanently, you have to add the following line to the /etc/sysctl.conf file: vm.max_map_count = 262144 If you are running Elasticsearch via Docker, you have to restart your Docker container after making this change to the host machine.

1 min · Kishore Nallan

What Is Fuzzy Search? Meaning, Algorithms and Examples

Fuzzy search is a search technique that finds results that closely match a query even when the words are misspelled or slightly different, usually by measuring how many character edits separate two words (edit distance). It is the reason a search for samsng can still find “Samsung” and szechun noodles can still find “Szechuan noodles.” Ever seen Google correcting you like this? That is fuzzy matching at work. The search system considers terms that are close to the query, then ranks the most plausible results. The user gets a useful result without having to stop, spot the typo, and search again. ...

7 min · Kishore Nallan

What Is Semantic Search? Semantic vs Keyword vs Hybrid Search

If you’ve ever typed “something to keep my tea warm on the train” into a store’s search bar and got a travel mug back, even though no product says “tea” or “train”, you’ve used semantic search. Semantic search finds documents by meaning instead of by shared words. A machine learning model turns every piece of text into an embedding, a vector of numbers, and texts with similar meanings get embeddings that sit close together. Keyword search matches the words themselves. Hybrid search runs both and merges the two rankings into one list. ...

13 min · Nikhil Adiga