简体   繁体   中英

With Elasticsearch .NET and NEST 6.x : How to MultiGet documents from multiples indices

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>();

I made this fluent NEST query:

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

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

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

it's generating the request below. We see the request use only the last index set which is "accounts" (and this is not what I need) :

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
    }
  ]
}

In fact I would like to create in fluent NEST the following (valid) Elasticsearch query request (with no specific index) :

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"
    }
  ]
}

Is there a way, in fluent NEST, to specify each index/type for each list of ids ?

And of course if I'm adding specific ids to ids["Prod"] and ids["Account"] this will be generated properly for those... for instance:

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"
    }
  ]
}

Each GetMany<T>(...) takes a second argument that is a delegate to further describe the call, including supplying an index name

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")
    )
);

which results in a request like

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"
    }
  ]
}

The "_type" names have been inferred from the CLR POCO names by lowercasing them, this can be changed by overriding the type name inferrer on Connection Settings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM