通过嵌套数组执行循环的ElasticSearch查询

webghufk  于 2022-10-06  发布在  ElasticSearch
关注(0)|答案(1)|浏览(247)

ElasticSearch v7.0

大家好,日安!

我正在尝试创建具有子属性(嵌套类型)、db_name和Contact_Full_Name的索引查询。我有一些单据的db_name值与主单的db_name字段不同。因此,我只想获取具有相同db_name计数>0的子项的文档

我曾尝试循环下子字段,但出现错误所有分片都失败谁能帮我找出原因?

索引Map:

PUT /test_dup_contacts_new
{
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "properties": {
      "con_full_name": { "type": "text" },
      "db_name": { "type": "text" },
      "child": { 
            "type": "nested",
            "properties":{
                "con_full_name": { "type": "text" },
                "db_name": { "type": "text" }
            } 
        }
    }
  }
}

文件:

[
      {
        "_index" : "test_dup_contacts_new",
        "_type" : "_doc",
        "_id" : "3HwmhIMBS7IAsS38K8vg",
        "_score" : 1.0,
        "_source" : {
          "con_full_name" : "Jesse Grinstead SirManBoy",
          "db_name" : "bc483ac37173a45849add211047a6dd7",
          "child" : [
            {
              "con_full_name" : "Jesse child 1",
              "db_name" : "bc483ac37173a45849add211047a6dd7"
            },
            {
              "con_full_name" : "Jesse child 2",
              "db_name" : "bc483ac37173a45849add211047a6dd7"
            }
          ]
        }
      },
      {
        "_index" : "test_dup_contacts_new",
        "_type" : "_doc",
        "_id" : "3XwmhIMBS7IAsS388MtR",
        "_score" : 1.0,
        "_source" : {
          "con_full_name" : "Jesse Grinstead",
          "db_name" : "bc483ac37173a45849add211047a6dd7",
          "child" : [
            {
              "con_full_name" : "Grinstead child 1",
              "db_name" : "bc483ac37173a45849add211047a6dd7"
            },
            {
              "con_full_name" : "Grinstead child 2",
              "db_name" : "bc483ac37173a45849add211047a6dd7"
            }
          ]
        }
      },
      {
        "_index" : "test_dup_contacts_new",
        "_type" : "_doc",
        "_id" : "3nw9hIMBS7IAsS38LMsf",
        "_score" : 1.0,
        "_source" : {
          "con_full_name" : "Test Dev",
          "db_name" : "bc483ac37173a45849add211047a6dd7",
          "child" : [
            {
              "con_full_name" : "Grinstead child 1",
              "db_name" : "test"
            },
            {
              "con_full_name" : "Grinstead child 2",
              "db_name" : "bc483ac37173a45849add211047a6dd7"
            }
          ]
        }
      },
      {
        "_index" : "test_dup_contacts_new",
        "_type" : "_doc",
        "_id" : "33xHhIMBS7IAsS386ctL",
        "_score" : 1.0,
        "_source" : {
          "con_full_name" : "Test Dev",
          "db_name" : "bc483ac37173a45849add211047a6dd7",
          "child" : [
            {
              "con_full_name" : "Grinstead child 1",
              "db_name" : "test"
            }
          ]
        }
      },
      {
        "_index" : "test_dup_contacts_new",
        "_type" : "_doc",
        "_id" : "4HxjiYMBS7IAsS38w8sU",
        "_score" : 1.0,
        "_source" : {
          "con_full_name" : "devid miller",
          "db_name" : "bc483ac37173a45849add211047a6dd7",
          "child" : [
            {
              "con_full_name" : "devid child 1",
              "db_name" : "test_db"
            },
            {
              "con_full_name" : "devid child 2",
              "db_name" : "bc483ac37173a45849add211047a6dd7"
            },
            {
              "con_full_name" : "devid child 3",
              "db_name" : "test_db"
            },
            {
              "con_full_name" : "devid child 4",
              "db_name" : "test_db"
            }
          ]
        }
      },
      {
        "_index" : "test_dup_contacts_new",
        "_type" : "_doc",
        "_id" : "43xliYMBS7IAsS38KMsW",
        "_score" : 1.0,
        "_source" : {
          "con_full_name" : "Snu dev",
          "db_name" : "test_db",
          "child" : [
            {
              "con_full_name" : "Snu child 1",
              "db_name" : "test_db"
            }
          ]
        }
      }
    ]

查询不成功/失败:

GET test_dup_contacts_new/_search
{
  "query": {
    "bool": {
      "filter":{
      "script":{
      "script": {
        "lang": "painless",
        "source": "def num = 0; for (int i=0; i<params._source['child'].length; i++){num++;} if(num>0){ return true;} else{ return false;}"

      }
    }
    }
    } 

  }
}

误差率

"error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "runtime error",
        "script_stack": [
          "i=0; i<params._source['child'].length; i++){",
          "             ^---- HERE"
        ],
        "script": "def num = 0; for (int i=0; i<params._source['child'].length; i++){num++;} if(num>0){ return true;} else{ return false;}",
        "lang": "painless"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",

提前谢谢!

8hhllhi2

8hhllhi21#

请检查Val和Pierre Mallet给出答案的this question,我认为这与您正在寻找的相似。我会建议使用Val Answer,他建议在索引时添加字段,在您的场景中,如果db_name在嵌套字段中可用,您可以有存储该字段的字段。

如果您仍想使用脚本,则以下是适用于您的用例的脚本:(但不建议这样做,因为它会影响查询性能)

POST test1/_search
{
  "min_score": 0.1,
  "query": {
    "function_score": {
      "query": {
        "match_all": {}
      },
      "functions": [
        {
          "script_score": {
            "script": {
              "source": """
              for (int i=0; i<params["_source"]["child"].length; i++){
                def childVal = params._source["child"].get(i);
                if(doc['db_name.keyword'].value==childVal.db_name){
                  return 1;
                }
              }
              return 0;
"""
            }
          }
        }
      ]
    }
  }
}

此外,您还需要更改索引Map并配置keyword字段类型以使用脚本。

"mappings": {
      "properties": {
        "child": {
          "type": "nested",
          "properties": {
            "con_full_name": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword"
                }
              }
            },
            "db_name": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword"
                }
              }
            }
          }
        },
        "con_full_name": {
          "type": "text"
        },
        "db_name": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword"
            }
          }
        }
      }
    }

相关问题