Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching multiple types in elasticsearch

I have a usecase where there are two different types in the same index. Both the types have different structure and mapping.

I need to query both types at the same time using different query DSL.

How can I build my query DSL to simultaneously query more than one type of the same index.

I looked into elasticsearch guide at https://www.elastic.co/guide/en/elasticsearch/guide/current/multi-index-multi-type.html but there is no proper explanation here. According to this even if I set two different types in my request :

/index/type1,type2/_search

I will have to send the same query DSL.

like image 802
Amriteya Avatar asked Apr 22 '16 12:04

Amriteya


People also ask

How do I search multiple indices in Elasticsearch?

To search multiple data streams and indices, add them as comma-separated values in the search API's request path. The following request searches the my-index-000001 and my-index-000002 indices. You can also search multiple data streams and indices using an index pattern.

Can Elasticsearch index have multiple types?

From Elasticsearch version 6.0 by default index doesn't allow multiple types per index. The better option is to always have one document type per index.

Can we join two indexes in Elasticsearch?

While fetching data from elasticsearch can we join two indexes in query. Is it possible? No, Elasticsearch does not support joins between indices.

Is Elasticsearch good for full-text search?

It stores and indexes documents. Indexing creates or updates documents. After indexing, you can search, sort, and filter complete documents—not rows of columnar data. This is a fundamentally different way of thinking about data and is one of the reasons ElasticSearch can perform a complex full-text search.


1 Answers

You need to use multi-search API and the _msearch endpoint

curl -XGET localhost:9200/index/_msearch -d '
{"type": "type1"}
{"query" : {"match_all" : {}}, "from" : 0, "size" : 10}
{"type": "type2"}
{"query" : {"match_all" : {}}, "from" : 0, "size" : 10}
'

Note: make sure to separate each line by newlines (including the last line)

You'll get two responses in the same order as the requests

like image 171
Val Avatar answered Sep 21 '22 10:09

Val