Hi guys, I've been almost 1 month trying to migrate a plugin developed in 5.4 from elasticsearch to 6.6. What I want is to establish different sources of which in each one the dates entered are verified to show the available calendar or events or events that have not yet expired.
Then an abstract method was created for each of the scripts by setting the "NativeScriptFactory" and.
Plugin:
import org.devmaster.elasticsearch.index.mapper.RecurringFieldMapper;
import org.devmaster.elasticsearch.script.*;
import org.elasticsearch.index.mapper.Mapper;
import org.elasticsearch.plugins.MapperPlugin;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.plugins.ScriptPlugin;
import org.elasticsearch.script.NativeScriptFactory;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
public class RecurringPlugin extends Plugin implements MapperPlugin, ScriptPlugin {
@Override
public Map<String, Mapper.TypeParser> getMappers() {
return Collections.singletonMap(RecurringFieldMapper.CONTENT_TYPE, new RecurringFieldMapper.TypeParser());
}
@Override
public List<NativeScriptFactory> getNativeScripts() {
return Arrays.asList(new NextOccurrenceSearchScript.Factory(),
new HasOccurrencesAtSearchScript.Factory(),
new OccurBetweenSearchScript.Factory(),
new NotHasExpiredSearchScript.Factory(),
new OccurrencesBetweenSearchScript.Factory(),
new HasAnyOccurrenceBetweenSearchScript.Factory());
}
}
Factory
import org.devmaster.elasticsearch.index.mapper.Recurring;
import org.elasticsearch.script.ScriptException;
import org.joda.time.LocalDate;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Map;
public class HasAnyOccurrenceBetweenSearchScript extends AbstractRecurringSearchScript {
public static final String SCRIPT_NAME = "hasAnyOccurrenceBetween";
private static final String PARAM_FIELD = "field";
private static final String PARAM_START = "start";
private static final String PARAM_END = "end";
protected HasAnyOccurrenceBetweenSearchScript(Map<String, String> paramMap) {
super(paramMap);
}
public static class Factory extends AbstractFactory<HasAnyOccurrenceBetweenSearchScript> {
public Factory() {
super(HasAnyOccurrenceBetweenSearchScript.class, Arrays.asList(PARAM_FIELD, PARAM_START, PARAM_END));
}
@Override
public String getName() {
return SCRIPT_NAME;
}
}
@Override
public Object run() {
Recurring recurring = getRecurring(getParamValueFor(PARAM_FIELD));
String startDate = getParamValueFor(PARAM_START);
String endDate = getParamValueFor(PARAM_END);
try {
return recurring != null && recurring.hasAnyOccurrenceBetween(startDate, endDate);
} catch (ParseException e) {
throw newScriptException("Error while obtaining has any occurrence between.", e, SCRIPT_NAME);
}
}
}
So I can not find a way to set these changes to 6.6 I tried to set it up under compile and verify the sources of each one. so I do not know if it's the correct way or what?
@Override
public <T> T compile(String scriptName, String scriptSource, ScriptContext<T> context, Map<String, String> params) {
if (context.equals(ScoreScript.CONTEXT) == false && context.equals(FilterScript.CONTEXT) == false) {
throw new IllegalArgumentException(getType() + " scripts cannot be used for context [" + context.name + "]");
}
// we use the script "source" as the script identifier
if ("hasAnyOccurrenceBetween".equals(scriptSource)) {
if (context.equals(SearchScript.CONTEXT) == true) {
ScoreScript.Factory factory = hasAnyOccurrenceBetweenScore::new;
return context.factoryClazz.cast(factory);
} else if (context.equals(FilterScript.CONTEXT) == true) {
FilterScript.Factory factory = (p, lookup) -> hasAnyOccurrenceBetweenFilter(p, lookup);
return context.factoryClazz.cast(factory);
}
}
throw new IllegalArgumentException("Unknown script name " + scriptSource);
}
One of the variants or problems is that the sources script varies to terms, filter, sort, so one of the problems is that the context varies and the verification gives the error alert in
if (context.equals(ScoreScript.CONTEXT) == false && context.equals(FilterScript.CONTEXT) == false) {
throw new IllegalArgumentException(getType() + " scripts cannot be used for context [" + context.name + "]");
}