elasticsearch 弹性无痛多值物体

t30tvxxf  于 11个月前  发布在  ElasticSearch
关注(0)|答案(2)|浏览(100)

我想做一个轻松的脚本,它将得到由 an 过滤的max groupe_an 值。我已经按如下方式索引,没有嵌套。然后我在数组中添加多值数据。但不知何故,我失去了轻松脚本中两个字段列表中顺序的一致性。使用索引k并不好,我只是在做实验。我想在这些物体上画上句号,但找不到方法,比如

for (item in doc['micro.ans'])

字符串
但我得到

"caused_by" : {
    "type" : "illegal_argument_exception",
    "reason" : "Extraneous for each loop."
  }


我可能需要不同的索引。在索引和无痛脚本方面,什么是好的解决方案?谢谢。

PUT test-500
    {
      "mappings": {
        "properties": {
          "micro" : {
            "properties": {
              "ans" : {
                "properties" : {
                  "an" : {
                    "type" : "keyword"
                  },
                  "groupe_an" : {
                    "type": "float"
                  }
                }
              }
            }
          }
        }
      }
    }

    PUT test-500/_doc/1
    {
      "micro" : {
        "ans" : [
          {"an": "686660", "groupe_an" : 14.0},
          {"an": "439750", "groupe_an" : 6.0}
        ]
      }
    }      

    PUT test-500/_doc/2
    {
      "micro" : {
        "ans" : [
          {"an": "286660", "groupe_an" : 100.0},
          {"an": "239750", "groupe_an" : 200.0}
        ]
      }
    }

    GET test-500/_search
    {
      "query": {
        "script_score" : {
          "query" : {
            "match_all": {}
          },
          "script" : {
            "source" : """
              double max = 0;
              int k=0;
              List ans = doc['micro.ans.an'];
              List groupe_ans = doc['micro.ans.groupe_an'];
              for (an in ans) {
                if (an == params.queried_an && groupe_ans[k]>max) {
                  max=groupe_ans[k]} k=k+1
              }
              return max
            """,
            "lang" : "painless",
            "params" : {
              "queried_an" : "239750"
            }
          }
        }
      }
    }

7kjnsjlb

7kjnsjlb1#

Tldr;

你不能。这是因为数组对象是扁平的
据我所知,订单没有保证。

解决方案?

去规范化

你能为数组中的每个元素创建一个文档吗?

嵌套字段?

PUT 77688728-2
{
  "mappings": {
    "properties": {
      "micro" : {
        "properties": {
          "ans" : {
            "type": "nested",
            "properties" : {
              "an" : {
                "type" : "keyword"
              },
              "groupe_an" : {
                "type": "float"
              }
            }
          }
        }
      }
    }
  }
}

PUT 77688728-2/_doc/1
{
  "micro" : {
    "ans" : [
      {"an": "686660", "groupe_an" : 14.0},
      {"an": "439750", "groupe_an" : 6.0}
    ]
  }
}      

PUT 77688728-2/_doc/2
{
  "micro" : {
    "ans" : [
      {"an": "286660", "groupe_an" : 100.0},
      {"an": "239750", "groupe_an" : 200.0}
    ]
  }
}

GET 77688728-2/_search
{
  "query": {
    "nested": {
      "inner_hits": {},
      "path": "micro.ans",
      "query": {
        "script_score": {
          "query": {
            "match_all": {}
          },
          "script": {
            "source": """
        double top = 0;
        int k=0;
        List ans = doc['micro.ans.an'];
        List groupe_ans = doc['micro.ans.groupe_an'];
        for (an in ans)
        {
          if (an == params.queried_an && groupe_ans[k]>top) 
          {
            top=groupe_ans[k];
          }
          k=k+1;
          
        }
        return top;
        """,
            "lang": "painless",
            "params": {
              "queried_an": "239750"
            }
          }
        }
      }
    }
  }
}

字符串

htzpubme

htzpubme2#

最后,我将两个字段连接成一个关键字类型的字段:

PUT test-2003
    {
      "mappings": {
        "properties": {
          "micro" : {
            "properties": {
              "an_groupe" : {
                "type" : "keyword"
              }
            }
          }
        }
      }
    }

    PUT test-2003/_doc/1
    {
      "micro" : {
        "an_groupe" : [
          "686660|14.0",
          "439750|6.0"
        ]
      }
    }      

    PUT test-2003/_doc/2
    {
      "micro" : {
        "an_groupe" : [
          "68666022|100.0",
          "43975022|200.0"
        ]
      }
    }

    GET test-2003/_search
    {
      "query": {
        "script_score" : {
          "query" : {
            "match_all": {}
          },
          "script" : {
            "source" : """
              double max = 0;
              for (an_groupe in doc['micro.an_groupe']) {
                String[] splitted = an8_groupe.splitOnToken("|");
                double groupe_val = Double.parseDouble(splitted[1]);
                for (queried_an in params.queried_ans) {
                  if (splitted[0] == queried_an && groupe_val > max) {
                    max = groupe_val
                  }
                }
              }
              return max
            """,
            "lang" : "painless",
            "params" : {
              "queried_ans" : ["68666022", "43975022", "439750"]
            }
          }
        }
      }
    }

字符串

相关问题