问题描述
文档似乎表明我可以返回一个字段的子集而不是整个文档。这是我的代码:
the documentation seems to indicate i can return a subset of fields instead of the entire document. here's my code:
var result = client.Search<MyObject>(s => s
.Fields(f => f.Title)
.Query(q => q
.QueryString(qs => qs
.OnField("title")
.Query("the"))));
我正在'标题'字段上搜索''',想要刚刚返回'标题'。我的结果。文档对象包含10个对象,每个对象都是空的。
i'm searching on the word 'the' on the 'title' field and wanting to just return 'title'. my result.Documents object contains 10 objects that are each null.
我看到我想要的值,但它在搜索响应中很深:
result.Hits [0] .Fields.FieldValues [0] ...
i do see the values i want but it's deep in the search response:result.Hits[0].Fields.FieldValues[0]...
有更好的方法来获取返回的'title'字段列表吗? strong>
is there a better way to get at the list of 'title' fields returned?
我的数据映射(截断)是这个...
my mapping for the data (truncated) is this ...
{
"myidex": {
"mappings": {
"myobject": {
"properties": {
"title": {
"type": "string"
},
"artists": {
"properties": {
"id": {
"type": "string",
"index": "not_analyzed",
"analyzer": "fullTerm"
},
"name": {
"type": "string",
"index": "not_analyzed",
"analyzer": "fullTerm"
}
}
}
}
}
}
}
}
我的类对象如下所示:
[Table("MyTable")]
[Serializable]
[ElasticType(Name="myobject")]
public class MyObject
{
[ElasticProperty]
public string Title { get; set; }
[JsonIgnore]
public string Artistslist { get; set; }
[ElasticProperty(Analyzer = "caseInsensitive")]
public List<Person> Artists { get; set; }
}
[Serializable]
public class Person
{
[ElasticProperty(Analyzer = "fullTerm", Index = FieldIndexOption.not_analyzed)]
public string Name { get; set; }
[ElasticProperty(Analyzer = "fullTerm", Index = FieldIndexOption.not_analyzed)]
public string Id { get; set; }
}
艺术家列表来自我的数据源(sql),然后我将其解析成在索引数据之前,一个新的List对象。
Artistslist comes from my data source (sql) then i parse it out into a new List object before indexing the data.
推荐答案
我认为这个深层嵌套的价值是对Elasticsearch 1.0的改变以及如何部分字段现在作为数组返回(请参阅)。这在文档中得到解决。在 Fields()vs SourceIncludes()部分。它显示了使用 FieldValue
助手方法来访问这些值的示例。基于此,请尝试以下操作:
I think this deeply nested value is do to a change in Elasticsearch 1.0 and how partial fields are now returned as arrays (See 1.0 Breaking Changes - Return Values for details.). This is addressed in the NEST 1.0 Breaking Changes documentation; in the Fields() vs SourceIncludes() section. It shows an example of using a FieldValue
helper method to gain access to these values. Based on that, try the following:
对于所有项目:
foreach (var hit in result.Hits)
{
var title = hit.Fields.FieldValue<MyObject, string>(f => f.Title);
}
对于特定项目:
var title = result.Hits.ElementAt(0)
.Fields.FieldValue<MyObject, string>(f => f.Title);
我知道它仍然有点冗长,但它应该适用于您,并将处理新的数组返回Elasticsearch 1.0的格式化。
I know it is still a bit verbose but it should work for you and will handle the new array return formatting of Elasticsearch 1.0.
这篇关于加载Elasticsearch Nest查询中的特定字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!