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



在NEST2.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);

我的索引中有多个类型,我想查询其中一个类型中的数据。(就像查询数据库中的一个表数据一样),我希望在SearchRequest对象初始值设定项中有一个"Type"参数。

您可以在SearchRequest<T>() 的构造函数中指定索引和类型

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

相关内容

  • 没有找到相关文章

最新更新