使用脚本进行弹性搜索时,每个文档的嵌套数组中具有两个对象的比较

时间:2019-04-06 17:52:35

标签: elasticsearch

我在elasticsearch中有一个场景,我需要比较两个嵌套在嵌套数组中的对象与弹性搜索中的文档,我在弹性搜索中存储了这样的json

{
    "_index": "index",
    "_type": "data",
    "_id": "stream+0+19",
    "_score": 1,
    "_source": {
        "objects": [
            {
                "value": 32.8,
                "Id": "5c73db4e7a0000b0ca3f0d89"
            },
            {
                "value": 39.7,
                "Id": "5ca1b924b400006dcf585f82",
            }
        ]
    }
},
{
    "_index": "index",
    "_type": "data",
    "_id": "stream+0+2",
    "_score": 1,
    "_source": {
        "objects": [
            {
                "value": 42.8,
                "Id": "5c73db4e7a0000b0ca3f0d89"
            },
            {
                "value": 39.7,
                "Id": "5ca1b924b400006dcf585f82"
            },
            {
                "value": 19.7,
                "Id": "5ca1b924b400006dcf585f867"
            }
        ]
    }
}

现在在每个文档的对象数组中,我需要比较两个对象的值,例如,我们需要发现如果它们在嵌套数组中存在两个对象,则对象1的ID为5c73db4e7a0000b0ca3f0d89,对象2的ID为5ca1b924b400006dcf585f82并且对象1(32.8)的值小于对象2(39.7)的值

直到现在我已经尝试过这些东西

试图使用for循环在无痛脚本中遍历对象数组,但数组的长度始终为1

POST _index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "query": {
              "term": {
                "objects.Id": {
                  "value": "5c73db4e7a0000b0ca3f0d89",
                  "boost": 1
                }
              }
            },
            "path": "objects",
            "ignore_unmapped": false,
            "score_mode": "none",
            "boost": 1
          }
        },
        {
          "bool": {
            "must": [
              {
                "nested": {
                  "query": {
                    "term": {
                      "objects.Id": {
                        "value": "5ca1b924b400006dcf585f82",
                        "boost": 1
                      }
                    }
                  },
                  "path": "objects",
                  "ignore_unmapped": false,
                  "score_mode": "none",
                  "boost": 1
                }
              }
            ],
            "adjust_pure_negative": true,
            "boost": 1
          }
        },
        {
          "bool": {
            "must": [
              {
                "nested": {
                  "query": {
                    "script": {
                      "script": {
                        "source": """
    double a = 0.0;
    double b = 0.0;
    for (int i = 0; i < doc['objects.Id'].length; ++i) {
    if(doc['objects.Id'][i] == '5c73db4e7a0000b0ca3f0d89') {
    a = doc['objects.value'][i];
    }
    if(doc['objects.Id'][i] == '5ca1b924b400006dcf585f82') {
    b = doc['objects.Value'][i];
    }
    }
    return a < b && a!=0.0 && b!=0.0;
"""
                      },
                      "boost": 1
                    }
                  },
                  "path": "objects",
                  "ignore_unmapped": false,
                  "score_mode": "none",
                  "boost": 1
                }
              }
            ],
            "adjust_pure_negative": true,
            "boost": 1
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  }
} 

实现此功能的最佳方法是什么,将不胜感激

0 个答案:

没有答案