use of org.apache.solr.common.util.NamedList in project lucene-solr by apache.
the class FieldAnalysisRequestHandler method handleAnalysisRequest.
/**
* Handles the resolved analysis request and returns the analysis breakdown response as a named list.
*
* @param request The request to handle.
* @param schema The index schema.
*
* @return The analysis breakdown as a named list.
*/
protected NamedList<NamedList> handleAnalysisRequest(FieldAnalysisRequest request, IndexSchema schema) {
NamedList<NamedList> analysisResults = new SimpleOrderedMap<>();
NamedList<NamedList> fieldTypeAnalysisResults = new SimpleOrderedMap<>();
if (request.getFieldTypes() != null) {
for (String fieldTypeName : request.getFieldTypes()) {
FieldType fieldType = schema.getFieldTypes().get(fieldTypeName);
fieldTypeAnalysisResults.add(fieldTypeName, analyzeValues(request, fieldType, null));
}
}
NamedList<NamedList> fieldNameAnalysisResults = new SimpleOrderedMap<>();
if (request.getFieldNames() != null) {
for (String fieldName : request.getFieldNames()) {
FieldType fieldType = schema.getFieldType(fieldName);
fieldNameAnalysisResults.add(fieldName, analyzeValues(request, fieldType, fieldName));
}
}
analysisResults.add("field_types", fieldTypeAnalysisResults);
analysisResults.add("field_names", fieldNameAnalysisResults);
return analysisResults;
}
use of org.apache.solr.common.util.NamedList in project lucene-solr by apache.
the class AnalysisRequestHandlerBase method convertTokensToNamedLists.
/**
* Converts the list of Tokens to a list of NamedLists representing the tokens.
*
* @param tokenList Tokens to convert
* @param context The analysis context
*
* @return List of NamedLists containing the relevant information taken from the tokens
*/
private List<NamedList> convertTokensToNamedLists(final List<AttributeSource> tokenList, AnalysisContext context) {
final List<NamedList> tokensNamedLists = new ArrayList<>();
final FieldType fieldType = context.getFieldType();
final AttributeSource[] tokens = tokenList.toArray(new AttributeSource[tokenList.size()]);
// sort the tokens by absolute position
ArrayUtil.timSort(tokens, new Comparator<AttributeSource>() {
@Override
public int compare(AttributeSource a, AttributeSource b) {
return arrayCompare(a.getAttribute(TokenTrackingAttribute.class).getPositions(), b.getAttribute(TokenTrackingAttribute.class).getPositions());
}
private int arrayCompare(int[] a, int[] b) {
int p = 0;
final int stop = Math.min(a.length, b.length);
while (p < stop) {
int diff = a[p] - b[p];
if (diff != 0)
return diff;
p++;
}
// One is a prefix of the other, or, they are equal:
return a.length - b.length;
}
});
for (int i = 0; i < tokens.length; i++) {
AttributeSource token = tokens[i];
final NamedList<Object> tokenNamedList = new SimpleOrderedMap<>();
final TermToBytesRefAttribute termAtt = token.getAttribute(TermToBytesRefAttribute.class);
BytesRef rawBytes = termAtt.getBytesRef();
final String text = fieldType.indexedToReadable(rawBytes, new CharsRefBuilder()).toString();
tokenNamedList.add("text", text);
if (token.hasAttribute(CharTermAttribute.class)) {
final String rawText = token.getAttribute(CharTermAttribute.class).toString();
if (!rawText.equals(text)) {
tokenNamedList.add("raw_text", rawText);
}
}
tokenNamedList.add("raw_bytes", rawBytes.toString());
if (context.getTermsToMatch().contains(rawBytes)) {
tokenNamedList.add("match", true);
}
token.reflectWith(new AttributeReflector() {
@Override
public void reflect(Class<? extends Attribute> attClass, String key, Object value) {
// leave out position and bytes term
if (TermToBytesRefAttribute.class.isAssignableFrom(attClass))
return;
if (CharTermAttribute.class.isAssignableFrom(attClass))
return;
if (PositionIncrementAttribute.class.isAssignableFrom(attClass))
return;
String k = attClass.getName() + '#' + key;
// map keys for "standard attributes":
if (ATTRIBUTE_MAPPING.containsKey(k)) {
k = ATTRIBUTE_MAPPING.get(k);
}
if (value instanceof BytesRef) {
final BytesRef p = (BytesRef) value;
value = p.toString();
}
tokenNamedList.add(k, value);
}
});
tokensNamedLists.add(tokenNamedList);
}
return tokensNamedLists;
}
use of org.apache.solr.common.util.NamedList in project lucene-solr by apache.
the class DocumentAnalysisRequestHandler method handleAnalysisRequest.
/**
* Handles the resolved {@link DocumentAnalysisRequest} and returns the analysis response as a named list.
*
* @param request The {@link DocumentAnalysisRequest} to be handled.
* @param schema The index schema.
*
* @return The analysis response as a named list.
*/
NamedList<Object> handleAnalysisRequest(DocumentAnalysisRequest request, IndexSchema schema) {
SchemaField uniqueKeyField = schema.getUniqueKeyField();
NamedList<Object> result = new SimpleOrderedMap<>();
for (SolrInputDocument document : request.getDocuments()) {
NamedList<NamedList> theTokens = new SimpleOrderedMap<>();
result.add(document.getFieldValue(uniqueKeyField.getName()).toString(), theTokens);
for (String name : document.getFieldNames()) {
// there's no point of providing analysis to unindexed fields.
SchemaField field = schema.getField(name);
if (!field.indexed()) {
continue;
}
NamedList<Object> fieldTokens = new SimpleOrderedMap<>();
theTokens.add(name, fieldTokens);
FieldType fieldType = schema.getFieldType(name);
final String queryValue = request.getQuery();
Set<BytesRef> termsToMatch;
try {
termsToMatch = (queryValue != null && request.isShowMatch()) ? getQueryTokenSet(queryValue, fieldType.getQueryAnalyzer()) : EMPTY_BYTES_SET;
} catch (Exception e) {
// ignore analysis exceptions since we are applying arbitrary text to all fields
termsToMatch = EMPTY_BYTES_SET;
}
if (request.getQuery() != null) {
try {
AnalysisContext analysisContext = new AnalysisContext(fieldType, fieldType.getQueryAnalyzer(), EMPTY_BYTES_SET);
fieldTokens.add("query", analyzeValue(request.getQuery(), analysisContext));
} catch (Exception e) {
// ignore analysis exceptions since we are applying arbitrary text to all fields
}
}
Analyzer analyzer = fieldType.getIndexAnalyzer();
AnalysisContext analysisContext = new AnalysisContext(fieldType, analyzer, termsToMatch);
Collection<Object> fieldValues = document.getFieldValues(name);
NamedList<NamedList<? extends Object>> indexTokens = new SimpleOrderedMap<>();
for (Object fieldValue : fieldValues) {
indexTokens.add(String.valueOf(fieldValue), analyzeValue(fieldValue.toString(), analysisContext));
}
fieldTokens.add("index", indexTokens);
}
}
return result;
}
use of org.apache.solr.common.util.NamedList in project lucene-solr by apache.
the class DumpRequestHandler method handleRequestBody.
@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws IOException {
// Show params
rsp.add("params", req.getParams().toNamedList());
String[] parts = req.getParams().getParams("urlTemplateValues");
if (parts != null && parts.length > 0) {
Map map = new LinkedHashMap<>();
rsp.getValues().add("urlTemplateValues", map);
for (String part : parts) {
map.put(part, req.getPathTemplateValues().get(part));
}
}
String[] returnParams = req.getParams().getParams("param");
if (returnParams != null) {
NamedList params = (NamedList) rsp.getValues().get("params");
for (String returnParam : returnParams) {
String[] vals = req.getParams().getParams(returnParam);
if (vals != null) {
if (vals.length == 1) {
params.add(returnParam, vals[0]);
} else {
params.add(returnParam, vals);
}
}
}
}
if (req.getParams().getBool("getdefaults", false)) {
NamedList def = (NamedList) initArgs.get(PluginInfo.DEFAULTS);
rsp.add("getdefaults", def);
}
if (req.getParams().getBool("initArgs", false)) {
rsp.add("initArgs", initArgs);
}
// Write the streams...
if (req.getContentStreams() != null) {
ArrayList<NamedList<Object>> streams = new ArrayList<>();
// Cycle through each stream
for (ContentStream content : req.getContentStreams()) {
NamedList<Object> stream = new SimpleOrderedMap<>();
stream.add(NAME, content.getName());
stream.add("sourceInfo", content.getSourceInfo());
stream.add("size", content.getSize());
stream.add("contentType", content.getContentType());
Reader reader = content.getReader();
try {
stream.add("stream", IOUtils.toString(reader));
} finally {
reader.close();
}
streams.add(stream);
}
rsp.add("streams", streams);
}
rsp.add("context", req.getContext());
}
use of org.apache.solr.common.util.NamedList in project lucene-solr by apache.
the class AnalysisRequestHandlerBase method analyzeValue.
/**
* Analyzes the given value using the given Analyzer.
*
* @param value Value to analyze
* @param context The {@link AnalysisContext analysis context}.
*
* @return NamedList containing the tokens produced by analyzing the given value
*/
protected NamedList<? extends Object> analyzeValue(String value, AnalysisContext context) {
Analyzer analyzer = context.getAnalyzer();
if (!TokenizerChain.class.isInstance(analyzer)) {
try (TokenStream tokenStream = analyzer.tokenStream(context.getFieldName(), value)) {
NamedList<List<NamedList>> namedList = new NamedList<>();
namedList.add(tokenStream.getClass().getName(), convertTokensToNamedLists(analyzeTokenStream(tokenStream), context));
return namedList;
} catch (IOException e) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
}
}
TokenizerChain tokenizerChain = (TokenizerChain) analyzer;
CharFilterFactory[] cfiltfacs = tokenizerChain.getCharFilterFactories();
TokenizerFactory tfac = tokenizerChain.getTokenizerFactory();
TokenFilterFactory[] filtfacs = tokenizerChain.getTokenFilterFactories();
NamedList<Object> namedList = new NamedList<>();
if (0 < cfiltfacs.length) {
String source = value;
for (CharFilterFactory cfiltfac : cfiltfacs) {
Reader reader = new StringReader(source);
reader = cfiltfac.create(reader);
source = writeCharStream(namedList, reader);
}
}
TokenStream tokenStream = tfac.create();
((Tokenizer) tokenStream).setReader(tokenizerChain.initReader(null, new StringReader(value)));
List<AttributeSource> tokens = analyzeTokenStream(tokenStream);
namedList.add(tokenStream.getClass().getName(), convertTokensToNamedLists(tokens, context));
ListBasedTokenStream listBasedTokenStream = new ListBasedTokenStream(tokenStream, tokens);
for (TokenFilterFactory tokenFilterFactory : filtfacs) {
for (final AttributeSource tok : tokens) {
tok.getAttribute(TokenTrackingAttribute.class).freezeStage();
}
// overwrite the vars "tokenStream", "tokens", and "listBasedTokenStream"
tokenStream = tokenFilterFactory.create(listBasedTokenStream);
tokens = analyzeTokenStream(tokenStream);
namedList.add(tokenStream.getClass().getName(), convertTokensToNamedLists(tokens, context));
listBasedTokenStream = new ListBasedTokenStream(listBasedTokenStream, tokens);
}
return namedList;
}
Aggregations