Reputation: 11
I have an Elastic cluster setup so that there are specific indices for a particular set of documents with id 1234 so the index will be index_2020_02_1234 for documents for customer 1234 in Feb but also have index_2020_02_OTHER for documents for other customers, as customer 1234 has the majority of the documents. For the 'OTHER' indices we have routing using the customer ID.
I would like to search all indices for multiple customers i.e. customer 1234 and 5678.
As indices for customer 1234 doesn't have routing I am unable to use the indeices specific search i.e. /index_2020_02_1234,index_2020_02_OTHER/_search?routing=5678 as index_2020_02_1234 doesn't have routing.
I was wondering if there is a way to build up the Search Query body to do this so I can call /_search endpoint?
I know you can have:
"query": {
"terms": {
"_index": ["index_2020_02_1234", "index_2020_02_OTHER"]
}
}
and
"query": {
"terms": {
"_routing": ["5678"]
}
}
I need a combination of _index and _routing in the query. Is this possible?
Would something like work:
"query": {
"bool": {
"should": [
{
"terms": {
"_index": ["index_2020_02_1234"]
}
},
{
"terms": {
"_index": ["index_2020_02_1234"],
"_routing": ["5678"]
}
}
]
}
}
Upvotes: 1
Views: 978
Reputation: 11
I managed to find the solution where I build upon the query with:
{
“query”: {
“bool”: {
"should": [
{
"terms": {
"_index": [
"index_2020_02_1234",
"index_2020_01_1234"
]
}
},
{
"bool": {
"must": [
{
"terms": {
"_index": [
"index_2020_02_OTHER",
"index_2020_01_OTHER"
]
}
},
{
"terms": {
"_routing": [
5678,
9012
]
}
]
}
}
]
}
}
}
Upvotes: 0
Reputation: 81
Probably you can add same alias to both indices so that you can search with an alias instead of index name.
Checkout Elasticsearch index alias API
In your case, you can index all users data into the same index, then create user-based alias to search for a specific user.
Upvotes: 1