Last active
December 20, 2015 01:49
-
-
Save saihoooooooo/6051429 to your computer and use it in GitHub Desktop.
MySQL's field() function in Solr.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="UTF-8"?> | |
| <project name="custom_field_jar" default="jar" basedir="." > | |
| <target name="jar"> | |
| <jar basedir="./bin" jarfile="custom_field.jar" /> | |
| </target> | |
| </project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package jp.co.hogehoge.function; | |
| import java.io.IOException; | |
| import java.util.*; | |
| import org.apache.lucene.index.AtomicReaderContext; | |
| import org.apache.lucene.queries.function.FunctionValues; | |
| import org.apache.lucene.queries.function.ValueSource; | |
| import org.apache.lucene.queries.function.docvalues.IntDocValues; | |
| public class CustomFieldFunction extends ValueSource | |
| { | |
| protected final ValueSource source; | |
| protected final String[] strArr; | |
| public CustomFieldFunction(ValueSource source, String strArr[]) { | |
| this.source = source; | |
| this.strArr = strArr; | |
| } | |
| @Override | |
| public boolean equals(Object o) { | |
| return false; | |
| } | |
| @Override | |
| public int hashCode() { | |
| return 0; | |
| } | |
| @Override | |
| public String description() { | |
| return null; | |
| } | |
| @Override | |
| public FunctionValues getValues(Map context, AtomicReaderContext readerContext) throws IOException { | |
| final FunctionValues sval = source.getValues(context, readerContext); | |
| final String[] haystack = strArr; | |
| return new IntDocValues(this) { | |
| @Override | |
| public int intVal(int doc) { | |
| int result = 0; | |
| String needles = sval.strVal(doc); | |
| for (int i = 0; i < haystack.length; i++) { | |
| if (haystack[i].equals(needles)) { | |
| result = haystack.length - i; | |
| break; | |
| } | |
| } | |
| return result; | |
| } | |
| }; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package jp.co.hogehoge.parser; | |
| import java.util.*; | |
| import jp.co.hogehoge.function.CustomFieldFunction; | |
| import org.apache.lucene.queries.function.ValueSource; | |
| import org.apache.solr.common.util.NamedList; | |
| import org.apache.solr.search.FunctionQParser; | |
| import org.apache.solr.search.SyntaxError; | |
| import org.apache.solr.search.ValueSourceParser; | |
| public class CustomFieldValueSourceParser extends ValueSourceParser | |
| { | |
| public void init(NamedList namedList) { | |
| } | |
| public ValueSource parse(FunctionQParser fqp) throws SyntaxError { | |
| ValueSource source = fqp.parseValueSource(); | |
| List<String> list = new ArrayList<String>(); | |
| while (fqp.hasMoreArguments()) { | |
| list.add(fqp.parseArg()); | |
| } | |
| String [] strArr = list.toArray(new String[list.size()]); | |
| return new CustomFieldFunction(source, strArr); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi