[英]How can i search multiple indices with ElasticSeach (C#/NEST)?
我創建了這個功能:
public static ISearchResponse<Immo> searchImmoByCustomerField(Nest.ElasticClient esClient, string esIndex, string esIndex2, int from, int take, string qField, string nmqField, string qTerm, string nmqTerm)
{
var result = esClient.Search<Immo>(s => s
.AllIndices()
.Query(q => q
.Indices(i => i
.Indices(new[] { esIndex, esIndex2 })
.Query(iq => iq.Term(qField, qTerm))
.NoMatchQuery(iq => iq.Term(nmqField, nmqTerm))
)
)
);
return result;
}
該函數在2個索引中查找搜索詞。 Visual Studio向我顯示以下消息:
“已棄用。您可以在查詢中指定_index以定位特定索引”
但是我該怎么辦呢?
不建議使用indexs查詢,因此它目前仍可使用,但棄用是一個警告,它可能會在將來的主要版本中刪除。
您可以通過以下方式實現與索引查詢相同的功能:
var esIndex = "index-1";
var esIndex2 = "index-2";
var qField ="query-field";
var qTerm = "query-term";
var nmqField = "no-match-query-field";
var nmqTerm = "no-match-query-term";
client.Search<Immo>(s => s
.AllIndices()
.Query(q => (q
.Term(t => t
.Field(qField)
.Value(qTerm)
) && +q
.Terms(t => t
.Field("_index")
.Terms(new[] { esIndex, esIndex2 })
)) || (q
.Term(t => t
.Field(nmqField)
.Value(nmqTerm)
) && !q
.Terms(t => t
.Field("_index")
.Terms(new[] { esIndex, esIndex2 })
))
)
);
產生以下查詢JSON
POST http://localhost:9200/_all/immo/_search
{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"term": {
"query-field": {
"value": "query-term"
}
}
}
],
"filter": [
{
"terms": {
"_index": [
"index-1",
"index-2"
]
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"no-match-query-field": {
"value": "no-match-query-term"
}
}
}
],
"must_not": [
{
"terms": {
"_index": [
"index-1",
"index-2"
]
}
}
]
}
}
]
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.