gpt4 book ai didi

elasticsearch - 如何使用嵌套映射对多个索引执行ElasticSearch查询

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

我有两个索引,以下配置带有映射

var settings = new ConnectionSettings(new Uri("http://localhost:9200/"));
settings
.DefaultMappingFor<ManagementIndex>(m => m
.IndexName("management")
)
.DefaultMappingFor<PropertyIndex>(m => m
.IndexName("apartmentproperty")
);
var client = new ElasticClient(settings);



1)属性映射
client.Indices.Create("property", i => i
.Settings(s => s
.NumberOfShards(2)
.NumberOfReplicas(0)
)
.Map<PropertyIndex>(map => map
.AutoMap()
.Properties(p => p
.Nested<PropertyData>(n => n
.Name(c => c.property)
.AutoMap()
.Properties(pp => pp
.Text(c => c
.Name(np => np.city)
.Analyzer("standard")
)
.Text(c => c
.Name(np => np.market)
.Fields(ff => ff
.Text(tt => tt
.Name(np => np.market)
.Analyzer("standard")
)
.Keyword(k => k
.Name("keyword")
.IgnoreAbove(256)
)
)
).Text(c => c
.Name(np => np.name)
.Analyzer("standard")
)

)
)
)
)
);



2)所有者
if (client.Indices.Exists("owner").Exists)
client.Indices.Delete("owner");

client.Indices.Create("owner", i => i
.Settings(s => s
.NumberOfShards(2)
.NumberOfReplicas(0)
)
.Map<OwnerIndex>(map => map
.AutoMap()
.Properties(p => p
.Nested<OwnerProp>(n => n
.Name(c => c.owner)
.AutoMap()
.Properties(pp => pp
.Text(c => c
.Name(np => np.market)
.Fields(ff => ff
.Text(tt => tt
.Name(np => np.market)
.Analyzer("standard")
)
.Keyword(k => k
.Name("keyword")
.IgnoreAbove(256)
)
)
).Text(c => c
.Name(np => np.name)
.Analyzer("standard")
)
)
)
)
)
);

具有以下POCO定义
    public class PropertyData
{
public string name { get; set; }
public string city { get; set; }
public string market { get; set; }
}

public class PropertyIndex
{
public PropertyData property { get; set; }
}

public class OwnerProp
{
public string name { get; set; }
public string market { get; set; }
}

public class OwnerIndex
{
public OwnerProp owner { get; set; }
}



试图像这样通过两个索引进行搜索
 public async Task<object> SearchPropertiesAsync(string searchQuery, List<string> description, int limit = 25, int skip = 1)
{
var propertyfilters = new List<Func<QueryContainerDescriptor<object>, QueryContainer>>();
var ownerFilters = new List<Func<QueryContainerDescriptor<object>, QueryContainer>>();
if (description.Any())
{
propertyfilters.Add(fq => fq.Terms(t => t.Field("property.market.keyword").Terms(description)));
ownerFilters.Add(fq => fq.Terms(t => t.Field("owner.market.keyword").Terms(description)));
}

var searchResponse = await _elasticClient.SearchAsync<object>(s => s
.Index(Indices.Index(typeof(PropertyIndex)).And(typeof(OwnerIndex)))

.Query(q => (q
.Nested(n => n

.Path(Infer.Field<PropertyIndex>(ff => ff.property))

.Query(nq => nq

.MultiMatch(m => m

.Fields(f => f
.Field(Infer.Field<PropertyIndex>(ff => ff.property.city))
.Field(Infer.Field<PropertyIndex>(ff => ff.property.market))
.Field(Infer.Field<PropertyIndex>(ff => ff.property.name))
)
.Operator(Operator.Or)
.Query(searchQuery)
.Fuzziness(Fuzziness.Auto)
) && +q.Bool(bq => bq.Filter(propertyfilters))
))
) || (q
.Nested(n => n
.Path(Infer.Field<OwnerIndex>(ff => ff.mgmt))
.Query(nq => nq

.MultiMatch(m => m
.Fields(f => f
.Field(Infer.Field<OwnerIndex>(ff => ff.owner.market))
.Field(Infer.Field<OwnerIndex>(ff => ff.owner.name))
)
.Operator(Operator.Or)
.Query(searchQuery)
.Fuzziness(Fuzziness.Auto)
)
&& +q.Bool(bq => bq.Filter(ownerFilters))
))
)
).From((skip - 1) * limit)
.Size(limit)
);

return searchResponse.Documents;
}

调用 SearchPropertiesAsync方法将返回此错误消息(为简洁起见被截断)
         ....
"index": "owner",
"caused_by": {
"type": "illegal_state_exception",
"reason": "[nested] failed to find nested object under path [property]"
}

....
"index": "property",
"caused_by": {
"type": "illegal_state_exception",
"reason": "[nested] failed to find nested object under path [owner]"
}

请注意,它似乎试图对 owner.索引执行 property的嵌套搜索,并尝试对不存在的 property.索引执行 owner的嵌套搜索。

我觉得这应该是一个非常琐碎的问题,但是我已经使用ElasticSearch仅仅4天了,但仍然非常新。

是我做错了什么,还是我想念了什么?搜索了整个互联网,甚至得出我目前有的解决方案。

请注意,当您一次仅执行一个嵌套查询时,代码可以正常工作,但是尝试对多个索引执行是我的问题所在。任何帮助将不胜感激。

我正在使用ElasticSearch 7.3.2和Nest Client 7.3.0。

我不介意降级为可用的较低版本。

最佳答案

显然,根据文档

ignore_unmapped (Optional, boolean) Indicates whether to ignore an unmapped path and not return any documents instead of an error. Defaults to false.

If false, Elasticsearch returns an error if the path is an unmapped field.

You can use this parameter to query multiple indices that may not contain the field path.



因此,在查询主体上为每个嵌套查询链接 .IgnoreUnmapped(true)即可解决此问题。
万一别人遇到同样的问题

关于elasticsearch - 如何使用嵌套映射对多个索引执行ElasticSearch查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58035140/

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