我正在為我的專案開發搜索引擎,我正在使用Elasticsearch和node.js服務器。
每天晚上我都有一個決議器從某個網站上抓取資料并將其插入資料庫。現在它復制了我已經擁有的資料。
例如,我可以在插入檔案時在索引中創建一個唯一欄位title : {unique : true},這樣它就不會向我插入帶有此標題的檔案
這是我的代碼:
async function insertManual(manual) {
return new Promise(async (resolve, reject) => {
const result = await client.index({
index : 'completeindexthree',
body : {
brand : manual.brand,
category : manual.category,
url : manual.url,
title : manual.title, // example {unique : true}
parsingData : new Date().toString()
}
})
await client.indices.refresh({index: 'completeindexthree'})
resolve(result);
})
}
第二個是,我怎樣才能從node.js中的那個索引而不是從logstach中洗掉我已經按標題進入的所有重復項?
uj5u.com熱心網友回復:
特長;
是的,這是可能的,但不是通過使用unique關鍵字。根據檔案,如果您設定了一個_id并且這個 id 已經存在,它將被替換/覆寫
如果目標是索引并且檔案已經存在,則請求更新檔案并增加其版本。
此外,您會發現此部分
使用 _create 保證檔案僅在它不存在時才被索引。
修理
您應該設定_id每個檔案并使用create
您的代碼可能如下所示:
async function insertManual(manual) {
return new Promise(async (resolve, reject) => {
const result = await client.create({
index : 'completeindexthree',
id: manual.id, // <- Here is your unique id.
body : {
brand : manual.brand,
category : manual.category,
url : manual.url,
title : manual.title, // example {unique : true}
parsingData : new Date().toString()
}
})
await client.indices.refresh({index: 'completeindexthree'})
resolve(result);
})
}
uj5u.com熱心網友回復:
如果您不提供 id,彈性搜索會創建一個唯一的 id,如果提供,它會創建您提供的 id。
有效載荷應該是這樣的
{
id:"you_unique_id",
body:{foo,"bar"}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/485192.html
上一篇:WSL2Nginx PHPFPM在連接到上游時失敗(111:連接被拒絕),客戶端:172.23.0.1,上游:“fastcgi://172.23.0.3:9001”
