【问题标题】:How can i search multiple indices with ElasticSeach (C#/NEST)?如何使用 ElasticSearch (C#/NEST) 搜索多个索引?
【发布时间】:2018-01-12 04:39:42
【问题描述】:

我创建了这个函数:

            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 以针对特定索引”

但是我该怎么做呢?

【问题讨论】:

  • 您需要NoMatchQuery 吗?因为弃用来自 Elasticsearch 5 本身,所以弃用 index query

标签: c# elasticsearch nest


【解决方案1】:

indices query is deprecated 所以它目前仍然可以使用,但弃用是警告它可能会在未来的主要版本中被删除。

您可以实现与索引查询相同的功能

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"
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
}

【讨论】:

    猜你喜欢
    • 2021-08-12
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-18
    • 2021-07-29
    相关资源
    最近更新 更多