-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_text_match.rb
38 lines (29 loc) · 1.08 KB
/
simple_text_match.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# gem install elasticsearch
# gem install hashie
require 'elasticsearch'
require 'hashie'
client = Elasticsearch::Client.new log: false
client.indices.delete index: "myindex" rescue nil
client.index index: 'myindex', type: 'mytype', id: 1, body: { title: "this is a title" }
client.index index: 'myindex', type: 'mytype', id: 2, body: { title: "this is a title with something appended" }
client.index index: 'myindex', type: 'mytype', id: 3, body: { title: "prefix - this is a title" }
client.index index: 'myindex', type: 'mytype', id: 4, body: { title: "prefix a - this is a title - suffix b" }
client.indices.refresh index: "myindex"
["this is a title"].each do |q|
results = Hashie::Mash.new(client.search(index: 'myindex', body: {
query: {
match: {
title: {
query: q,
operator: "and",
type: "phrase"
}
}
}
}))
if results.hits
res = results.hits.hits.map(&:_id)
scores = results.hits.hits.map(&:_score)
end
puts "Query: #{q} \t Found count: #{results.hits.total} \t Found: #{res} \t Scores: #{scores}"
end