Merged revision(s) 1665723 from lucene/dev/branches/branch_5x:

SOLR-7228: expose Analyzer for AbstractSpatialPrefixTreeFieldType


git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1665724 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Ryan McKinley 2015-03-10 22:13:10 +00:00
parent 1c4c0114dd
commit 09bd503fb2
3 changed files with 58 additions and 1 deletions

View File

@ -17,14 +17,22 @@ package org.apache.solr.schema;
* limitations under the License.
*/
import java.io.IOException;
import java.io.Reader;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.core.KeywordTokenizer;
import org.apache.lucene.spatial.prefix.PrefixTreeStrategy;
import org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree;
import org.apache.lucene.spatial.prefix.tree.SpatialPrefixTreeFactory;
import org.apache.lucene.spatial.query.SpatialArgsParser;
import org.apache.solr.util.MapListener;
import com.spatial4j.core.shape.Shape;
/**
* @see PrefixTreeStrategy
* @lucene.experimental
@ -63,6 +71,39 @@ public abstract class AbstractSpatialPrefixTreeFieldType<T extends PrefixTreeStr
defaultFieldValuesArrayLen = Integer.valueOf(v);
}
/**
* This analyzer is not actually used for indexing. It is implemented here
* so that the analysis UI will show reasonable tokens.
*/
@Override
public Analyzer getIndexAnalyzer()
{
return new Analyzer() {
@Override
protected TokenStreamComponents createComponents(final String fieldName) {
return new TokenStreamComponents(new KeywordTokenizer()) {
private Shape shape = null;
protected void setReader(final Reader reader) throws IOException {
source.setReader(reader);
shape = parseShape(IOUtils.toString(reader));
}
public TokenStream getTokenStream() {
PrefixTreeStrategy s = newSpatialStrategy(fieldName==null ? getTypeName() : fieldName);
return s.createIndexableFields(shape)[0].tokenStreamValue();
}
};
}
};
}
@Override
public Analyzer getQueryAnalyzer()
{
return getIndexAnalyzer();
}
@Override
protected T newSpatialStrategy(String fieldName) {

View File

@ -86,6 +86,8 @@
<fieldType name="tdatedv" class="solr.TrieDateField" precisionStep="6" docValues="true" multiValued="true"/>
<fieldType name="dateRange" class="solr.DateRangeField" />
<fieldType name="location_rpt" class="solr.SpatialRecursivePrefixTreeFieldType"
geo="true" distErrPct="0.025" maxDistErr="0.001" distanceUnits="kilometers" />
<!-- solr.TextField allows the specification of custom
text analyzers specified as a tokenizer and a list

View File

@ -411,4 +411,18 @@ public class FieldAnalysisRequestHandlerTest extends AnalysisRequestHandlerTestB
assertToken(tokenList.get(4), new TokenInfo("a", null, "word", 12, 13, 4, new int[]{3,4,4}, null, false));
assertToken(tokenList.get(5), new TokenInfo("test", null, "word", 14, 18, 5, new int[]{4,5,5}, null, false));
}
@Test
public void testSpatial() throws Exception {
FieldAnalysisRequest request = new FieldAnalysisRequest();
request.addFieldType("location_rpt");
request.setFieldValue("MULTIPOINT ((10 40), (40 30), (20 20), (30 10))");
NamedList<NamedList> result = handler.handleAnalysisRequest(request, h.getCore().getLatestSchema());
NamedList<List<NamedList>> tokens = (NamedList<List<NamedList>>)
((NamedList)result.get("field_types").get("location_rpt")).get("index");
List<NamedList> tokenList = tokens.get("org.apache.lucene.spatial.prefix.BytesRefIteratorTokenStream");
assertEquals("s", tokenList.get(0).get("text") );
}
}