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 inindex1and sorts them by the_idfield.response2: The second search request which searches for documents inindex2and sorts them by the_idfield after the last document fromindex1. Thesearch_afterparameter is used here to specify the last document fromindex1.response1['hits']['hits'].last['_id']: This is used to get the_idof the last document fromindex1which is used as thesearch_afterparameter in the second search request.
Helpful links
More of Elasticsearch
- How can I use Elasticsearch with Zammad?
- How can I use elasticsearch zone awareness to improve my software development?
- How do I use the Elasticsearch UI?
- How can I use an Elasticsearch tokenizer?
- How do I use Elasticsearch with ZGC?
- How can I use Elasticsearch and ZFS together?
- How do I set up an Elasticsearch Yum repository?
- How do I configure xpack.security.authc.realms in Elasticsearch?
- How do I check which version of Java is compatible with Elasticsearch?
- How do I use Yandex with Elasticsearch?
See more codes...