|
| 1 | +module Elasticsearch |
| 2 | + module API |
| 3 | + module Actions |
| 4 | + |
| 5 | + # Return documents similar to the specified one. |
| 6 | + # |
| 7 | + # Performs a `more_like_this` query with the specified document as the input. |
| 8 | + # |
| 9 | + # @example Search for similar documents using the `title` property of document `myindex/mytype/1` |
| 10 | + # |
| 11 | + # # First, let's setup a synonym-aware analyzer ("quick" <=> "fast") |
| 12 | + # client.indices.create index: 'myindex', body: { |
| 13 | + # settings: { |
| 14 | + # analysis: { |
| 15 | + # filter: { |
| 16 | + # synonyms: { |
| 17 | + # type: 'synonym', |
| 18 | + # synonyms: [ "quick,fast" ] |
| 19 | + # } |
| 20 | + # }, |
| 21 | + # analyzer: { |
| 22 | + # title_synonym: { |
| 23 | + # type: 'custom', |
| 24 | + # tokenizer: 'whitespace', |
| 25 | + # filter: ['lowercase', 'stop', 'snowball', 'synonyms'] |
| 26 | + # } |
| 27 | + # } |
| 28 | + # } |
| 29 | + # }, |
| 30 | + # mappings: { |
| 31 | + # mytype: { |
| 32 | + # properties: { |
| 33 | + # title: { |
| 34 | + # type: 'string', |
| 35 | + # analyzer: 'title_synonym' |
| 36 | + # } |
| 37 | + # } |
| 38 | + # } |
| 39 | + # } |
| 40 | + # } |
| 41 | + # |
| 42 | + # # Index three documents |
| 43 | + # client.index index: 'myindex', type: 'mytype', id: 1, body: { title: 'Quick Brown Fox' } |
| 44 | + # client.index index: 'myindex', type: 'mytype', id: 2, body: { title: 'Slow Black Dog' } |
| 45 | + # client.index index: 'myindex', type: 'mytype', id: 3, body: { title: 'Fast White Rabbit' } |
| 46 | + # client.indices.refresh index: 'myindex' |
| 47 | + # |
| 48 | + # client.mlt index: 'myindex', type: 'mytype', id: 1, mlt_fields: 'title', min_doc_freq: 1, min_term_freq: 1 |
| 49 | + # # => { ... {"title"=>"Fast White Rabbit"}}]}} |
| 50 | + # |
| 51 | + # @option arguments [String] :id The document ID (*Required*) |
| 52 | + # @option arguments [String] :index The name of the index (*Required*) |
| 53 | + # @option arguments [String] :type The type of the document (use `_all` to fetch |
| 54 | + # the first document matching the ID across all types) (*Required*) |
| 55 | + # @option arguments [Hash] :body A specific search request definition |
| 56 | + # @option arguments [Number] :boost_terms The boost factor |
| 57 | + # @option arguments [Number] :max_doc_freq The word occurrence frequency as count: words with higher occurrence |
| 58 | + # in the corpus will be ignored |
| 59 | + # @option arguments [Number] :max_query_terms The maximum query terms to be included in the generated query |
| 60 | + # @option arguments [Number] :max_word_len The minimum length of the word: longer words will be ignored |
| 61 | + # @option arguments [Number] :min_doc_freq The word occurrence frequency as count: words with lower occurrence |
| 62 | + # in the corpus will be ignored |
| 63 | + # @option arguments [Number] :min_term_freq The term frequency as percent: terms with lower occurence |
| 64 | + # in the source document will be ignored |
| 65 | + # @option arguments [Number] :min_word_len The minimum length of the word: shorter words will be ignored |
| 66 | + # @option arguments [List] :mlt_fields Specific fields to perform the query against |
| 67 | + # @option arguments [Number] :percent_terms_to_match How many terms have to match in order to consider |
| 68 | + # the document a match (default: 0.3) |
| 69 | + # @option arguments [String] :routing Specific routing value |
| 70 | + # @option arguments [Number] :search_from The offset from which to return results |
| 71 | + # @option arguments [List] :search_indices A comma-separated list of indices to perform the query against |
| 72 | + # (default: the index containing the document) |
| 73 | + # @option arguments [String] :search_query_hint The search query hint |
| 74 | + # @option arguments [String] :search_scroll A scroll search request definition |
| 75 | + # @option arguments [Number] :search_size The number of documents to return (default: 10) |
| 76 | + # @option arguments [String] :search_source A specific search request definition (instead of using the request body) |
| 77 | + # @option arguments [String] :search_type Specific search type (eg. `dfs_then_fetch`, `count`, etc) |
| 78 | + # @option arguments [List] :search_types A comma-separated list of types to perform the query against |
| 79 | + # (default: the same type as the document) |
| 80 | + # @option arguments [List] :stop_words A list of stop words to be ignored |
| 81 | + # |
| 82 | + # @see http://elasticsearch.org/guide/reference/api/more-like-this/ |
| 83 | + # |
| 84 | + def mlt(arguments={}) |
| 85 | + raise ArgumentError, "Required argument 'index' missing" unless arguments[:index] |
| 86 | + raise ArgumentError, "Required argument 'type' missing" unless arguments[:type] |
| 87 | + raise ArgumentError, "Required argument 'id' missing" unless arguments[:id] |
| 88 | + method = HTTP_GET |
| 89 | + path = Utils.__pathify Utils.__escape(arguments[:index]), |
| 90 | + Utils.__escape(arguments[:type]), |
| 91 | + Utils.__escape(arguments[:id]), |
| 92 | + '_mlt' |
| 93 | + |
| 94 | + params = Utils.__validate_and_extract_params arguments, ParamsRegistry.get(__method__) |
| 95 | + |
| 96 | + [:mlt_fields, :search_indices, :search_types, :stop_words].each do |name| |
| 97 | + params[name] = Utils.__listify(params[name]) if params[name] |
| 98 | + end |
| 99 | + |
| 100 | + body = arguments[:body] |
| 101 | + |
| 102 | + perform_request(method, path, params, body).body |
| 103 | + end |
| 104 | + |
| 105 | + # Register this action with its valid params when the module is loaded. |
| 106 | + # |
| 107 | + # @since 6.2.0 |
| 108 | + ParamsRegistry.register(:mlt, [ |
| 109 | + :boost_terms, |
| 110 | + :max_doc_freq, |
| 111 | + :max_query_terms, |
| 112 | + :max_word_len, |
| 113 | + :min_doc_freq, |
| 114 | + :min_term_freq, |
| 115 | + :min_word_len, |
| 116 | + :mlt_fields, |
| 117 | + :percent_terms_to_match, |
| 118 | + :routing, |
| 119 | + :search_from, |
| 120 | + :search_indices, |
| 121 | + :search_query_hint, |
| 122 | + :search_scroll, |
| 123 | + :search_size, |
| 124 | + :search_source, |
| 125 | + :search_type, |
| 126 | + :search_types, |
| 127 | + :stop_words ].freeze) |
| 128 | + end |
| 129 | + end |
| 130 | +end |
0 commit comments