如何使用ElasticSeach(C#/NEST)搜索多个索引? [英] How can i search multiple indices with ElasticSeach (C#/NEST)?

查看:180
本文介绍了如何使用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向我显示消息:

The function looking in 2 Indices for a search term. Visual Studio show me the message:

已弃用.您可以在查询中指定_index以定位特定索引"

"Deprecated. You can specify _index on the query to target specific indices"

但是我该怎么做?

推荐答案

已弃用索引查询,因此它仍将在当前运行,但是弃用是一个警告,它可能会在将来的主要版本中删除.

The indices query is deprecated so it will still work currently, but the deprecation serves as a warning that it'll likely be removed in a future major version.

您可以通过以下方式实现与索引查询相同的功能

You can achieve the same functionality as the indices query with

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

which produces the following query 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"
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
}

这篇关于如何使用ElasticSeach(C#/NEST)搜索多个索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆