gpt4 book ai didi

javascript - 将副本添加到索引模板上的elasticsearch

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

我使用elasticsearch 6.2,我想优化elasticsearch搜索功能,将所有字段值复制到一个值并生成,而不是在一个字段而不是多个字段上通过查询字符串进行搜索。怎么做?如何复制所有字段,现在将哪个字段复制到一个字段即可。

初始化索引模板

export const init = async types => {
try {
let client = createClient()

const templateSettings = {
index_patterns : ['*'],
settings: indexTemplateSettings,
mappings : types.reduce((p, type) => ({
...p,
[type] : {
numeric_detection: true,
_source : {enabled : true},
'properties': {
'searchIndex': {
'type': 'text',
},
'*': {
'type': 'text',
'copy_to': 'searchIndex',
},
},
},
}), {}),
}

await client.indices.putTemplate({
name: 'default',
body: templateSettings,
},(error, response) => {
logger.silly('Pushing of index template completed', response)
})
} catch (e) {
logger.error(e)
}
}

看跌指数

export const push = (message, type) => new Promise(async resolve => {
try {
let client = createClient()
let indexCreationTime = new Date('2016-02-08').toISOString().substring(0, 10)

// '2016-02-08'
console.log(message, 'message')
console.log(type, 'type')

await client.index({
index: type.toLowerCase(),
type,
body: {
...message,
_timestampIndex: indexCreationTime,
},
},
(error, response) => {
logger.silly('Pushing of data completed', response)

resolve(response)
})

} catch (e) {
logger.error(e)
}
})

最佳答案

最好的方法是创建一个利用 dynamic template 的索引模板。这将捕获所有字段并将 copy_to 参数添加到其定义中。

PUT _template/my-template
{
"index_patterns": ["*"],
"settings": {},
"mappings": {
"_doc": {
"dynamic_templates": [
{
"all": {
"match": "*",
"mapping": {
"type": "text",
"copy_to": "searchIndex"
}
}
}
],
"properties": {
"searchIndex": {
"type": "text"
}
}
}
}
}

关于javascript - 将副本添加到索引模板上的elasticsearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49752705/

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