Niv*_*ved 8 c# elasticsearch nest
我正在尝试编写一个只返回其中一个字段的查询.现在我正在存储文件的filePath和文件的内容,在我的搜索中我想搜索内容,但只返回filePath.
我从这句话开始:
var searchResults = client.Search<File>(
s => s.Query(q => q.Wildcard(w => w.Value("*" + genre + "*").OnField("fileContents"))).AllIndices().AllTypes());
Run Code Online (Sandbox Code Playgroud)
返回结果searchResults.Documents并将.Fields添加到它:
var searchResults = client.Search<File>(
s => s.Query(q => q.Wildcard(w => w.Value("*" + genre + "*").OnField("fileContents"))).AllIndices().AllTypes().Fields(f=>f.filePath));
Run Code Online (Sandbox Code Playgroud)
并且它没有任何内容,searchResults.Documents但它显示正确使用的命中数searchResults.Hits.Total.
File类只是:
public class File
{
public string filePath { get; set; }
public string fileContents { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这会生成以下json请求:
{
"fields": [
"filePath"
],
"query": {
"wildcard": {
"fileContents": {
"value": "*int*"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在Sense中运行时返回结果,并在执行时searchResults.Hits.Total给出命中数.
但是,searchResults.DocumentIEnumerable中没有记录.
是否有一种不同的方式我应该返回一个特定的领域?
jhi*_*den 11
使用"source"字段指定要撤回的字段.以下是我的应用程序中仅返回一些字段的示例代码.
var searchResults = ElasticClient.Search<AuthForReporting>(s => s
.Size(gridSortData.PageSize)
.From(gridSortData.PageIndex * gridSortData.PageSize)
.Sort(sort)
.Source(sr => sr
.Include(fi => fi
.Add(f => f.AuthEventID)
.Add(f => f.AuthResult.AuthEventDate)
.Add(f => f.AuthInput.UID)
.Add(f => f.AuthResult.CodeID)
.Add(f => f.AuthResult.AuthenticationSuccessful)
.Add(f => f.AuthInput.UserName)
.Add(f => f.AuthResult.ProductID)
.Add(f => f.AuthResult.ProductName)
.Add(f => f.AuthInput.AuthType)
.Add(f => f.AuthResult.Address.City)
.Add(f => f.AuthResult.Address.State)
.Add(f => f.AuthResult.Address.CountryCode)
.Add(f => f.AuthResult.RulesFailed)
)
)
.Query(query)
);
Run Code Online (Sandbox Code Playgroud)
然后,您可以通过结果中的"source"访问字段:
var finalResult = from x in searchResults.Hits
select new AlertListRow
{
AlertCode = x.Source.AlertCode,
AlertDate = x.Source.AlertDate,
AlertID = x.Id,
AlertSummary = x.Source.Subject,
AlertMessage = x.Source.Body
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5569 次 |
| 最近记录: |