当前位置: 首页 > 面试题库 >

如何根据来源匹配在Elasticsearch中获得准确的金额?

顾文昌
2023-03-14
问题内容

我如何在Elasticsearch中获得确切的总和?前参考我目前正在使用elasticsearch 5.6,我的索引映射如下所示:

{
  "my-index":{
    "mappings":{
      "my-type":{
        "properties":{
          "id":{
            "type":"keyword"
          },
          "fieldA":{
            "type":"double"
          },
          "fieldB":{
            "type":"double"
          },
          "fieldC":{
            "type":"double"
          },
          "version":{
            "type":"long"
          }
        }
      }
    }
  }
}

生成的搜索查询(使用Java客户端)为:

{
 /// ... some filters here
 "aggregations" : {
       "fieldA" : {
         "sum" : {
           "field" : "fieldA"
         }
       },
       "fieldB" : {
         "sum" : {
           "field" : "fieldB"
         }
       },
       "fieldC" : {
         "sum" : {
           "field" : "fieldC"
         }
       }
     }
}

但是,我的结果点击产生以下内容:

{
    "took": 10,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 5,
        "max_score": 3.8466966,
        "hits": [
            {
                "_index": "my-index",
                "_type": "my-type",
                "_id": "25a203b63e264fd2be13db006684b06d",
                "_score": 3.8466966,
                "_source": {
                    "fieldC": 108,
                    "fieldA": 108,
                    "fieldB": 0
                }
            },
            {
                "_index": "my-index",
                "_type": "my-type",
                "_id": "25a203b63e264fd2be13db006684b06d",
                "_score": 3.8466966,
                "_source": {
                    "fieldC": -36,
                    "fieldA": 108,
                    "fieldB": 144
                }
            },
            {
                "_index": "my-index",
                "_type": "my-type",
                "_id": "25a203b63e264fd2be13db006684b06d",
                "_score": 3.8466966,
                "_source": {
                    "fieldC": -7.2,
                    "fieldA": 1.8,
                    "fieldB": 9
                }
            },
            {
                "_index": "my-index",
                "_type": "my-type",
                "_id": "25a203b63e264fd2be13db006684b06d",
                "_score": 3.8466966,
                "_source": {
                    "fieldC": 14.85,
                    "fieldA": 18.9,
                    "fieldB": 4.05
                }
            },
            {
                "_index": "my-index",
                "_type": "my-type",
                "_id": "25a203b63e264fd2be13db006684b06d",
                "_score": 3.8466966,
                "_source": {
                    "fieldC": 36,
                    "fieldA": 36,
                    "fieldB": 0
                }
            }
        ]
    },
    "aggregations": {
        "fieldA": {
            "value": 272.70000000000005
        },
        "fieldB": {
            "value": 157.05
        },
        "fieldC": {
            "value": 115.64999999999999
        }
    }
}

为什么我得到:

115.64999999999999而不是字段C中的115.65 272.70000000000005而不是字段A中的272.7

我应该使用float而不是double?还是有一种方法可以更改查询而无需使用无痛脚本并使用具有指定精度和舍入模式的Java BigDecimal?


问题答案:

这有两种检查方法:

node答:如果安装了node.js,只需在提示符下键入,然后输入所有fieldA值的总和:

 $ node
 108 - 36 - 7.2 + 14.85 + 36
 115.64999999999999            <--- this is the answer

B.打开浏览器的开发人员工具,然后选择“控制台”视图。然后输入与上述相同的和:

 > 108-36-7.2+14.85+36
 < 115.64999999999999

如您所见,这两个结果都与您在ES响应中看到的一致。

规避方法之一就是存储你的号码既可以作为正常的整数(即1485,而不是14.85,3600代替36等)或scaled_floatscalingfactor 100(或更大的取决于精度则需要)



 类似资料:
  • 我使用php for binary documents(fscrawler)实现了elasticsearch。它在默认设置下工作得很好。我可以在文档中搜索我想要的单词,并得到不区分大小写的结果。然而,我现在想做精确匹配,即在当前搜索的顶部,如果查询被括在引号中,我想得到结果,只匹配查询完全…甚至区分大小写。 我的映射如下所示: 对于完全匹配(不起作用): 内容字段是文档的主体。如何实现内容字段中特

  • 问题内容: 我想在一个字段中搜索“ vision”项目,但是通过在DSL中使用match / match_phrace / term,我只得到了“ vision A”,“ vision B”,“ xx版本”,“ vision”等结果。 我想要的是精确匹配“视觉”应该获得最高分,而包含“视觉”的项目应该排在精确匹配之后。排名应该是: 我检查了Elasticsearch匹配精确术语其中识别出将“索引”

  • 问题内容: 假设在我的Elasticsearch索引中,我有一个名为“点”的字段,其中将包含由标点符号分隔的字符串(例如“ first.second.third”)。 我需要搜索例如“ first.second”,然后获取其“点”字段包含正好是“ first.second”或以“ first.second”开头的字符串的所有条目。 我在理解文本查询的工作方式时遇到问题,至少我无法创建执行此任务的查

  • 我需要使用REST保证Jsonpath根据匹配标准获取计数值 我尝试了以下方法,但不起作用: JSON:

  • 问题内容: 如何在Node.js中获得最准确的时间戳? ps我的Node.js版本是0.8.X,而node-microtime扩展名对我不起作用(安装时崩溃) 问题答案: ?这给您一个以毫秒为单位的时间戳,这是JS给您的最准确的时间戳。 更新:正如vaughan所述,它可以在Node.js中使用- 它的分辨率为纳秒,因此它的分辨率更高,这并不意味着它必须更精确。 PS .:为了更清楚一点,返回一个

  • 问题内容: 我有一个类似于http://drive.google.com的链接,并且我想在该链接之外匹配“ google”。 我有: 但这仅在整个文本为“ google”时才匹配(不区分大小写,因此也匹配Google或GooGlE等)。如何匹配另一个字符串中的“ google”? 问题答案: 关键是您使用的ElasticSearch正则表达式需要 完整的字符串匹配 : Lucene的模式总是锚定的