2

Я пытаюсь загрузить объект json в Cloudsearch AWS. Вот весь мой код для создания индексных полей: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 побежал с рельсов консоли это показывает, что поля созданы, но когда я иду, чтобы загрузить документы, которые он говорит, что не может найти поля с различные имена полей, которые я определяю.

определения хэш, чтобы превратиться в 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 CLI:

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)"] } 
+0

1) Если это ваши настоящие ключи AWS, отредактируйте сообщение как можно скорее. 2) Проверьте веб-консоль AWS - видите ли вы свои поля? 3) То, что у вас здесь, довольно сложно, и это усложнит процесс отладки. Попробуйте упростить, например, определив жесткий код json (сгенерируйте его, распечатав json, который вы сейчас отправляете) и загрузите его через веб-консоль AWS - это работает? – alexroussos

+0

Спасибо, я отредактировал ключ и секрет. Прямо сейчас у меня нет доступа к веб-консоли AWS. Все, что мне дали, это ключ доступа и секрет для него. – Neil

+0

Вы можете проверить свою схему с помощью инструментов AWS CS CLI из http://docs.aws.amazon.com/cloudsearch/latest/developerguide/cloudsearch-command-line-tools.html, а затем запустить 'cs-describe-domain - -show-all'. Похоже, что в вашем индексе отсутствует куча полей, поэтому, вероятно, проблема с тем, как вы его создаете, но это будет еда, чтобы подтвердить это. – alexroussos

ответ

1

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

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

+0

Спасибо, это сработало для меня! – Neil

Смежные вопросы