用于检索某些字段的 ElasticSearch NEST 库
ElasticSearch NEST library to retrieve certain fields
正在努力实现以下目标,
- 检索与给定 ID 和类型匹配的文章
- 检索匹配记录的选定字段
我已经用 Sense(chrome 插件)试过了,
POST /d3acampaign/article/_search
{
"fields": ["title","genre"] ,
"query": {
"filtered": {
"filter": {
"bool": {
"must": [{
"term": {
"id": "6"
}
},
{
"term": {
"genre": "metal"
}
}]
}
}
}
}
}
对于 C# 代码,我正在尝试使用以下结构构建查询,
FilterContainer fc = null;
TermFilter title = new TermFilter()
{
Field = "id",
Value = "6",
};
TermFilter genre = new TermFilter()
{
Field = "genre",
Value = "metal",
};
fc = title & genre;
QueryContainer qc = new FilteredQuery() { Filter = fc };
var searchRequest = new SearchRequest
{
SearchType = Elasticsearch.Net.SearchType.QueryAndFetch,
Query = qc,
Indices = new IndexNameMarker[] {"journal"},
Types = new TypeNameMarker[] { "article" },
};
var r = client.SearchAsync<Article>(searchRequest);
var l = (List<Article>) r.Result.Documents;
我能够 运行 此查询并获得匹配的记录,但我不确定如何指定要检索的选定字段。让我知道可以在 C# 代码中更改哪些内容以指定必要的字段。
提前致谢。
根据this回答你可以修改你的请求对象如下:
var searchRequest = new SearchRequest
{
...
Fields = new List<PropertyPathMarker>
{
Property.Path<Article>(p => p.YourField)
}
};
或者如果您决定使用 source filtering:
var searchRequest = new SearchRequest
{
...
Source = new SourceFilter
{
Include = new []
{
Property.Path<Article>(p => p.YourField)
}
}
};
希望对您有所帮助。
正在努力实现以下目标,
- 检索与给定 ID 和类型匹配的文章
- 检索匹配记录的选定字段
我已经用 Sense(chrome 插件)试过了,
POST /d3acampaign/article/_search
{
"fields": ["title","genre"] ,
"query": {
"filtered": {
"filter": {
"bool": {
"must": [{
"term": {
"id": "6"
}
},
{
"term": {
"genre": "metal"
}
}]
}
}
}
}
}
对于 C# 代码,我正在尝试使用以下结构构建查询,
FilterContainer fc = null;
TermFilter title = new TermFilter()
{
Field = "id",
Value = "6",
};
TermFilter genre = new TermFilter()
{
Field = "genre",
Value = "metal",
};
fc = title & genre;
QueryContainer qc = new FilteredQuery() { Filter = fc };
var searchRequest = new SearchRequest
{
SearchType = Elasticsearch.Net.SearchType.QueryAndFetch,
Query = qc,
Indices = new IndexNameMarker[] {"journal"},
Types = new TypeNameMarker[] { "article" },
};
var r = client.SearchAsync<Article>(searchRequest);
var l = (List<Article>) r.Result.Documents;
我能够 运行 此查询并获得匹配的记录,但我不确定如何指定要检索的选定字段。让我知道可以在 C# 代码中更改哪些内容以指定必要的字段。
提前致谢。
根据this回答你可以修改你的请求对象如下:
var searchRequest = new SearchRequest
{
...
Fields = new List<PropertyPathMarker>
{
Property.Path<Article>(p => p.YourField)
}
};
或者如果您决定使用 source filtering:
var searchRequest = new SearchRequest
{
...
Source = new SourceFilter
{
Include = new []
{
Property.Path<Article>(p => p.YourField)
}
}
};
希望对您有所帮助。