提问者:小点点

尝试在OpenSearch中使用日期、月、年进行筛选(希望在java中转换无痛脚本)


            "filter" : [
                {
          "bool" : {
            "must" : [
              {
              "script": {
      "script": """
         int day ;
         int month ;
         int year;
         
      if ( (int) doc['decisionDate.decisionDay'].size() > 0 && (int) doc['decisionDate.decisionMonth'].size() > 0  && (int) doc['decisionDate.decisionYear'].size() > 0) {

      day = (int) doc['decisionDate.decisionDay'].value;
      month = (int) doc['decisionDate.decisionMonth'].value;
      year = (int) doc['decisionDate.decisionYear'].value;

    return
      ChronoUnit.DAYS.between(ZonedDateTime.of(year, month, day, 0, 0, 0, 0, ZoneId.of('Z')), 
      ZonedDateTime.of(2022, 12, 13, 0, 0, 0, 0, ZoneId.of('Z'))) > 0 && 
      ChronoUnit.DAYS.between(ZonedDateTime.of(2011, 03, 30, 0, 0, 0, 0, ZoneId.of('Z')), 
      ZonedDateTime.of( year,  month, day, 0, 0, 0, 0, ZoneId.of('Z'))) > 0 ; } """
              
    }
            }
            ]
          }
                }
              ]

上面的代码是OpenSearch中带有日,月,年的日期过滤器,我正在尝试将其转换为java中的脚本。我不知道它如何在 java 中转换

尝试使用插件'org.codelibs.fess:fess:14.4.0'

    final SearchEngineClient searchEngineClient = ComponentUtil.getSearchEngineClient();
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    final LabelTypeHelper labelTypeHelper = ComponentUtil.getLabelTypeHelper();
    final LanguageHelper languageHelper = ComponentUtil.getLanguageHelper();


    Script script = ComponentUtil.getLanguageHelper().createScript("here i need add the above (painless script), st);

共1个答案

匿名用户

这是opensearch中存储的脚本

POST/脚本/日期筛选器脚本{

             "script": {    
                "lang" : "painless",
      "source": """
         int day ;
         int month ;
         int year;
         
      if ( (int) doc['decisionDate.decisionDay'].size() != 0 ) {
      day = (int) doc['decisionDate.decisionDay'].value;
      }
   
      if ( (int) doc['decisionDate.decisionMonth'].size() != 0 ) {
      month = (int) doc['decisionDate.decisionMonth'].value;
      }
      
         if ( (int) doc['decisionDate.decisionYear'].size() != 0 ) {
      year = (int) doc['decisionDate.decisionYear'].value;
      }
   

      if (day >= 1 && month >= 1 && year >= 1) {
        return
      ChronoUnit.DAYS.between(ZonedDateTime.of(year, month, day, 0, 0, 0, 0, ZoneId.of('Z')), ZonedDateTime.of(params.year_end, params.month_end, params.day_end, 0, 0, 0, 0, ZoneId.of('Z'))) > 0 && ChronoUnit.DAYS.between(ZonedDateTime.of(params.year_start, params.month_start, params.day_start, 0, 0, 0, 0, ZoneId.of('Z')), ZonedDateTime.of( year,  month, day, 0, 0, 0, 0, ZoneId.of('Z'))) > 0 ; } """
              
    }

}