-
Notifications
You must be signed in to change notification settings - Fork 0
/
partial_phrase_match.rb
40 lines (30 loc) · 1.26 KB
/
partial_phrase_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
39
40
# 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: "is a title this" }
client.index index: 'myindex', type: 'mytype', id: 3, body: { title: "some other document" }
client.index index: 'myindex', type: 'mytype', id: 4, body: { title: "this one is for the hommies" }
client.index index: 'myindex', type: 'mytype', id: 5, body: { title: "this is the end" }
client.indices.refresh index: "myindex"
["for my hommies this is a title and you know it"].each do |q|
results = Hashie::Mash.new(client.search(index: 'myindex', body: {
query: {
match: {
title: {
query: q,
minimum_should_match: "40%"
# http://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-minimum-should-match.html
}
}
}
}))
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