Skip to content

Instantly share code, notes, and snippets.

@karthikshiraly
Created December 19, 2011 04:58
Show Gist options
  • Save karthikshiraly/1495452 to your computer and use it in GitHub Desktop.
Save karthikshiraly/1495452 to your computer and use it in GitHub Desktop.
Function testing for Solr 3.3.x schema.xml
/**
* This class uses Solr 3.3.x classes to load schema.xml and runs input text through the analyzers
* defined in it.
*/
public class SchemaTester {
public static void main(String[] args) {
try {
InputSource solrCfgIs = new InputSource(
new FileReader("solr/conf/solrconfig.xml"));
SolrConfig solrConfig = new SolrConfig(null, solrCfgIs);
InputSource solrSchemaIs = new InputSource(
new FileReader("solr/conf/schema.xml"));
IndexSchema solrSchema = new IndexSchema(solrConfig, null,
solrSchemaIs);
Map<String, FieldType> fieldTypes = solrSchema.getFieldTypes();
for (Iterator<Entry<String, FieldType>> iter = fieldTypes.entrySet().iterator();
iter.hasNext();) {
Entry<String, FieldType> entry = iter.next();
FieldType fldType = entry.getValue();
Analyzer analyzer = fldType.getAnalyzer();
System.out.println(entry.getKey() + ":" + analyzer.toString());
}
String inputText = "Proof of the pudding lies in its eating";
FieldType fieldTypeText = fieldTypes.get("text_en");
System.out.println("Indexing analysis:");
Analyzer analyzer = fieldTypeText.getAnalyzer();
TokenStream tokenStream = analyzer.tokenStream("dummyfield",
new StringReader(inputText));
CharTermAttribute termAttr = (CharTermAttribute) tokenStream.getAttribute(CharTermAttribute.class);
while (tokenStream.incrementToken()) {
System.out.println(termAttr.toString());
}
System.out.println("\n\nQuerying analysis:");
Analyzer qryAnalyzer = fieldTypeText.getQueryAnalyzer();
TokenStream qrytokenStream = qryAnalyzer.tokenStream("dummyfield",
new StringReader(inputText));
CharTermAttribute termAttr2 = (CharTermAttribute) qrytokenStream.getAttribute(CharTermAttribute.class);
while (qrytokenStream.incrementToken()) {
System.out.println(termAttr2.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment