ElasticSearch 多索引查询
ElasticSearch Multi Index Query
简单问题:我的 elasticsearch 引擎中有多个索引由 postgresql 使用 logstash 镜像。 ElasticSearch 对于模糊搜索表现良好,但现在我需要在索引中使用引用,需要由查询处理。
Index A:
{
name: "alice",
_id: 5
}
...
Index B:
{
name: "bob",
_id: 3,
best_friend: 5
}
...
如何查询:
获取字段名称以 "b" 开头的索引 B 和 "best_friend" 引用的名称以 "a"[=20= 开头的索引 A 的所有匹配项]
这甚至可以用 elasticsearch 实现吗?
是的,这是可能的:POST A,B/_search
将查询多个索引。
为了匹配来自特定索引的记录,您可以使用meta-data field _index
下面是一个查询,获取字段名称以 "b" 开头的索引 B 和名称以 "a" 开头的索引 A 的所有匹配项,但是不像您通常在关系 SQL 数据库中那样匹配 reference。 Elastic 中的外键引用匹配(连接)和每个 NoSQL 是你的责任 AFAIK。请参阅 Elastic Definitive Guide 以找到满足您需求的最佳方法。最后,NoSQL 不是 SQL,改变主意。
POST A,B/_search
{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"prefix": {
"name": "a"
}
},
{
"term": {
"_index": "A"
}
}
]
}
},
{
"bool": {
"must": [
{
"prefix": {
"name": "b"
}
},
{
"term": {
"_index": "B"
}
}
]
}
}
]
}
}
}
简单问题:我的 elasticsearch 引擎中有多个索引由 postgresql 使用 logstash 镜像。 ElasticSearch 对于模糊搜索表现良好,但现在我需要在索引中使用引用,需要由查询处理。
Index A:
{
name: "alice",
_id: 5
}
...
Index B:
{
name: "bob",
_id: 3,
best_friend: 5
}
...
如何查询:
获取字段名称以 "b" 开头的索引 B 和 "best_friend" 引用的名称以 "a"[=20= 开头的索引 A 的所有匹配项]
这甚至可以用 elasticsearch 实现吗?
是的,这是可能的:POST A,B/_search
将查询多个索引。
为了匹配来自特定索引的记录,您可以使用meta-data field _index
下面是一个查询,获取字段名称以 "b" 开头的索引 B 和名称以 "a" 开头的索引 A 的所有匹配项,但是不像您通常在关系 SQL 数据库中那样匹配 reference。 Elastic 中的外键引用匹配(连接)和每个 NoSQL 是你的责任 AFAIK。请参阅 Elastic Definitive Guide 以找到满足您需求的最佳方法。最后,NoSQL 不是 SQL,改变主意。
POST A,B/_search
{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"prefix": {
"name": "a"
}
},
{
"term": {
"_index": "A"
}
}
]
}
},
{
"bool": {
"must": [
{
"prefix": {
"name": "b"
}
},
{
"term": {
"_index": "B"
}
}
]
}
}
]
}
}
}