在Elasticsearch 7.7版中跨多个索引查询 [英] Query across multiple index in the Elasticsearch version 7.7
问题描述
在弹性搜索7.7版中,删除了索引中的多个_type, 现在,如果要跨多个索引进行查询,我们将按照以下方式进行操作.
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
在7.7中,使用Transport Java API从多个索引查询的最佳方法是什么?
In 7.7, what is the best way to query from multiple indexes using Transport Java API?
已编辑:
1)假设我有两个索引,"用户"和"推特",我想同时搜索索引-用户和推特,如下所示
Edited :
1) Say I have two indexes, "user" and "tweet" I want to search both the index - user and tweet like below
如果我想在字段中以{"username" =" Opster "}查询"用户"索引 并在字段的" tweet "索引中以{"data" =" some_text "}}
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"}
这可能吗?
2)我知道,每个索引在弹性搜索中都是一个单独的分区,但是跨索引的搜索在弹性搜索中如何内部工作?
2) I understand, each index is a separate partition in elastic search but How does the search across indexes work internally in elastic search?
谢谢,
哈里
推荐答案
I think the below code should help. Note that you can create TransportClient client
instance as mentioned in this link
为了使用 Java API ,下面的代码应该有帮助:
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();
以下一些有用的API链接:
Some of the below useful API links:
- 搜索API 我就是利用上面的代码
- QueryBuilders
- SearchType
- Search API from which I made use of the above code
- QueryBuilders
- SearchType
- paepareSearch method which returns SearchRequestBuilder instance
注意:ES建议人们按照本指南应该为您提供帮助,帮助您从Java API迁移到使用REST Client.
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.
假设我有两个索引
-
user
的字段username
的值为Opster
-
tweet
的字段data
的值为some text
user
having fieldusername
with valueOpster
tweet
having fielddata
with valuesome text
为简单起见,我将两个字段都设为keyword
For the sake of simplicity I have made both the fields keyword
type
您要查找的内容如下
POST /_search
{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"term": {
"_index": "user"
}
},
{
"term": {
"username": "Opster"
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"_index": "tweet"
}
},
{
"term": {
"data": "some text"
}
}
]
}
}
]
}
}
}
Java API:
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!
这篇关于在Elasticsearch 7.7版中跨多个索引查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!