{"record":{"id":"b1a4df5463caf55f","repo":"ankane/searchkick","slug":"must-specify-fields-to-search","errorCode":null,"errorMessage":"Must specify fields to search","messagePattern":"Must specify fields to search","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/searchkick/query.rb","lineNumber":288,"sourceCode":"          :profile, :select, :smart_aggs, :suggest, :where]\n        raise ArgumentError, \"Options incompatible with body option: #{ignored_options.join(\", \")}\" if ignored_options.any?\n        payload = @json\n      else\n        must_not = []\n        should = []\n\n        if options[:similar]\n          like = options[:similar] == true ? term : options[:similar]\n          query = {\n            more_like_this: {\n              like: like,\n              min_doc_freq: 1,\n              min_term_freq: 1,\n              analyzer: \"searchkick_search2\"\n            }\n          }\n          if fields.all? { |f| f.start_with?(\"*.\") }\n            raise ArgumentError, \"Must specify fields to search\"\n          end\n          if fields != [\"_all\"]\n            query[:more_like_this][:fields] = fields\n          end\n        elsif all && !options[:exclude]\n          query = {\n            match_all: {}\n          }\n        else\n          queries = []\n\n          misspellings =\n            if options.key?(:misspellings)\n              options[:misspellings]\n            else\n              true\n            end\n","sourceCodeStart":270,"sourceCodeEnd":306,"githubUrl":"https://github.com/ankane/searchkick/blob/93e901a75b11a25101668a616e006b158251b16e/lib/searchkick/query.rb#L270-L306","documentation":"For `similar:` (more_like_this) queries, Elasticsearch requires concrete field names — a wildcard like `*.analyzed` cannot back an mlt query. `set_fields` returns wildcards when no explicit fields/searchable/default_fields are configured, so the similar-branch raises `ArgumentError` when every resolved field starts with `*.`.","triggerScenarios":"`Product.search(\"text\", similar: true)` (or `similar: \"some text\"`) on a model that sets no `searchable:`/`fields:` and has `_all` disabled; or explicitly passing `fields: [\"*_analyzed\"]`-style wildcards with `similar:`. The check runs at query build, before any request.","commonSituations":"Adding a 'related items' feature with `similar: true` to a model indexed with `searchable: false`-style defaults or `_all: false`; copying the README's similar example without the `fields:` line; migrating from old `_all`-based configs where similar used to work implicitly.","solutions":["Specify concrete fields at query time: `Product.search(product.name, fields: [:name, :description], similar: true)`.","Or set them on the model: `searchkick searchable: [:name, :description]`, then reindex.","If the model relies on `_all`, re-enable it (`_all: true`) so the similar query can target `_all` instead of wildcards.","Remember `similar: true` uses the search term as the like-text; pass the source text via `similar: record.name` for record-based similarity."],"exampleFix":"# before\nProduct.search(\"organic whole milk\", similar: true)\n# => ArgumentError: Must specify fields to search\n\n# after\nProduct.search(\"organic whole milk\", fields: [:name, :description], similar: true)","handlingStrategy":"validation","validationCode":"def similar_products(record, fields: %i[name description])\n  raise ArgumentError, \"similar requires explicit fields\" if fields.blank? || fields.all? { |f| f.to_s.start_with?(\"*\") }\n  record.class.search(record.name, fields: fields, similar: true, load: false)\nend","typeGuard":"def similar_fields_valid?(fields)\n  fields.present? && !fields.map(&:to_s).all? { |f| f.start_with?(\"*.\") }\nend","tryCatchPattern":null,"preventionTips":["Always pass explicit `fields:` when using `similar:`.","Set `searchable:` on models you plan to use for related-item features, then reindex.","Wrap similar lookups in one helper so the field requirement is checked in one place."],"tags":["searchkick","similar","more-like-this","fields","argumenterror"],"backgroundTag":"missing-required-option","analyzedSha":"93e901a75b11a25101668a616e006b158251b16e","analyzedAt":"2026-08-21T19:06:23.767Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}