elasticsearchHow can I join two Elasticsearch indices?
Joining two Elasticsearch indices is possible by using the search_after
parameter. It allows you to search for documents after a certain document. The following example code block shows how to join two indices:
# First search
response1 = Elasticsearch::Client.search index: 'index1', body: {
query: {
match: {
field1: 'value1'
}
},
sort: [
{
_id: 'asc'
}
]
}
# Second search
response2 = Elasticsearch::Client.search index: 'index2', body: {
query: {
match: {
field2: 'value2'
}
},
search_after: response1['hits']['hits'].last['_id']
}
This will return the documents from the first index (index1
) sorted by the _id
field, and then the documents from the second index (index2
) sorted by the _id
field after the last document from the first index.
Parts of the code explained:
response1
: The first search request which searches for documents inindex1
and sorts them by the_id
field.response2
: The second search request which searches for documents inindex2
and sorts them by the_id
field after the last document fromindex1
. Thesearch_after
parameter is used here to specify the last document fromindex1
.response1['hits']['hits'].last['_id']
: This is used to get the_id
of the last document fromindex1
which is used as thesearch_after
parameter in the second search request.
Helpful links
More of Elasticsearch
- How do I use an elasticsearch query builder?
- How can I use Elasticsearch and ZFS together?
- How can I use Yandex Mirror to access Elasticsearch data?
- How can I use Elasticsearch and Zookeeper together to manage distributed applications?
- How can I use Elasticsearch to diagnose "yellow" issues?
- How can I use Elasticsearch and Zabbix together for software development?
- How do I set up an Elasticsearch Yum repository?
- How can I use YouTube to learn about Elasticsearch?
- How can I use elasticsearch zone awareness to improve my software development?
- How do I configure elasticsearch to use an XMS memory allocator?
See more codes...