Search
Close this search box.

Exclude index in Elasticsearch when searching

Table of Contents

1. Introduction

When you search through the indexes you have certain pattern. That pattern defines what indexes and then what shards will be called. To exclude indexes from being part of the search action you need to focus of that pattern.

2. Create test data

To have hands dirty start Elasticsearch node and load data into it.

				
					docker run --rm -d \
--name elk \
-p 9300:9300 \
-p 9200:9200 \
docker.elastic.co/elasticsearch/elasticsearch:8.8.1

curl -k -XPOST -u elastic "https://localhost:9200/vehicle-truck/_doc" -H 'Content-Type: application/json' -d'
{
    "brand": "KrAZ"
}'

curl -k -XPOST -u elastic "https://localhost:9200/vehicle-car/_doc" -H 'Content-Type: application/json' -d'
{
    "brand": "toyota"
}'

curl -k -XPOST -u elastic "https://localhost:9200/vehicle-motorcycle/_doc" -H 'Content-Type: application/json' -d'
{
    "brand": "ducati"
}'
				
			

3. Search with exclusion

To search through all vehicles except trucks means without vehicle-truck index use pattern

vehicle-*,-vehicle-truck
Which means all indexes starting from ‘vehicle-‘ excluding vehicle-truck.

				
					curl -k -XGET -u elastic "https://localhost:9200/vehicle-*,-vehicle-truck/_search"
				
			
Follow me on LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *

Share the Post:

Enjoy Free Useful Amazing Content

Related Posts