AWS Cloudsearch上传文档

您所在的位置:网站首页 couchbase安装 AWS Cloudsearch上传文档

AWS Cloudsearch上传文档

2023-03-22 02:35| 来源: 网络整理| 查看: 265

我正在尝试将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

当从rails控制台运行create_index_字段时,它显示字段已创建,但当我去上传文档时,它表示找不到具有我定义的不同字段名的字段。

定义散列以转换为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

和文档上传rake任务:

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


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3