gpt4 book ai didi

types - Elasticsearch:有没有办法将对象字段的所有(可能是动态的)子字段声明为字符串?

转载 作者:行者123 更新时间:2023-12-01 09:27:44 24 4
gpt4 key购买 nike

我有一个 doc_type,其映射类似于这个非常简化的映射:

{
"test":{
"properties":{
"name":{
"type":"string"
},
"long_searchable_text":{
"type":"string"
},
"clearances":{
"type":"object"
}
}
}
}

clearances 字段应该是一个对象,带有一系列用于过滤目的的字母数字标识符。典型的文档将具有以下格式:

{
"name": "Lord Macbeth",
"long_searchable_text": "Life's but a walking shadow, a poor player, that..."
"clearances": {
"glamis": "aa2862jsgd",
"cawdor": "3463463551"
}
}

问题在于,有时在索引期间,对象字段 clearances 内的新字段的第一个索引内容将完全是数字,如上例所示。这会导致 Elasticsearch 将此字段的类型推断为 long。但这是一个意外。该字段在另一个文档中可能是字母数字。当后一个包含此字段中的字母数字值的文档到达时,我得到一个解析异常:

{"error":"MapperParsingException[failed to parse [clearances.cawdor]]; nested: NumberFormatException[For input string: \"af654hgss1\"]; ","status":400}% 

我尝试使用这样定义的动态模板来解决这个问题:

{
"test":{
"properties":{
"name":{
"type":"string"
},
"long_searchable_text":{
"type":"string"
},
"clearances":{
"type":"object"
}
}
},
"dynamic_templates":[
{
"source_template":{
"match":"clearances.*",
"mapping":{
"type":"string",
"index":"not_analyzed"
}
}
}
]
}

但是,如果第一个索引文档有一个可以解析为整数的 clearance.some_subfield 值,它将被推断为整数,并且所有后续文档都具有字母数字值该子字段将无法被索引。

我可以列出映射中的所有当前子字段,但它们很多,并且我希望它们的数量在未来会增加(触发映射的更新和完全重新索引的需要......)。

有没有办法让这项工作在每次添加新子字段时不诉诸这种完整的重新索引?

最佳答案

你快到了。

首先,您的动态映射路径必须在 clearances.* 上,并且它必须是 path_match 而不是普通的 match

这是一个可运行的示例:https://www.found.no/play/gist/df030f005da71827ca96

export ELASTICSEARCH_ENDPOINT="http://localhost:9200"

# Create indexes

curl -XPUT "$ELASTICSEARCH_ENDPOINT/play" -d '{
"settings": {},
"mappings": {
"test": {
"dynamic_templates": [
{
"clearances_as_string": {
"path_match": "clearances.*",
"mapping": {
"type": "string",
"index": "not_analyzed"
}
}
}
]
}
}
}'


# Index documents
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_bulk?refresh=true" -d '
{"index":{"_index":"play","_type":"test"}}
{"clearances":{"glamis":1234,"cawdor":5678}}
{"index":{"_index":"play","_type":"test"}}
{"clearances":{"glamis":"aa2862jsgd","cawdor":"some string"}}
'

# Do searches

curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '
{
"facets": {
"cawdor": {
"terms": {
"field": "clearances.cawdor"
}
}
}
}
'

关于types - Elasticsearch:有没有办法将对象字段的所有(可能是动态的)子字段声明为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20401709/

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