Skip to content

Instantly share code, notes, and snippets.

@tfuji
Last active October 30, 2019 02:33
Show Gist options
  • Save tfuji/18c2a82418059b957d9fdf3ef6bf0df5 to your computer and use it in GitHub Desktop.
Save tfuji/18c2a82418059b957d9fdf3ef6bf0df5 to your computer and use it in GitHub Desktop.
es_msearch_test.rb
#!/usr/bin/env ruby
require 'elasticsearch'
require 'json'
require 'pp'
begin
#client = Elasticsearch::Client.new({log: true, hosts: { host: 'localhost'}})
client = Elasticsearch::Client.new({hosts: { host: 'localhost', port: 9291 }})
q ='water'
from = 0
size = 20
res = client.msearch \
body: [
{ index: 'meo', type: 'mdb_common' } ,
{ from: from, size: size, _source: ['identifier', 'name'], sort: ['_id'], query: { multi_match: {fields: [ 'name', 'alternateNames'], query: "#{q}", operator: 'and'} }},
{ index: 'strain', type: 'mdb_common' },
{ from: from, size: size, _source: ['identifier', 'name'], sort: ['_id'], query: { multi_match: {fields: [ 'name', 'alternateNames'], query: "#{q}", operator: 'and'} }},
{ index: 'mpo', type: 'mdb_common'},
{ from: from, size: size, _source: ['identifier', 'name'], sort: ['_id'], query: { multi_match: {fields: [ 'name', 'alternateNames'], query: "#{q}", operator: 'and'} }},
{ index: 'hmado', type: 'mdb_common'},
{ from: from, size: size, _source: ['identifier', 'name'], sort: ['_id'], query: { multi_match: {fields: [ 'name', 'alternateNames'], query: "#{q}", operator: 'and'} }},
{ index: 'pdo', type: 'mdb_common'},
{ from: from, size: size, _source: ['identifier', 'name'], sort: ['_id'], query: { multi_match: {fields: [ 'name', 'alternateNames'], query: "#{q}", operator: 'and'} }},
{ index: 'kegg', type: 'mdb_common'},
{ from: from, size: size, _source: ['identifier', 'name'], sort: ['_id'], query: { multi_match: {fields: [ 'name', 'alternateNames'], query: "#{q}", operator: 'and'} }},
{ index: 'taxonomy', type: 'mdb_common'},
{ from: from, size: size, _source: ['identifier', 'name'], sort: ['_id'], query: { multi_match: {fields: [ 'name', 'alternateNames'], query: "#{q}", operator: 'and'} }},
{ index: 'sample', type: 'mdb_common'},
{ from: from, size: size, _source: ['identifier', 'name'], sort: ['_id'], query: { multi_match: {fields: [ 'name', 'alternateNames'], query: "#{q}", operator: 'and'} }},
{ index: 'mbgd', type: 'mdb_common'},
{ from: from, size: size, _source: ['identifier', 'name'], sort: ['_id'], query: { multi_match: {fields: [ 'name', 'alternateNames'], query: "#{q}", operator: 'and'} }}
]
res["responses"].each do |idx|
#pp idx
puts "count: #{idx['hits']['total']}"
pp idx['hits']['hits'].map{ |hit| [hit['_source'], hit['_index']]}
end
rescue Exception => ex
puts ex.backtrace.first + ": #{ex.message} (#{ex.class})"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment