Reputation: 3282
In elastic search version 7.7, multiple _types in the index is removed, Now If we want to query across multiple index, we are doing in the following way.
/index1,index2/_search?q=type:tweet
In 7.7, what is the best way to query from multiple indexes using Transport Java API?
Edited :
1) Say I have two indexes, "user" and "tweet" I want to search both the index - user and tweet like below
If I want to query the "user" index on the field as {"username" = "Opster"} and in "tweet" index on the field as {"data" = "some_text"}
Is this possible?
2) I understand, each index is a separate partition in elastic search but How does the search across indexes work internally in elastic search?
Thanks,
Harry
Upvotes: 2
Views: 4065
Reputation: 8860
I think the below code should help. Note that you can create TransportClient client
instance as mentioned in this link
In order to execute the search using the Java API, the below code should help:
SearchResponse response = client.prepareSearch("index1", "index2")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(QueryBuilders.termQuery("type", "tweet")) // Query
.setFrom(0).setSize(60) // Set whatever size you'd want
.get();
Some of the below useful API links:
Note: ES recommends people to migrate to Java Rest Client as mentioned in this link and this guide should help you as how you can migrate from Java API to using the REST Client.
Assuming that I have two indexes
user
having field username
with value Opster
tweet
having field data
with value some text
For the sake of simplicity I have made both the fields keyword
type
What you are looking for would be as below
POST /_search
{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"term": {
"_index": "user"
}
},
{
"term": {
"username": "Opster"
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"_index": "tweet"
}
},
{
"term": {
"data": "some text"
}
}
]
}
}
]
}
}
}
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
public class QueryForMultipleIndexes {
public static void main(String[] args) throws UnknownHostException {
// on startup
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
QueryBuilder firstQuery = new BoolQueryBuilder()
.must(QueryBuilders.termQuery("_index", "user"))
.must(QueryBuilders.termQuery("username", "Opster"));
QueryBuilder secondQuery = new BoolQueryBuilder()
.must(QueryBuilders.termQuery("_index", "tweet"))
.must(QueryBuilders.termQuery("data", "some text"));
//This is the should clause which in turn contains two must clause
QueryBuilder mainQuery = new BoolQueryBuilder()
.minimumShouldMatch(1)
.should(firstQuery).should(secondQuery);
SearchResponse response = client.prepareSearch("*")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(mainQuery)
.setFrom(0).setSize(60)
.get();
System.out.println(response.getHits().getTotalHits());
// on shutdown
client.close();
}
}
Below is what should appear in the output/console:
2 hits
Let me know if this helps!
Upvotes: 4