Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
773 views
in Technique[技术] by (71.8m points)

elasticsearch - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...