字典项的 ElasticSearch/NEST 索引策略 [英] ElasticSearch/NEST indexing policy for Dictionary items
问题描述
我有一个包含字段的文档模型,它是一个字典,可以取任何值,如下所示:
I have a document model containing field which is a dictionary and can take any value, like this:
public class Document
{
public string Id { get; set; }
public string Description { get; set; }
public Dictionary<string, object> Permissions { get; set; }
public Dictionary<string, object> Metadata { get; set; }
}
例如
client.IndexDocument(new Document
{
Id = "4",
Description = "ordinary document",
Metadata = new Dictionary<string, object>
{
{ "publish_date", new DateTime(1930, 10, 11) },
{ "author_country", "RU" },
{ "salary", 2590.00 },
{ "likes", 23 },
}
})
映射是自动完成的,因此默认分析器会更改字典的值,例如:将字符串更改为小写或删除破折号,以便此代码不会返回任何结果:
Mapping is done automatically so default analizers change values of dictionary, e.g.: change strings to lowercase or remove dashes so this code won't return any results:
var result = client.Search<Document>(s => s
.Query(q =>
q.Term(new Field("metadata.author_country"), "RU"))
);
如何为元数据的任何值禁用此行为?或者-设置自定义分析器?或者-仅对给定类型的值禁用它?
How can i disable this behaviour for any value of Metadata? Or- set custom analizer? Or- disable it only for values of given type?
推荐答案
您应该将文档字段的映射设置为正确的类型.在此处查看更多信息:https://www.elastic.co/guide/en/elasticsearch/reference/7.10/mapping.html所有类型都在这里列出:https://www.elastic.co/guide/en/elasticsearch/reference/7.10/mapping-types.html
You should set the mapping of the document fields to the correct kind. See more info here: https://www.elastic.co/guide/en/elasticsearch/reference/7.10/mapping.html And all the types are listed here: https://www.elastic.co/guide/en/elasticsearch/reference/7.10/mapping-types.html
要在 Nest 中执行此操作,您需要将正确的属性添加到文档定义中.
To do it in Nest you need to add the correct attribute to your document definition.
在此处查看详细信息:https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/attribute-mapping.html
如果我了解您要执行的操作,那么您需要将元数据类型设置为嵌套,因为您要搜索元数据:
If I understand what you are trying to do then you will need to set the Metadata type as nested because you want to search the Metadata:
https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html
这篇关于字典项的 ElasticSearch/NEST 索引策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!