mongodb/mongoid · error · ArgumentError

hnswOptions is only supported with indexingMethod: hnsw

Error message

hnswOptions is only supported with indexingMethod: hnsw

What it means

Raised by validate_vector_index_definition! (used by vector_search_index) when an index field specifies hnswOptions while its indexingMethod is not 'hnsw'. MongoDB only accepts hnswOptions for HNSW indexes, not flat ones.

Solutions

  1. Remove :hnswOptions from fields using indexingMethod: 'flat'
  2. Set indexingMethod: 'hnsw' on the field that needs hnswOptions
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at lib/mongoid/search_indexable.rb:408 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of mongodb/mongoid@0da0e23d71 (2026-08-23). Data as JSON: /api/errors/22da3b14c7c5bf22. Report an issue: GitHub.

Appendix: source

Thrown at lib/mongoid/search_indexable.rb:408

        agg_pipeline.concat(Array(pipeline))

        collection.aggregate(agg_pipeline).map { |doc| instantiate(doc) }
      end

      private

      # Validates the vector index definition, raising ArgumentError for
      # combinations that MongoDB does not support.
      #
      # @param [ Hash ] defn The vector search index definition.
      def validate_vector_index_definition!(defn)
        fields = defn[:fields] || defn['fields'] || []
        fields.each do |field|
          method = field[:indexingMethod] || field['indexingMethod']
          next unless method.to_s == 'flat'
          next unless field[:hnswOptions] || field['hnswOptions']

          raise ArgumentError, 'hnswOptions is only supported with indexingMethod: hnsw'
        end
      end

      # Retrieves the index records for the indexes with the given names.
      #
      # @param [ Array<String> ] names the index names to query
      #
      # @return [ Array<Hash> ] the raw index documents
      def get_indexes(names)
        collection.search_indexes.select { |i| names.include?(i['name']) }
      end

      # Resolves the index name and vector path from the declared specs,
      # applying inference when either is omitted.
      #
      # @param [ String | Symbol | nil ] index The requested index name.
      # @param [ String | Symbol | nil ] path The requested field path.
      #

View on GitHub (pinned to 0da0e23d71)