Загрузка документов в облако AWS Cloudsearch

Я пытаюсь загрузить объект json в AWS Cloudsearch. Вот весь мой код для создания полей индекса:

  require 'aws-sdk'
  require 'pp'

  class CloudSearch
    @@client = Aws::CloudSearch::Client.new({region: 'us-east-1',credentials: Aws::Credentials.new('KEY', 'SECRET')})
    def self.create_index_fields
      Aws.config.update({
        region: 'us-east-1'
      })

      client = @@client
      fields = {'id' => 'metadata',
                'name' => 'search',
                'rating' => 'sort',
                'review_count' => 'sort',
                'price' => 'sort',
                'category_name' => 'search/filter',
                'rank_in_category' => 'sort',
                'brand_name' => 'search/filter',
                'award_winner' => 'filter'
              }
      fields.each do |key, value|
        options = {}
        indexFields = value.split("/")
        options[:domain_name] = "dev-purchxapp-com"
        options[:index_field] = {}
        options[:index_field][:index_field_name] = key
        type = :text_options
        if key == "price"
          options[:index_field][:index_field_type] = "double"
          type = :double_options
        elsif key == "review_count" || key == "rating"
          options[:index_field][:index_field_type] = "int"
          type = :int_options
        else
          options[:index_field][:index_field_type] = "literal"
          type = :literal_options
        end      
        options[:index_field][type] = {}
        if indexFields.include?('search')
          options[:index_field][type][:search_enabled] = true
        end
        if indexFields.include?('sort') || indexFields.include?('filter')
          options[:index_field][type][:facet_enabled] = true
        end
        options[:index_field][type][:return_enabled] = true

        res = client.define_index_field(options)
        pp(res)
      end
    end

    def self.list_index_fields
      client = @@client
      res=client.describe_index_fields({
        domain_name: "dev-purchxapp-com"
      })
      res
    end

    def self.delete_index_field(field)
      client = @@client
      res = client.delete_index_field({
        domain_name: "dev-purchxapp-com", # required
        index_field_name: field, # required
      })
    end

    def self.delete_all_index_fields
      client = @@client
          fields = {'id' => 'metadata',
                'name' => 'search',
                'rating' => 'sort',
                'review_count' => 'sort',
                'price' => 'sort',
                'category_name' => 'search/filter',
                'rank_in_category' => 'sort',
                'brand_name' => 'search/filter',
                'award_winner' => 'filter'
              }
      fields.each do |key, value|
        res = client.delete_index_field({
          domain_name: "dev-purchxapp-com", # required
          index_field_name: key, # required
        })
      end
    end

    def self.create_cloudsearch_domain
      client = @@client
      res = client.create_domain({domain_names: ["dev-purchxapp-com"]})
    end

    def self.list_cloudsearch_domains
      res = @@client.describe_domains({domain_names: ["dev-purchxapp-com"]})
    end

    def self.index_documents
      res = @@client.index_documents()
    end
  end

Когда create_index_fields запускается из консоли rails, он показывает, что поля созданы, но когда я иду загружать документы, он говорит, что не может найти поля с разными именами полей, которые я определяю.

определение хэша для превращения в объект json:

    def cloudsearch_product_json
      fields = {}
      fields[:award_winner] = !self.consumr_approved_at.nil? ? 1 : 0
      fields[:brand_name] = !self.brand.nil? ? self.brand.name.to_s : ""
      fields[:category_name] = !self.category.nil? ? self.category.name.to_s : ""
      fields[:description] = ""
      fields[:id] = self.id
      fields[:name] = self.name
      fields[:price] = self.price.to_s
      fields[:rank_in_category] = self.rank_in_category.to_s
      fields[:rating] = self.rating
      fields[:review_count] = self.review_count

      data = {}
      data[:type] = "add"
      data[:id] = "product-#{self.id }"
      data[:fields] = fields
      data
    end

И задача загрузки документа:

    require 'aws-sdk'
    require 'pp'

    namespace :cloud_search do
      task :index_all_products => :environment do
        Aws.config.update({
          region: 'us-east-1', access_key_id: 'KEY', secret_access_key: 'SECRET'
        })
        client = Aws::CloudSearchDomain::Client.new(endpoint:AppConfig.cloud_search_host)
        product = "[#{Product.first.cloudsearch_product_json.to_json}]"
        resp = client.upload_documents({documents: product, content_type: "application/json",})
      end
    end

и, наконец, моя ошибка:

    Aws::CloudSearchDomain::Errors::DocumentServiceException: { ["Field "award_winner" does not exist in domain configuration (near operation with index 1; document_id product-1)","Field "brand_name" does not exist in domain configuration (near operation with index 1; document_id product-1)","Field "category_name" does not exist in domain configuration (near operation with index 1; document_id product-1)","Field "description" does not exist in domain configuration (near operation with index 1; document_id product-1)","Field "id" does not exist in domain configuration (near operation with index 1; document_id product-1)","Field "name" does not exist in domain configuration (near operation with index 1; document_id product-1)","Field "price" does not exist in domain configuration (near operation with index 1; document_id product-1)","Field "rank_in_category" does not exist in domain configuration (near operation with index 1; document_id product-1)","Field "rating" does not exist in domain configuration (near operation with index 1; document_id product-1)","Field "review_count" does not exist in domain configuration (near operation with index 1; document_id product-1)"] }

Я забыл что-то инициализировать или что происходит? Любая помощь приветствуется. Надеюсь, я дал вам достаточно, чтобы помочь мне понять это. Я читал документы AWS SDK в течение пары недель, пытаясь понять это, и мне не повезло. http://docs.aws.amazon.com/sdkforruby/api/Aws/CloudSearch/Client.html http://docs.aws.amazon.com/cloudsearch/latest/developerguide/configuring-index-fields.html

Спасибо за вашу помощь

Вот что я получаю, перечисляя свои поля индекса в интерфейсе командной строки AWS:

C:\Users\ndalton>aws cloudsearch describe-index-fields --domain-name dev-purchxa
pp-com
{
    "IndexFields": [
        {
            "Status": {
                "PendingDeletion": false,
                "State": "RequiresIndexDocuments",
                "CreationDate": "2015-08-11T20:54:00.568Z",
                "UpdateVersion": 200,
                "UpdateDate": "2015-08-18T15:08:47.629Z"
            },
            "Options": {
                "LiteralOptions": {
                    "FacetEnabled": true,
                    "ReturnEnabled": true
                },
                "IndexFieldType": "literal",
                "IndexFieldName": "award_winner"
            }
        },
        {
            "Status": {
                "PendingDeletion": false,
                "State": "RequiresIndexDocuments",
                "CreationDate": "2015-08-11T20:53:59.962Z",
                "UpdateVersion": 199,
                "UpdateDate": "2015-08-18T15:08:46.704Z"
            },
            "Options": {
                "LiteralOptions": {
                    "FacetEnabled": true,
                    "SearchEnabled": true,
                    "ReturnEnabled": true
                },
                "IndexFieldType": "literal",
                "IndexFieldName": "brand_name"
            }
        },
        {
            "Status": {
                "PendingDeletion": false,
                "State": "RequiresIndexDocuments",
                "CreationDate": "2015-08-11T20:53:58.440Z",
                "UpdateVersion": 197,
                "UpdateDate": "2015-08-18T15:08:45.556Z"
            },
            "Options": {
                "LiteralOptions": {
                    "FacetEnabled": true,
                    "SearchEnabled": true,
                    "ReturnEnabled": true
                },
                "IndexFieldType": "literal",
                "IndexFieldName": "category_name"
            }
        },
        {
            "Status": {
                "PendingDeletion": false,
                "State": "RequiresIndexDocuments",
                "CreationDate": "2015-08-11T21:11:20.083Z",
                "UpdateVersion": 73,
                "UpdateDate": "2015-08-12T21:58:32.590Z"
            },
            "Options": {
                "LiteralOptions": {
                    "SearchEnabled": true,
                    "ReturnEnabled": true
                },
                "IndexFieldType": "literal",
                "IndexFieldName": "description"
            }
        },
        {
            "Status": {
                "PendingDeletion": false,
                "State": "RequiresIndexDocuments",
                "CreationDate": "2015-08-11T20:53:54.808Z",
                "UpdateVersion": 192,
                "UpdateDate": "2015-08-18T15:08:41.349Z"
            },
            "Options": {
                "LiteralOptions": {
                    "ReturnEnabled": true
                },
                "IndexFieldType": "literal",
                "IndexFieldName": "id"
            }
        },
        {
            "Status": {
                "PendingDeletion": false,
                "State": "RequiresIndexDocuments",
                "CreationDate": "2015-08-11T20:53:55.684Z",
                "UpdateVersion": 193,
                "UpdateDate": "2015-08-18T15:08:42.233Z"
            },
            "Options": {
                "LiteralOptions": {
                    "SearchEnabled": true,
                    "ReturnEnabled": true
                },
                "IndexFieldType": "literal",
                "IndexFieldName": "name"
            }
        },
        {
            "Status": {
                "PendingDeletion": false,
                "State": "RequiresIndexDocuments",
                "CreationDate": "2015-08-11T20:53:57.682Z",
                "UpdateVersion": 196,
                "UpdateDate": "2015-08-18T15:08:44.738Z"
            },
            "Options": {
                "DoubleOptions": {
                    "FacetEnabled": true,
                    "ReturnEnabled": true
                },
                "IndexFieldType": "double",
                "IndexFieldName": "price"
            }
        },
        {
            "Status": {
                "PendingDeletion": false,
                "State": "RequiresIndexDocuments",
                "CreationDate": "2015-08-11T20:53:59.214Z",
                "UpdateVersion": 198,
                "UpdateDate": "2015-08-18T15:08:46.145Z"
            },
            "Options": {
                "LiteralOptions": {
                    "FacetEnabled": true,
                    "ReturnEnabled": true
                },
                "IndexFieldType": "literal",
                "IndexFieldName": "rank_in_category"
            }
        },
        {
            "Status": {
                "PendingDeletion": false,
                "State": "RequiresIndexDocuments",
                "CreationDate": "2015-08-11T20:53:56.494Z",
                "UpdateVersion": 194,
                "UpdateDate": "2015-08-18T15:08:43.476Z"
            },
            "Options": {
                "IntOptions": {
                    "FacetEnabled": true,
                    "ReturnEnabled": true
                },
                "IndexFieldType": "int",
                "IndexFieldName": "rating"
            }
        },
        {
            "Status": {
                "PendingDeletion": false,
                "State": "RequiresIndexDocuments",
                "CreationDate": "2015-08-11T20:53:57.158Z",
                "UpdateVersion": 195,
                "UpdateDate": "2015-08-18T15:08:44.008Z"
            },
            "Options": {
                "IntOptions": {
                    "FacetEnabled": true,
                    "ReturnEnabled": true
                },
                "IndexFieldType": "int",
                "IndexFieldName": "review_count"
            }
        },
        {
            "Status": {
                "PendingDeletion": true,
                "State": "RequiresIndexDocuments",
                "CreationDate": "2015-08-11T20:22:56.541Z",
                "UpdateVersion": 201,
                "UpdateDate": "2015-08-18T15:11:34.982Z"
            },
            "Options": {
                "TextOptions": {
                    "AnalysisScheme": "_en_default_"
                },
                "IndexFieldType": "text",
                "IndexFieldName": "testfield"
            }
        }
    ]
}

Мой жестко закодированный файл JSON выглядит так:

     [{"type":"add","id":"product-1","fields":{"award_winner":0,"brand_name":"","category_name":"","description":"","id":1,"name":"CNSMRBLKHL","price":"","rank_in_category":"","rating":"4.67","review_count":3}}] 

И ошибка остается прежней:

C:\Users\ndalton>aws cloudsearchdomain --endpoint-url http://doc-dev-purchxapp-c
om-rjxkouy2hppztmth47dn2oowua.us-east-1.cloudsearch.amazonaws.com upload-documen
ts --content-type application/json --documents \\OGDC1\og-users\ndalton\Document
s\test.json

A client error (DocumentServiceException) occurred when calling the UploadDocume
nts operation: { ["Field "award_winner" does not exist in domain configuration (
near operation with index 1; document_id product-1)","Field "brand_name" does no
t exist in domain configuration (near operation with index 1; document_id produc
t-1)","Field "category_name" does not exist in domain configuration (near operat
ion with index 1; document_id product-1)","Field "description" does not exist in
 domain configuration (near operation with index 1; document_id product-1)","Fie
ld "id" does not exist in domain configuration (near operation with index 1; doc
ument_id product-1)","Field "name" does not exist in domain configuration (near
operation with index 1; document_id product-1)","Field "price" does not exist in
 domain configuration (near operation with index 1; document_id product-1)","Fie
ld "rank_in_category" does not exist in domain configuration (near operation wit
h index 1; document_id product-1)","Field "rating" does not exist in domain conf
iguration (near operation with index 1; document_id product-1)","Field "review_c
ount" does not exist in domain configuration (near operation with index 1; docum
ent_id product-1)"] }

person Neil    schedule 18.08.2015    source источник
comment
1) Если это ваши настоящие ключи AWS, отредактируйте пост как можно скорее. 2) Проверьте в веб-консоли AWS — видите ли вы там свои поля? 3) То, что у вас здесь, довольно сложно, и это усложнит отладку. Попробуйте упростить, например, определив жестко запрограммированный документ json (создайте его, распечатав отправляемый в данный момент json) и загрузив его через веб-консоль AWS — это работает?   -  person alexroussos    schedule 19.08.2015
comment
Спасибо, я отредактировал ключ и секрет. Сейчас у меня нет доступа к веб-консоли AWS. Все, что мне дали, это ключ доступа и секрет для него.   -  person Neil    schedule 19.08.2015
comment
Вы можете проверить свою схему с помощью инструментов интерфейса командной строки AWS CS по адресу docs.aws.amazon.com/cloudsearch/latest/developerguide/ и запустите cs-describe-domain --show-all. Похоже, что в вашем индексе отсутствует несколько полей, поэтому, вероятно, есть проблема с тем, как вы его создаете, но было бы неплохо это подтвердить.   -  person alexroussos    schedule 19.08.2015
comment
Я добавил свой материал cmdline   -  person Neil    schedule 19.08.2015
comment
Все ваши поля находятся в состоянии "State": "RequiresIndexDocuments", но они должны быть State": "Active". Это должно быть решено путем переиндексации вашего домена -- aws cloudsearch index-documents --domain-name dev-purchxapp-com как описано в docs.aws.amazon.com/cloudsearch/latest/developerguide/ .   -  person alexroussos    schedule 19.08.2015
comment
Вы опубликуете это как ответ, чтобы я мог проголосовать за вас и принять его. Это сработало для меня, спасибо.   -  person Neil    schedule 19.08.2015
comment
Рад, что сработало! Добавил ответ   -  person alexroussos    schedule 19.08.2015


Ответы (1)


Вам необходимо повторно проиндексировать свой домен, чтобы сделать вновь добавленные поля активными. Сейчас они отображаются как "State": "RequiresIndexDocuments", но должны быть "State": "Active".

Запуск aws cloudsearch index-documents --domain-name dev-purchxapp-com должен решить проблему. Дополнительные сведения об индексации вашего домена см. на странице http://docs.aws.amazon.com/cloudsearch/latest/developerguide/indexing.html

person alexroussos    schedule 19.08.2015