AWS Cloudsearch正在上传文档

时间:2015-08-18 19:23:58

标签: ruby-on-rails json amazon-web-services amazon-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

当从rails控制台运行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

文档上传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)"] }

1 个答案:

答案 0 :(得分:1)

您需要重新编制域名索引,才能使新添加的字段处于活动状态。它们目前显示为"State": "RequiresIndexDocuments"但应位于"State": "Active"

运行aws cloudsearch index-documents --domain-name dev-purchxapp-com应解决问题。有关为您的域编制索引的详情,请参阅:http://docs.aws.amazon.com/cloudsearch/latest/developerguide/indexing.html