Commit 390878f3f3ef52e063ef7a4172f0b45f530d2229

Authored by Andrew Kane
1 parent b0150970

Added word: false, match, and highlighted fields to load: false

  1 +## 1.0.4 [unreleased]
  2 +
  3 +- Added `word: false` option
  4 +- Added `match` option
  5 +- Added highlighted fields to `load: false`
  6 +
1 ## 1.0.3 7 ## 1.0.3
2 8
3 - Added support for Elasticsearch 2.1 9 - Added support for Elasticsearch 2.1
lib/searchkick/index.rb
@@ -417,13 +417,14 @@ module Searchkick @@ -417,13 +417,14 @@ module Searchkick
417 field_mapping = { 417 field_mapping = {
418 type: "multi_field", 418 type: "multi_field",
419 fields: { 419 fields: {
420 - field => {type: "string", index: "not_analyzed"},  
421 - "analyzed" => {type: "string", index: "analyzed"}  
422 - # term_vector: "with_positions_offsets" for fast / correct highlighting  
423 - # http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-highlighting.html#_fast_vector_highlighter 420 + field => {type: "string", index: "not_analyzed"}
424 } 421 }
425 } 422 }
426 423
  424 + unless options[:word] == false
  425 + field_mapping[:fields]["analyzed"] = {type: "string", index: "analyzed"}
  426 + end
  427 +
427 mapping_options.except(:highlight).each do |type, fields| 428 mapping_options.except(:highlight).each do |type, fields|
428 if fields.include?(field) 429 if fields.include?(field)
429 field_mapping[:fields][type] = {type: "string", index: "analyzed", analyzer: "searchkick_#{type}_index"} 430 field_mapping[:fields][type] = {type: "string", index: "analyzed", analyzer: "searchkick_#{type}_index"}
@@ -455,6 +456,18 @@ module Searchkick @@ -455,6 +456,18 @@ module Searchkick
455 routing = {required: true, path: options[:routing].to_s} 456 routing = {required: true, path: options[:routing].to_s}
456 end 457 end
457 458
  459 + dynamic_fields = {
  460 + # analyzed field must be the default field for include_in_all
  461 + # http://www.elasticsearch.org/guide/reference/mapping/multi-field-type/
  462 + # however, we can include the not_analyzed field in _all
  463 + # and the _all index analyzer will take care of it
  464 + "{name}" => {type: "string", index: "not_analyzed"}
  465 + }
  466 +
  467 + unless options[:word] == false
  468 + dynamic_fields["analyzed"] = {type: "string", index: "analyzed"}
  469 + end
  470 +
458 mappings = { 471 mappings = {
459 _default_: { 472 _default_: {
460 properties: mapping, 473 properties: mapping,
@@ -468,14 +481,7 @@ module Searchkick @@ -468,14 +481,7 @@ module Searchkick
468 mapping: { 481 mapping: {
469 # http://www.elasticsearch.org/guide/reference/mapping/multi-field-type/ 482 # http://www.elasticsearch.org/guide/reference/mapping/multi-field-type/
470 type: "multi_field", 483 type: "multi_field",
471 - fields: {  
472 - # analyzed field must be the default field for include_in_all  
473 - # http://www.elasticsearch.org/guide/reference/mapping/multi-field-type/  
474 - # however, we can include the not_analyzed field in _all  
475 - # and the _all index analyzer will take care of it  
476 - "{name}" => {type: "string", index: "not_analyzed"},  
477 - "analyzed" => {type: "string", index: "analyzed"}  
478 - } 484 + fields: dynamic_fields
479 } 485 }
480 } 486 }
481 } 487 }
lib/searchkick/query.rb
@@ -30,7 +30,7 @@ module Searchkick @@ -30,7 +30,7 @@ module Searchkick
30 options[:fields].map { |f| "#{f}.autocomplete" } 30 options[:fields].map { |f| "#{f}.autocomplete" }
31 else 31 else
32 options[:fields].map do |value| 32 options[:fields].map do |value|
33 - k, v = value.is_a?(Hash) ? value.to_a.first : [value, :word] 33 + k, v = value.is_a?(Hash) ? value.to_a.first : [value, options[:match] || :word]
34 k2, boost = k.to_s.split("^", 2) 34 k2, boost = k.to_s.split("^", 2)
35 field = "#{k2}.#{v == :word ? 'analyzed' : v}" 35 field = "#{k2}.#{v == :word ? 'analyzed' : v}"
36 boost_fields[field] = boost.to_f if boost 36 boost_fields[field] = boost.to_f if boost
lib/searchkick/results.rb
@@ -42,6 +42,14 @@ module Searchkick @@ -42,6 +42,14 @@ module Searchkick
42 else 42 else
43 hit.except("fields").merge(hit["fields"]) 43 hit.except("fields").merge(hit["fields"])
44 end 44 end
  45 +
  46 + if hit["highlight"]
  47 + highlight = Hash[hit["highlight"].map { |k, v| [k.sub(/\.(analyzed|word_start|word_middle|word_end|text_start|text_middle|text_end)\z/, ""), v.first] }]
  48 + highlight.each do |k, v|
  49 + result["highlighted_#{k}"] ||= (v || result[k])
  50 + end
  51 + end
  52 +
45 result["id"] ||= result["_id"] # needed for legacy reasons 53 result["id"] ||= result["_id"] # needed for legacy reasons
46 Hashie::Mash.new(result) 54 Hashie::Mash.new(result)
47 end 55 end