How To Search

Tips and tricks for building delightful search experiences.

Elasticsearch: bulk importing a JSON file

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: 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....

1 min · Kishore Nallan

Elasticsearch: how to include specific fields in a query

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....

1 min · Kishore Nallan

Elasticsearch: Max virtual memory areas vm.max_map_count is too low

If your Elasticsearch data node service is refusing to start with this error: [1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144] You’ve to increase the mmap count limit by running 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

Elasticsearch: must not match text 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 to condition using query string query

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": "!...

1 min · Kishore Nallan

Full Text search in MongoDB (with examples)

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

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'....

7 min · Sabesh Bharathi

Fuzzy Search with Javascript

Fuzzy Search refers to the process of approximately searching for a given search query. It may also be called as a “typo tolerant search”. 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)

In information systems, it is common to have the same entity being 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. Let’s explore how we can utilize various fuzzy string matching algorithms in Python to compute similarity between pairs of strings....

4 min · Kishore Nallan

What is fuzzy search?

Fuzzy search is the process of finding strings that approximately match a given string. A fuzzy matching search algorithm will be able to find relevant strings even if your original string contains typo errors and misspellings. Ever seen Google correcting you like this? That’s Google using fuzzy search to match your misspelled query to the word with the correct spelling. Fuzzy search is effective, when it can reliably detect the intent behind a given search term....

2 min · Kishore Nallan