使用Elasticsearch .NET和NEST 6.x:如何从多重索引中多获取文档

时间:2018-06-25 14:46:44

标签: c# elasticsearch nest

var ids = new Dictionary<string, List<string>>();
ids["Topic"] = new List<string> {"KL7KJ2QBWD77yvpxyjvd", "374wJ2QBWD77yvpxDjpX", "B7499GMBWD77yvpxFzgW"};
ids["Prod"] = new List<string>();
ids["Account"] = new List<string>();

我做了这个流利的NEST查询:

var results = client.MultiGet(m => 
m.Index("topics").GetMany<Topic>(ids["Topic"])

.Index("prods").GetMany<Prod>(ids["Prod"])

.Index("accounts").GetMany<Account>(ids["Account"])

它在下面生成请求。我们看到请求仅使用了最后一个索引集,即“帐户”(这不是我所需要的):

http://localhost:9200/accounts/_mget?pretty=true
{
  "docs": [
    {
      "_type": "Topic",
      "_id": "KL7KJ2QBWD77yvpxyjvd"
    },
    {
      "_type": "Topic",
      "_id": "374wJ2QBWD77yvpxDjpX"
    },
    {
      "_type": "Topic",
      "_id": "B7499GMBWD77yvpxFzgW"
    }
  ]
}
# Response:
{
  "docs" : [
    {
      "_index" : "accounts",
      "_type" : "Topic",
      "_id" : "KL7KJ2QBWD77yvpxyjvd",
      "found" : false
    },
    {
      "_index" : "accounts",
      "_type" : "Topic",
      "_id" : "374wJ2QBWD77yvpxDjpX",
      "found" : false
    },
    {
      "_index" : "accounts",
          "_type" : "Topic",
      "_id" : "B7499GMBWD77yvpxFzgW",
      "found" : false
    }
  ]
}

实际上,我想流畅地创建以下(有效)Elasticsearch查询请求(无特定索引):

http://localhost:9200/_mget?pretty=true
{
  "docs": [
    {
      "_index": "topics",
      "_type": "Topic",
      "_id": "KL7KJ2QBWD77yvpxyjvd"
    },
    {
      "_index": "topics",
      "_type": "Topic",
      "_id": "374wJ2QBWD77yvpxDjpX"
    },
    {
      "_index": "topics",
      "_type": "Topic",
      "_id": "B7499GMBWD77yvpxFzgW"
    }
  ]
}

在流畅的NEST中,是否可以为每个ID列表指定每个索引/类型?

当然,如果我要在ids [“ Prod”]和ids [“ Account”]中添加特定的id,则会为这些对象正确生成...例如:

http://localhost:9200/_mget?pretty=true
{
  "docs": [
    {
      "_index": "topics",
      "_type": "Topic",
      "_id": "KL7KJ2QBWD77yvpxyjvd"
    },
    {
      "_index": "prods",
      "_type": "Prod",
      "_id": "xxxxx"
    },
    {
      "_index": "accounts",
      "_type": "Account",
      "_id": "yyyyy"
    }
  ]
}

1 个答案:

答案 0 :(得分:0)

每个yarn global add @angular/cli yarn add @angular/cli ng update @angular/cli 都使用第二个参数作为委托,以进一步描述调用,包括提供索引名称

GetMany<T>(...)

这会导致类似的请求

var ids = new Dictionary<string, List<string>>
{
    { "Topic", new List<string> { "topic1", "topic2", "topic3" } },
    { "Prod", new List<string> { "prod1", "prod2", "prod3" } },
    { "Account", new List<string> { "account1", "account2", "account3" } }
};

var multiGetResponse = client.MultiGet(m => m
    .GetMany<Topic>(ids["Topic"], (op, id) => op
        .Index("topics")
    )
    .GetMany<Prod>(ids["Prod"], (op, id) => op
        .Index("prods")
    )
    .GetMany<Account>(ids["Account"], (op, id) => op
        .Index("accounts")
    )
);

POST http://localhost:9200/_mget { "docs": [ { "_index": "topics", "_type": "topic", "_id": "topic1" }, { "_index": "topics", "_type": "topic", "_id": "topic2" }, { "_index": "topics", "_type": "topic", "_id": "topic3" }, { "_index": "prods", "_type": "prod", "_id": "prod1" }, { "_index": "prods", "_type": "prod", "_id": "prod2" }, { "_index": "prods", "_type": "prod", "_id": "prod3" }, { "_index": "accounts", "_type": "account", "_id": "account1" }, { "_index": "accounts", "_type": "account", "_id": "account2" }, { "_index": "accounts", "_type": "account", "_id": "account3" } ] } 名称是通过CLR POCO名称的小写来推断的,可以通过enter image description here进行更改