Search
Write a publication
Pull to refresh

MongoDB && Elasticsearch

Расскажу, как подружить MongoDB и Elasticsearch с помощью MongoDB River Plugin for ElasticSearch. По ходу развернем MongoDB Replica Set, Elasticsearch Cluster, создадим пару коллекций с парой тестовых документов в каждой из них, пару индексов по одному на коллекцию, а также затестим что получилось путем отправки нескольких нехитрых запросов через cURL. В процессе используем ConEmu (у меня окна в качестве ОС), MongoDB Shell, Git SCM (Bash + cURL) и Python JSON tool (для читабельного вывода в консоль ответов на запросы в формате JSON).

https://www.youtube.com/watch?v=dfMkhVQvQYo

Команды, использованные по ходу пьесы:
// MongoDB && Elasticsearch
// create replSet
md 1 2 3
mongod --replSet x --port 27001 --dbpath 1 --smallfiles --oplogSize 50
mongod --replSet x --port 27002 --dbpath 2 --smallfiles --oplogSize 50
mongod --replSet x --port 27003 --dbpath 3 --smallfiles --oplogSize 50
mongo --port 27001
rs.initiate({
  _id: "x",
  members: [
    {_id: 0, host: "localhost:27001"},
    {_id: 1, host: "localhost:27002"},
    {_id: 2, host: "localhost:27003"}
  ]
})
rs.status()

// insert data
show dbs
use myapp
db.users.insert([{name: "ass", fullName: "Ass Hole", email: "ass@hole.com"}, {name: "azz", fullName: "Azz Hole"}])
db.users.find().pretty()
db.posts.insert([{text: "wtf ass hole"}, {text: "hi"}])
db.posts.find()
show dbs
show collections

// install mongodb river
plugin --install com.github.richardwilly98.elasticsearch/elasticsearch-river-mongodb/2.0.9
// run elasticsearch
elasticsearch -Des.node.name=Master
curl "localhost:9200"
curl "localhost:9200/_cluster/health" | python -mjson.tool
elasticsearch -Des.node.name=Slave
curl "localhost:9200/_cluster/health" | python -mjson.tool

// create indices
curl -XPUT 'localhost:9200/_river/users/_meta' -d '{
  "type": "mongodb",
  "mongodb": {
    "servers": [
      { "host": "127.0.0.1", "port": 27001 },
      { "host": "127.0.0.1", "port": 27002 },
      { "host": "127.0.0.1", "port": 27003 }
    ],
    "options": {
      "secondary_read_preference": true,
      "include_fields": ["name", "fullName"]
    },
    "db": "myapp",
    "collection": "users"
  },
  "index": {
    "name": "myapp",
    "type": "users"
  }
}'

curl -XPUT 'localhost:9200/_river/posts/_meta' -d '{
  "type": "mongodb",
  "mongodb": {
    "servers": [
      { "host": "127.0.0.1", "port": 27001 },
      { "host": "127.0.0.1", "port": 27002 },
      { "host": "127.0.0.1", "port": 27003 }
    ],
    "options": {
      "secondary_read_preference": true
    },
    "db": "myapp",
    "collection": "posts"
  },
  "index": {
    "name": "myapp",
    "type": "posts"
  }
}'

curl 'localhost:9200/_cat/indices?v'

// search
curl 'localhost:9200/myapp/_search' | python -m json.tool
curl 'localhost:9200/myapp/users/_search' | python -m json.tool
curl 'localhost:9200/myapp/posts/_search' | python -m json.tool
// checkout river
db.posts.update({text: "hi"}, {$set: {text: "Hello, World!"}})
curl 'localhost:9200/myapp/posts/_search' | python -m json.tool
// search
curl 'localhost:9200/myapp/_search?q=ass' | python -m json.tool
curl 'localhost:9200/myapp/_search?q=azz' | python -m json.tool
curl 'localhost:9200/myapp/posts/_search?q=azz' | python -m json.tool
curl 'localhost:9200/myapp/posts/_search?q=world' | python -m json.tool
curl 'localhost:9200/myapp/_search?q=hole' | python -m json.tool
curl 'localhost:9200/myapp/users/_search?q=hole' | python -m json.tool
curl 'localhost:9200/myapp/_search?q=text:hole' | python -m json.tool

Tags:
Hubs:
You can’t comment this publication because its author is not yet a full member of the community. You will be able to contact the author only after he or she has been invited by someone in the community. Until then, author’s username will be hidden by an alias.