在 OpenSearch 查询中,我想在必须查询中使用脚本

问题描述 投票:0回答:2

在返回任何相关文件或数据之前,我需要验证候选人的总经验是否为零。相反,如果总经验不为零,我想检索文档。尽管我尝试使用各种参数(例如 params._source.total_experiences、ctx._source.total_experiences 和 doc['total_experiences'])来访问此信息,但这些方法都没有产生所需的结果。看来检索此数据的机制在任何这些情况下都无法正常运行。因此,我寻求替代解决方案,以根据指定的标准有效地确定总经验值并对其采取行动。

这是我的询问

GET enriched_scraped_profiles/_search
{
  "_source": true,
  "size": 2000, 
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "experiences.industry": "Banking"
          }
        }
      ],
      "filter": {
        "script": {
          "script": {
            "lang": "painless",
            "source": """
                if (params['_source']['total_experiences'] != 0) {
                  return true;
                }
              return false;
            """
          }
        }
      }
    }
  }
}

但是我收到了这个错误:

{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "runtime error",
        "script_stack": [
          "if (params['_source']['total_experiences'] != 0) {\n                  ",
          "          ^---- HERE"
        ],
        "script": " ...",
        "lang": "painless",
        "position": {
          "offset": 27,
          "start": 17,
          "end": 86
        }
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "enriched_scraped_profiles",
        "node": "je5hZrUORgWk9EEmuHQ9rQ",
        "reason": {
          "type": "script_exception",
          "reason": "runtime error",
          "script_stack": [
            "if (params['_source']['total_experiences'] != 0) {\n                  ",
            "          ^---- HERE"
          ],
          "script": " ...",
          "lang": "painless",
          "position": {
            "offset": 27,
            "start": 17,
            "end": 86
          },
          "caused_by": {
            "type": "null_pointer_exception",
            "reason": "Cannot invoke \"Object.getClass()\" because \"callArgs[0]\" is null"
          }
        }
      }
    ]
  },
  "status": 400
}

这是文件

{
  "_index": "enriched_scraped_profiles",
  "_id": "mohammed-alyousef-mkt",
  "_score": 21.133842,
  "_source": {
    "country": "SA",
    "accomplishment_patents": [
      
    ],
    "occupation": "Digital Content Officer at Takamol Holding",
    "education": [
      {
        "starts_at": {
          "month": 1,
          "year": 2017,
          "day": 1
        },
        "degree_name": "Bachelor ",
        "school": "King Saud University",
        "logo_url": "https://media.licdn.com/dms/image/C560BAQGsF4tX875gZw/company-logo_400_400/0/1631357058699?e=1714003200&v=beta&t=Ik1pHNTQPvtRPwibnqchGGVHEKP5PZB_XyaHpQ-P6sU",
        "grade": null,
        "activities_and_societies": null,
        "description": null,
        "ends_at": {
          "month": 12,
          "year": 2021,
          "day": 31
        },
        "school_linkedin_profile_url": null,
        "field_of_study": "marketing"
      }
    ],
    "birth_date": null,
    "total_experiences": 2.48,
    "recommendations": [
      
    ],
    "skills": [
      "Twitter Ads",
      "Twitter Marketing",
      "Tiktok Marketing",
      "Snapchat Marketing",
      "Meta Marketing",
      "Google Ads",
      "الإدارة",
      "Digital Marketing",
      "Analytical Skills",
      "تسويق المنتجات",
      "Strategy",
      "Microsoft Excel",
      "Microsoft Office",
      "Teamwork",
      "Communication",
      "Hootsuite"
    ],
    "personal_numbers": [
      
    ],
    "state": null,
    "volunteer_work": [
      
    ],
    "profile_pic_url": "https://media.licdn.com/dms/image/D4D03AQEDGKNsFHwUEQ/profile-displayphoto-shrink_800_800/0/1701794487213?e=1711584000&v=beta&t=5ejzAgm5kocP07McKbs4h7_kWlT6tYIxWDbmjChhU2A",
    "connections": null,
    "candidate_gender": "Male",
    "last_updated": "24-01-2024",
    "similarly_named_profiles": [
      
    ],
    "accomplishment_courses": [
      
    ],
    "experiences": [
      {
        "starts_at": {
          "month": 2,
          "year": 2022,
          "day": 1
        },
        "logo_url": "https://media.licdn.com/dms/image/C4E0BAQEbKUtqr6OQhQ/company-logo_400_400/0/1645452491805/takamolholding_logo?e=1714003200&v=beta&t=v3C3LiMBXJiUqfxv7JYQdD_hjBGfv0q6I0jvb_FBLUo",
        "company_linkedin_profile_url": "https://www.linkedin.com/company/takamolholding/",
        "total_experience_months": 23,
        "description": null,
        "company": "Takamol Holding",
        "location": "Riyadh, Saudi Arabia",
        "ends_at": {
          "month": 12,
          "year": 2023,
          "day": 31
        },
        "title": "Digital Content Officer"
      },
      {
        "starts_at": {
          "month": 11,
          "year": 2021,
          "day": 1
        },
        "logo_url": "https://media.licdn.com/dms/image/C4E0BAQEbKUtqr6OQhQ/company-logo_400_400/0/1645452491805/takamolholding_logo?e=1714003200&v=beta&t=v3C3LiMBXJiUqfxv7JYQdD_hjBGfv0q6I0jvb_FBLUo",
        "company_linkedin_profile_url": "https://www.linkedin.com/company/takamolholding/",
        "total_experience_months": 4,
        "description": null,
        "company": "Takamol Holding",
        "location": "Saudi Arabia",
        "ends_at": {
          "month": 2,
          "year": 2022,
          "day": 28
        },
        "title": "Digital Marketing Intern"
      },
      {
        "starts_at": {
          "month": 9,
          "year": 2021,
          "day": 1
        },
        "logo_url": "https://media.licdn.com/dms/image/C4E0BAQEbKUtqr6OQhQ/company-logo_400_400/0/1645452491805/takamolholding_logo?e=1714003200&v=beta&t=v3C3LiMBXJiUqfxv7JYQdD_hjBGfv0q6I0jvb_FBLUo",
        "company_linkedin_profile_url": "https://www.linkedin.com/company/takamolholding/",
        "total_experience_months": 3,
        "description": null,
        "company": "Takamol Holding",
        "location": "Riyadh, Saudi Arabia",
        "ends_at": {
          "month": 11,
          "year": 2021,
          "day": 30
        },
        "title": "Internship Trainee"
      }
    ],
    "country_full_name": "Saudi Arabia",
    "full_name": "Mohammed Alyousef",
    "accomplishment_organisations": [
      
    ],
    "status": "ready for match",
    "note": "",
    "gender": "Male",
    "activity": [
      {
        "note": "",
        "status_id": 1,
        "stage": "screening",
        "status_change_time": "04-16-2024 13:18",
        "user_id": "Nawaf Alageel",
        "status": "not contacted"
      },
      {
        "note": "test",
        "status_id": 1,
        "stage": "screening",
        "status_change_time": "04-16-2024 13:18",
        "user_id": "Nawaf Alageel",
        "status": "not contacted"
      },
      {
        "note": "ما يبغى سيلز",
        "stage": "screening",
        "status_change_time": "04-16-2024 13:20",
        "user_id": "Nawaf Alageel",
        "status": "contacted"
      },
      {
        "note": "",
        "stage": "screening",
        "status_change_time": "04-16-2024 13:22",
        "user_id": "Nawaf Alageel",
        "status": "ready for match"
      }
    ],
    "city": "Riyadh Region",
    "Response Time": 97.1856210231781,
    "people_also_viewed": [
      {
        "summary": "Creative Copywriter | social media| Marketing Communication",
        "link": "https://www.linkedin.com/in/ruba-al-jumah-dmi-0561a61b0",
        "name": "Ruba   Al Jumah, DMI",
        "location": null
      },
      {
        "summary": "Digital Marketing| Social Media| Management |Sr.Project Manager at tact digital communication",
        "link": "https://www.linkedin.com/in/meshal-almeshkhas-979485187",
        "name": "Meshal AlMeshkhas",
        "location": null
      },
      {
        "summary": "Digital Marketing | Social Media Management & Ads | Media buying",
        "link": "https://www.linkedin.com/in/ali-alhudaithy-312957135",
        "name": "Ali Alhudaithy",
        "location": null
      },
      {
        "summary": "Communications | PR | Digital Media | Marketing | Corporate Identity & Branding",
        "link": "https://www.linkedin.com/in/muhammad-al-sulami",
        "name": "Mohammad Alsulami",
        "location": null
      },
      {
        "summary": "Media Communications Specialist",
        "link": "https://www.linkedin.com/in/lina-abuqayyan",
        "name": "Lina Abuqayyan",
        "location": null
      },
      {
        "summary": "Digital Marketing | Strategy | Paid ads | Social media",
        "link": "https://www.linkedin.com/in/sarah-binshuhayl-a08a20163",
        "name": "Sarah Binshuhayl ",
        "location": null
      },
      {
        "summary": "Marketing | Communication | Digital Marketing |copywriting | Campaigns management",
        "link": "https://www.linkedin.com/in/nawaf-alshehri443",
        "name": "Nawaf Alshehri ",
        "location": null
      },
      {
        "summary": "Data Analyst | Digital Marketing | Social Media Management | Campaigns | Project Management",
        "link": "https://www.linkedin.com/in/mona-alfozan-590502198",
        "name": "Mona Alfozan",
        "location": null
      },
      {
        "summary": "Digital Marketing | Branding | Communications",
        "link": "https://www.linkedin.com/in/afnan-al-mutairi",
        "name": "Afnan Almutairi",
        "location": null
      },
      {
        "summary": "Advertising Campaigns | Marketing | Communication | Marketer | Engagement Media Channel | Marketing Strategy | Digital Marketing",
        "link": "https://www.linkedin.com/in/abdullah-bin-jughayman-043536155",
        "name": "Abdullah Bin jughayman",
        "location": null
      }
    ],
    "inferred_salary": null,
    "background_cover_image_url": null,
    "industry": null,
    "follower_count": 658,
    "Source": "Proxycurl",
    "profile_link_url": "https://www.linkedin.com/in/mohammed-alyousef-mkt",
    "total_experience": 2.48,
    "accomplishment_publications": [
      
    ],
    "extra": null,
    "headline": "Digital Marketer | Social Media ",
    "public_identifier": "mohammed-alyousef-mkt",
    "first_name": "Mohammed",
    "summary": "A passionate and dedicated individual with a strong theoretical and practical knowledge in Marketing, looking forward to pursue an exciting new career that will broaden my horizons and increase my knowledge in the various fields of Digital Marketing. ",
    "languages": [
      
    ],
    "groups": [
      
    ],
    "last_name": "Alyousef",
    "certifications": [
      
    ],
    "stage": "screening",
    "activities": [
      
    ],
    "personal_emails": [
      
    ],
    "accomplishment_test_scores": [
      
    ],
    "interests": [
      
    ],
    "articles": [
      
    ],
    "accomplishment_projects": [
      
    ],
    "accomplishment_honors_awards": [
      
    ]
  }
}
elasticsearch opensearch
2个回答
0
投票

在脚本查询中,您应该使用

doc['field'].value
而不是
params[]

这是工作版本:

PUT test_enriched_scraped_profiles/_doc/1
{
  "total_experiences": 5
}

GET test_enriched_scraped_profiles/_search
{
  "size": 2000, 
  "query": {
    "bool": {
      "filter": {
        "script": {
          "script": {
            "lang": "painless",
            "source": """
                if (doc['total_experiences'].value != 0) {
                  return true;
                }
              return false;
            """
          }
        }
      }
    }
  }
}

查看以下文章了解语法和信息:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-script-query.html


0
投票

您的脚本可以替换为聚合

我的示例的映射

PUT /enriched_scraped_profiles
{
    "mappings": {
        "properties": {
            "full_name": {
                "type": "text",
                "fields": {
                    "raw": {
                        "type": "keyword"
                    }
                }
            },
            "experiences": {
                "type": "nested",
                "properties": {
                    "total_experience_months": {
                        "type": "integer"
                    },
                    "industry": {
                        "type": "keyword"
                    }
                }
            }
        }
    }
}

示例文档

PUT /enriched_scraped_profiles/_bulk
{"create":{"_id":"mohammed-alyousef-mkt"}}
{"full_name":"Mohammed Alyousef","experiences":[{"industry":"Banking","total_experience_months":16},{"industry":"Banking","total_experience_months":4}]}

您修改后的查询

GET /enriched_scraped_profiles/_search?filter_path=aggregations
{
    "_source": true,
    "size": 2000,
    "query": {
        "bool": {
            "must": [
                {
                    "nested": {
                        "path": "experiences",
                        "query": {
                            "term": {
                                "experiences.industry": "Banking"
                            }
                        }
                    }
                }
            ]
        }
    },
    "aggs": {
        "by_name": {
            "terms": {
                "field": "full_name.raw",
                "size": 10
            },
            "aggs": {
                "inside_experience": {
                    "nested": {
                        "path": "experiences"
                    },
                    "aggs": {
                        "sum_total_experience_months": {
                            "sum": {
                                "field": "experiences.total_experience_months"
                            }
                        }
                    }
                },
                "experince_months_filter": {
                    "bucket_selector": {
                        "buckets_path": {
                            "sumTotalExperinceMonths": "inside_experience>sum_total_experience_months"
                        },
                        "script": """
                            (params.sumTotalExperinceMonths >= 12) && (params.sumTotalExperinceMonths <= 24)
                        """
                    }
                }
            }
        }
    }
}

experince_months_filter
是带有过滤条件的管道聚合

回应

{
    "aggregations" : {
        "by_name" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
                {
                    "key" : "Mohammed Alyousef",
                    "doc_count" : 1,
                    "inside_experience" : {
                        "doc_count" : 2,
                        "sum_total_experience_months" : {
                            "value" : 20.0
                        }
                    }
                }
            ]
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.