gpt4 book ai didi

c# - 如何在ElasticSearch中仅选择子对象的匹配字段?

转载 作者:行者123 更新时间:2023-12-02 23:13:34 25 4
gpt4 key购买 nike

我在用Elasticsearch搜索时遇到问题。我正在使用ES 7.3进行实现。

下面是我的类(class)结构:

    [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 Adding Sample Query of 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")
)
)
);

UPDATE 2 : Adding another query:


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")
)
)
)
)
);

关于c# - 如何在ElasticSearch中仅选择子对象的匹配字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57805376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com