Komal Kartan
Komal Kartan

Reputation: 35

Searching in multiple elasticsearch indexes using NEST

I am trying to search for a text in elasticsearch using nest 7.10.1. I want to search in two different indexes, I get a response in the form of documents, but I cannot access its properties because the result has the combination of two indexes. Below is the code I tried. Both the indexes has same properties. What do I use in the foreach loop to access the key and values of the result documents.

public void searchIndices(string query) {
    var response = client.Search<object>(
      s => s.Index("knowledgearticles_index, index2")
            .Query(q => q.Match(m => m.Field("locationName")
                         .Query(query))));

    Console.WriteLine(response.Documents);
    
    foreach(object r in response.Documents) {
      
    }
}

I am using elasticsearch 7.10.2

Upvotes: 2

Views: 1733

Answers (2)

Russ Cam
Russ Cam

Reputation: 125488

If the two indices that you're searching over contain different JSON structures, then the T in Search<T> will need to be a type that the differing JSON structures can be deserialize to. object will work as per the example in your question, but then there is no typed access for any of the properties.

A simple approach to overcoming this is to hook up the JsonNetSerializer and then use JObject for T

var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var connectionSettings =
    new ConnectionSettings(pool, sourceSerializer: JsonNetSerializer.Default);
var client = new ElasticClient(connectionSettings);

var response = client.Search<JObject>(s => s
    .Index("knowledgearticles_index, index2")
    .Query(q => q
        .Match(m => m
            .Field("locationName")
            .Query(query)
        )
     )
);

We now have a way of accessing properties on JObject, but will still need to handle the type of each property.

Upvotes: 1

Joe - Check out my books
Joe - Check out my books

Reputation: 16905

Each raw hit coming back in the search response has the _index meta field associated with it:

"hits" : {
  "total" : {
    "value" : 91,
    "relation" : "eq"
  },
  "hits" : [
    {
      "_index" : "knowledgearticles_index",   <---
      "_type" : "_doc",
      "_id" : "r_oLl3cBZOT6A8Qby8Qd",
      "_score" : 1.0,
      "_source" : {
        ...
      }
    }

Now, in NEST,

.Documents is a convenient shorthand for retrieving the _source for each hit

-- meaning that you'll have lost access to the meta properties.

So the trick is to use a loop like this instead:

foreach (var hit in response.HitsMetadata.Hits) {
  Console.WriteLine(hit);
}

Upvotes: 1

Related Questions