【发布时间】:2019-09-05 12:21:18
【问题描述】:
我在使用 Elasticsearch 进行搜索时遇到问题。我正在使用 ES 7.3 来实现。
下面是我的班级结构:
[ElasticsearchType]
public class InquiryInformation
{
[Text(Name = "inquiryNumber")]
public string InquiryNumber { get; set; }
[Text(Name = "inquiryStatus")]
public string InquiryStatus { get; set; }
[Text(Name = "submitedBy")]
public string SubmittedBy { get; set; }
[Nested]
[PropertyName("files")]
public List<FileInformation> Files { get; set; }
}
[ElasticsearchType]
public class FileInformation
{
[Text(Name = "fileName")]
public string FileName { get; set; }
[Text(Name = "fileContent")]
public string FileContent { get; set; }
}
并使用此代码创建映射:
elasticClient.Map<InquiryInformation>(m => m.AutoMap());
这已经用 ES 创建了下面的索引映射:
{
"city" : {
"mappings" : {
"properties" : {
"files" : {
"type" : "nested",
"properties" : {
"fileContent" : {
"type" : "text"
},
"fileName" : {
"type" : "text"
}
}
},
"inquiryNumber" : {
"type" : "text"
},
"inquiryStatus" : {
"type" : "text"
},
"submitedBy" : {
"type" : "text"
}
}
}
}
}
现在,我必须编写一个查询来搜索用户以及用户的文件。
假设如果我搜索带有inquiryNumber XYZ1212 的用户,那么它将返回所有匹配的记录。现在的问题是 - 我只需要返回匹配的文件。就像如果 FileContent 的 FileName 包含匹配的记录信息,那么只有匹配的文件应该与文档一起返回。
我用 NEST 尝试了很多方法。但未能获得唯一匹配的文件。它总是向我返回特定用户的所有文件。
这能满足我的需求吗?
UPDATE 1 添加 C# (NEST) 的示例查询
var searchResult = elasticClient.Search<InquiryInformation>(s => s
.Query(q => q
.MultiMatch(m => m
.Fields(f => f
.Field("files.fileContent")
.Field("files.fileName")
)
.Query("Hello Stackoverflow")
)
)
);
更新 2:添加另一个查询:
POST city/_search
{
"_source": {
"includes": [ "*" ],
"excludes": [ "files" ]
},
"query": {
"nested": {
"path": "files",
"inner_hits": {
"_source": [
"inquiryNumber", "submitedBy"
]
},
"query": {
"bool": {
"must": [
{
"term": {
"files.fileContent": "Samsung"
}
}
]
}
}
}
}
}
【问题讨论】:
-
最好显示您迄今为止尝试过的一个或多个查询。
-
@Val 添加了查询!
-
@Val:我还在stackoverflow.com/a/31721025/903507 上检查了您的 cmets。但我没有运气。如果您能帮助我实现这一目标,那可能对我很有帮助。我是 ES 的初学者
标签: c# asp.net elasticsearch kibana nest