gpt4 book ai didi

c# - ElasticSearch在多个字段上完全匹配

转载 作者:行者123 更新时间:2023-12-03 00:44:50 24 4
gpt4 key购买 nike

我是Elastic Search的新手,我试图在Elasticsearch索引中的对象的每个字段上获得完全匹配。例如,我有两个对象:

        {
"_index": "sql",
"_type": "_doc",
"_id": "mpovsH",
"_score": 1.0,
"_source": {
"entityId": 1,
"user": "userfirst",
"descr": "testfirst",
}
},
{
"_index": "sql",
"_type": "_doc",
"_id": "mpovsH",
"_score": 1.0,
"_source": {
"entityId": 2,
"user": "usersecond",
"descr": "testsecond",
}
}
我想在对象的所有字段中搜索字符串“userfirst”,并仅获取第一个作为响应。我试过了:
                var searchResponse = client.SearchAsync<MyObject>(s => s
.Source(sf => sf)
.Query(q => q
.MultiMatch(a => a
.Query(queryValue)))).Result;
其中queryValue是“userfirst”,但我在结果中都得到了两个对象。我该如何更改?另外,如果可能的话,我不会写每个字段,因为我的对象要大得多。
编辑:我设法通过此查询仅获得一个结果:
                var searchResponse = client.SearchAsync<TendersElasticSearch>(s => s
.Source(sf => sf)
.Query(qn => qn
.MatchPhrasePrefix(ma => ma
.Field(x => x.User)
.Query(queryValue)))).Result;
但是通过此查询,我只能在现场用户上获得结果。我想搜索每个对象的所有字段。有小费吗?

最佳答案

添加带有索引数据,映射,搜索查询和搜索结果的工作示例
索引映射:

{
"mappings": {
"properties": {
"users": {
"type": "nested"
}
}
}
}
索引数据:
{
"users": [
{
"entityId": 1,
"user": "userfirst",
"descr": "testfirst"
},
{
"entityId": 2,
"user": "usersecond",
"descr": "testsecond"
}
]
}
搜索查询:
{
"query": {
"nested": {
"path": "users",
"query": {
"bool": {
"must": [
{ "match": { "users.user": "userfirst" }}
]
}
},
"inner_hits":{}
}
}
}
使用多重匹配的搜索查询:
{
"query": {
"nested": {
"path": "users",
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "userfirst",
"fields": [
"users.user"
]
}
}
]
}
},
"inner_hits": {}
}
}
}
搜索结果:
hits": [
{
"_index": "stof_64061575",
"_type": "_doc",
"_id": "1",
"_nested": {
"field": "users",
"offset": 0
},
"_score": 0.6931471,
"_source": {
"entityId": 1,
"user": "userfirst",
"descr": "testfirst"
}
}
]

关于c# - ElasticSearch在多个字段上完全匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64061575/

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