19

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

Mani
  • 783
  • 1
  • 9
  • 19

1 Answers1

22

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.

gsk
  • 1,233
  • 15
  • 32
Martijn Laarman
  • 13,476
  • 44
  • 63
  • Hello Martijn, thanks for your reply! The way to query against different indices I already figured out. The main problem I currently face is that I have **different document types with different structure** stored in die indices like Books, Cars, etc. I mean your example is: client.Search MyObject would be in my case something more generic because I wanna retrieve Books, Cars, etc with one query. I tried with generic type but without success. – Mani Apr 30 '13 at 09:37
  • The only way I got documents back was by using client.Search(query). I also added MapTypeIndices to the connection settings (…). Could you please give an example to accomplish something like `var results = Client.Search????????????????>(s => s .Indices(new[] { “Book-index", "Car-index" }) .Size(200) .MatchAll() );` Or do you recommend another approach? Highly appreciate your effort! – Mani Apr 30 '13 at 09:37
  • Update: Currently I'm using the base class of Books, Cars etc but it isn't a clean solution. `client.Search` – Mani Apr 30 '13 at 11:53
  • @Neil NEST should have you covered here too, see my updated answer. – Martijn Laarman Apr 30 '13 at 17:49
  • @Martjin, thank you very much - works like a charme! I've updated the question accordingly. – Mani May 02 '13 at 09:17
  • @Martjin This example doesn't work for me. Has this .Types interface gone away? – zorlack Mar 11 '16 at 19:56
  • its now `.Type` see also: https://github.com/elastic/elasticsearch-net/blob/master/docs/asciidoc/ClientConcepts/HighLevel/CovariantHits/CovariantSearchResults.doc.asciidoc – Martijn Laarman Mar 14 '16 at 13:52
  • dude the github link do not work anymore , can you provide a new version of it, – Abdessamad Jadid Mar 03 '21 at 09:04