问题描述
我在C#项目中与NEST结合使用ElasticSearch。我的用例包括我迄今单独查询的不同文档类型的几个索引。现在我想实现一个全局搜索功能,可以查询所有现有的索引,文档类型,并正确分数结果。所以我的问题:如何通过使用NEST来实现?
目前我正在使用的功能 SetDefaultIndex
但是如何定义多个索引?
也许为了更好的理解,这是我想要实现的查询与NEST:
{
查询:{
indices:{
index:[
INDEX_A,
INDEX_B
],
query:{
term:{
FIELD :VALUE
}
},
no_match_query:{
term:{
FIELD:VALUE
}
}
}
}
}
TIA
您可以明确告诉NEST使用多个索引:
client.Search< MyObject>(s => s
.Indices(new [] {Index_A,Index_B})
...
)
如果要搜索所有索引
client.Search< MyObject>(s => s
.AllIndices()
...
)
或者如果要搜索一个索引(不是默认索引)
client.Search< MyObject>(s =>
.Index(Index_A)
...
)
记住,从elasticsearch 19.8还可以在索引名称上指定通配符
client.Search< MyObject> ;(s => s
.Index(Index_ *)
...
)
至于您的
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))
)
)
);
更新
这些测试显示如何使C#的协方差工作适合你:
在您的情况下,如果所有类型不是共享库的子类,您仍然可以使用object
ie:
.Search< object> => s
.Types(typeof(Product),typeof(Category),typeof(Manufacturer))
.Query(...)
);
这将搜索 / yourdefaultindex / products,categories,manufacturer / _search
并设置默认的 ConcreteTypeSelector
,了解每个返回的文档的类型。
使用 ConcreteTypeSelector(Func< dynamic,Hit< dynamic>,Type>)
您可以根据某些json值(动态)或命中元数据手动返回类型。
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
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:
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.
这篇关于NEST:如何查询多个索引并处理不同的子类(文档类型)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!