如何使用nest elasticsearch查询一种类型的数据限制

how to query data limit in one type using nest elasticsearch

在 NEST 2.x 中,我编写了如下代码来查询数据:

var query = new QueryContainer();
query = query && new TermQuery { Field = "catId", Value = catId };
query = query && new NumericRangeQuery { Field ="price", GreaterThan = 10 };

var request =new SearchRequest<Project>
{
    From = 0,
    Size = 100,
    Query = query,
    Sort = new List<ISort>
            {
                new SortField { Field = "field", Order = SortOrder.Descending },
                ...
            },
    Type?? //problem comes here, how to specify type??
}

var response = _client.Search<Project>(request);

我的索引中有不止一种类型,我想查询其中一种类型的数据。(就像查询数据库中的 table 数据之一),我希望在 SearchRequest 对象初始化器中有一个 "Type" 参数。

您可以在 SearchRequest<T>()

的构造函数中指定 indices 和类型
var catId = 1;

var query = new QueryContainer(new TermQuery { Field = "catId", Value = catId });
query = query && new NumericRangeQuery { Field = "price", GreaterThan = 10 };

var request = new SearchRequest<Project>("index-name", Types.Type(typeof(Project), typeof(AnotherProject)))
{
    From = 0,
    Size = 100,
    Query = query,
    Sort = new List<ISort>
        {
            new SortField { Field = "field", Order = Nest.SortOrder.Descending },
        }
};

var response = client.Search<Project>(request);

将生成以下查询

POST http://localhost:9200/index-name/project%2Canotherproject/_search?pretty=true 
{
  "from": 0,
  "size": 100,
  "sort": [
    {
      "field": {
        "order": "desc"
      }
    }
  ],
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "catId": {
              "value": 1
            }
          }
        },
        {
          "range": {
            "price": {
              "gt": 10.0
            }
          }
        }
      ]
    }
  }
}