我在用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"
}
}
}
}
}
现在,我必须编写一个查询,该查询将搜索用户以及用户的文件。
可以说,如果我用查询号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"
}
}
]
}
}
}
}
}
由于该files
字段是一个nested
字段,因此您还需要利用nested
查询。像这样:
var searchResult = elasticClient.Search<InquiryInformation>(s => s
.Source(false)
.Query(q => q
.Nested(c => c
.InnerHits()
.Path(p => p.Files)
.Query(nq => nq
.MultiMatch(m => m
.Fields(f => f
.Field("files.fileContent")
.Field("files.fileName")
)
.Query("Hello Stackoverflow")
)
)
)
)
);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句