简体   繁体   English

AWS CloudSearch - 以JSON格式获取搜索结果

[英]AWS CloudSearch - Getting results of a search in JSON format

I am performing a search on my AWS CloudSearch domain from a Lambda function in node.js: 我正在从node.js中的Lambda函数在我的AWS CloudSearch域上执行搜索:

I uploaded a document such as this: 我上传了一个这样的文件:

         {
               “some_field”: “bla bla“,
               “some_date_field”: 1.466719E9,
               "number_field”: 4,
               “some_string”: "some long string blabla"
         }

And I perform a search like this 我执行这样的搜索

   var params = {
                  query: 'bla bla',
                };

    cloudsearchdomain.search(params, function(err, data) {

      if (err) {
        console.log(err, err.stack); // an error occurred
        context.fail(err); 
      } 
      else  {
        context.succeed(data);           // successful response
      }    

    });

The search works and as documented here CloudSearch returns document info in fields property of a hit. 搜索工作正如此处所述, CloudSearch 匹配的字段属性中返回文档信息。 Here is an example: 这是一个例子:

  {
   "status": {
   "timems": 2,
   "rid": “blabla”
  },
    "hits": {
       "found": 1,
       "start": 0,
       "hit": [
               {
                "id": “452545-49B4-45C3-B94F-43524542352-454352435.6666-8532-4099-xxxx-1",
                "fields": {
                   “some_field”: [
                     “bla bla“
                    ],
                   “some_date_field”: [
                     "1.466719E9"
                    ],
                   "number_field”: [
                      "4"
                    ],
                   “some_string”: [
                     "some long string blabla"
                   ],
             }
      }
   ]
 }
 }

As you can see all the fields are returned as strings in an array. 如您所见,所有字段都以数组中的字符串形式返回。 Is there anyway to get the results as a JSON that preserves the type of all the fields? 反正是否有结果作为JSON保留所有字段的类型?

After submitting a report about this to AWS I received this reply: 在向AWS提交有关此报告后,我收到了以下回复:

Hello, This is actually the intended behavior. 您好,这实际上是预期的行为。 The SDK team chose to implement the "fields" property as a dictionary of string keys and string-array values to maintain consistency across the various languages in which the AWS SDK exists. SDK团队选择将“fields”属性实现为字符串键和字符串数组值的字典,以保持AWS SDK所在的各种语言的一致性。 They place the responsibility for handling the various response formats (HTTP request vs. SDK method) on the client. 他们负责在客户端上处理各种响应格式(HTTP请求与SDK方法)。 For more details, please see: https://github.com/aws/aws-sdk-js/issues/791 有关详细信息,请参阅: https//github.com/aws/aws-sdk-js/issues/791

Unfortunately the only current solutions to the problem I describe above is: 不幸的是,我上面描述的问题的唯一解决方案是:

1) Create a parser that will parse the results as needed based on your expected response which takes into account your data types 1)创建一个解析器,根据您考虑数据类型的预期响应,根据需要解析结果

2) Add a new field to your cloudsearch index (text type) containing a stringified version of your entire json object/document. 2)向您的cloudsearch索引(文本类型)添加一个新字段,其中包含整个json对象/文档的字符串化版本。 You can then just use JSON.parse() on this to get the document in JSON format. 然后,您可以在此处使用JSON.parse()来获取JSON格式的文档。 This solution is not ideal because it adds an unnecessary chunk of text to your document but it proved a quick solution to my problem above. 这个解决方案并不理想,因为它会为您的文档添加不必要的文本块,但事实证明它是上述问题的快速解决方案。

I'd love to hear of any more solutions if anyone knows of any. 如果有人知道,我很想听到更多的解决方案。

CloudSearch does preserve the field type; CloudSearch 确实保留了字段类型; the results imply that you've configured these fields as arrays. 结果意味着您已将这些字段配置为数组。

You can confirm this by going to Indexing Options for your domain on the AWS web console. 您可以通过在AWS Web控制台上转到域的索引选项来确认这一点。 You should see fields that are text-array , literal-array , etc as in the screenshot below. 您应该看到text-arrayliteral-array等字段,如下面的屏幕截图所示。 Those will be returned as arrays. 这些将作为数组返回。 You can change them to non-array types if you will only ever be submitting a single value for each field in each document and you'll get back non-array values. 如果您只为每个文档中的每个字段提交单个值,则可以将它们更改为非数组类型,并且您将获得非数组值。 索引选项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM