본문 바로가기

Elasticsearch

[Elasticsearch] Monstache config script _id override / mongoDB의 특정 필드를 ES _id로 지정하기

반응형

MongoDB에서 Elasticsearch 로 데이터를 옮기는 와중, ES의 _id가 MongoDB의 _id와 동일하게 들어간다는 것을 발견하고 ES의 _id를 field 중 유일한 pk인 buildingManagementNo로 설정해서 다시 monstache insert 하기로 했다. 

 

Few days ago, I did migrate over 10 millions documents from MongoDB to Elasticsearch by using Monstache. But it was found out that _id of ES document was the same as _id of MongoDB so I reinserted data from MongoDB with different config.toml to make it the PK field of the incoming MongoDB document. 

 

 

Monstache 를 설치하고 설정하는 것 까지의 과정은 아래 포스팅에 있다. 

My posting about Monstache installation and basic configuration is below. 

https://blckchainetc.tistory.com/410

 

[MongoDB] mongoDB 데이터 Monstache을 사용하여 elasticsearch로 이동시키기

mongoDB 데이터 약 1,000만건을 Monstache를 이용하여 Elasticsearch로 옮기기! 1. git clone https://github.com/rwynn/monstache.git 2. cd monstache 3. go install 4. make * 뭔가 잘 되었는데 마지막에 이..

blckchainetc.tistory.com

 

monstache/build/darwin-amd64/config.toml 을 이제 수정해준다. (아래 코드만 추가해주면 된다.) 

And now all you have todo is modifying (actually just "adding") the config.toml. 

.
.
.

[[mapping]]
~~~
~~~

[[script]]
namespace = "[dbName].[collectionName]"
script = """
module.exports = function(doc, ns){
doc._meta_monstache = {id: doc['buildingManagementNo']};
return doc;
}
"""

내가 검색하면서 이해한 바로는 namespace는 MongoDB의 db  path 

script = 함수 작성 

doc 은 mongoDB 의 document 

doc._meta_monstache 에 { id : MongoDB.buildingManagementNo}로 설정 

"_"가 있는 _id이지만 id로 그냥 써주는 것 같다. _doc도 doc라고 씀 그리고 리턴해주기 

 

script is like function you would like to execute (I guess)

doc is the incoming document of MongoDB (I guess) 

and _meta_monstache is something to modify the properties of ES? (I have no idea) but it worked! 

 

 

 

이제 다시 monstach 명령어로 MongoDB -> ES으로 옮기기 / 기존의 ES DB는 미리 삭제 후 전체 도큐먼트를 다시 새로 넣어주었다.  

Next, you can reinsert all the documents from the scretch but before that, remember you need to delete the origin Index first. 

./monstache -f ./config.toml

done! 

 

 

 

 

 

Reference : https://github.com/rwynn/monstache/issues/159

https://github.com/rwynn/monstache/issues/151

반응형