Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NEST: How to query against multiple indices and handle different subclasses (document types)?

I’m playing around with ElasticSearch in combination with NEST in my C# project. My use case includes several indices with different document types which I query separately so far. Now I wanna implement a global search function which queries against all existing indices, document types and score the result properly.

So my question: How do I accomplish that by using NEST?

Currently I’m using the function SetDefaultIndex but how can I define multiple indices?

Maybe for a better understanding, this is the query I wanna realize with NEST:

{
  "query": {
    "indices": {
      "indices": [
        "INDEX_A",
        "INDEX_B"
      ],
      "query": {
        "term": {
          "FIELD": "VALUE"
        }
      },
      "no_match_query": {
        "term": {
          "FIELD": "VALUE"
        }
      }
    }
  }
}

TIA

like image 781
Mani Avatar asked Apr 26 '13 11:04

Mani


1 Answers

You can explicitly tell NEST to use multiple indices:

client.Search<MyObject>(s=>s
    .Indices(new [] {"Index_A", "Index_B"})
    ...
)

If you want to search across all indices

client.Search<MyObject>(s=>s
    .AllIndices()
    ...
)

Or if you want to search one index (thats not the default index)

client.Search<MyObject>(s=>s.
    .Index("Index_A")
    ...
)

Remember since elasticsearch 19.8 you can also specify wildcards on index names

client.Search<MyObject>(s=>s
    .Index("Index_*")
    ...
)

As for your indices_query

client.Search<MyObject>(s=>s
    .AllIndices()
    .Query(q=>q
        .Indices(i=>i
            .Indices(new [] { "INDEX_A", "INDEX_B"})
            .Query(iq=>iq.Term("FIELD","VALUE"))
            .NoMatchQuery(iq=>iq.Term("FIELD", "VALUE"))
        )
    )
);

UPDATE

These tests show off how you can make C#'s covariance work for you:

https://github.com/Mpdreamz/NEST/blob/master/src/Nest.Tests.Integration/Search/SubClassSupport/SubClassSupportTests.cs

In your case if all the types are not subclasses of a shared base you can still use 'object'

i.e:

 .Search<object>(s=>s
      .Types(typeof(Product),typeof(Category),typeof(Manufacturer))
      .Query(...)
 );

This will search on /yourdefaultindex/products,categories,manufacturers/_search and setup a default ConcreteTypeSelector that understands what type each returned document is.

Using ConcreteTypeSelector(Func<dynamic, Hit<dynamic>, Type>) you can manually return a type based on some json value (on dynamic) or on the hit metadata.

like image 169
Martijn Laarman Avatar answered Dec 08 '22 01:12

Martijn Laarman