👋

ElasticSearch(OpenSearch)の動的並べ替え【雑メモ】

2024/02/12に公開
  • ユーザの条件に応じてポイント計算
  • sortでscript書ける
  • paramsを渡せる
  • painless言語(ほぼJavaらしい)

      sort: {
        "_script": {
          "script": {
            "source": """
              def source = params['_source'];
              int point = source['basic_recommend_score'];
              List category_codes = source['corporation_category_codes'];
              List tdfk_codes = source['job_tdfk_codes'];
              if (category_codes.contains(params.industry_1)) {
                  point += 3;
              }
              if (category_codes.contains(params.industry_2)) {
                  point += 2;
              }
              if (category_codes.contains(params.industry_3)) {
                  point += 1;
              }
              if (tdfk_codes.contains(params.location_1)) {
                  point += 3;
              }
              if (tdfk_codes.contains(params.location_2)) {
                  point += 2;
              }
              if (tdfk_codes.contains(params.location_3)) {
                  point += 1;
              }
              return point;
            """,
            "lang": "painless",
             "params": {
              "industry_1": desired_condition.industry_type1 || 0,
              "industry_2": desired_condition.industry_type2 || 0,
              "industry_3": desired_condition.industry_type3 || 0,
              "occupation_1": desired_condition.category_type1 || 0,
              "occupation_2": desired_condition.category_type2 || 0,
              "occupation_3": desired_condition.category_type3 || 0,
            }
          },
          "type": "number",
          "order": "desc"
        }
      }

Discussion