SOLR-7978: Fixed example/files update-script.js to be Java 7 and 8 compatible

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1701883 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Erik Hatcher 2015-09-08 23:52:16 +00:00
parent 9dcef8c8c3
commit 50bf071600
5 changed files with 87 additions and 6 deletions

View File

@ -195,6 +195,8 @@ Bug Fixes
* SOLR-8001: Fixed bugs in field(foo,min) and field(foo,max) when some docs have no values * SOLR-8001: Fixed bugs in field(foo,min) and field(foo,max) when some docs have no values
(David Smiley, hossman) (David Smiley, hossman)
* SOLR-7978: Fixed example/files update-script.js to be Java 7 and 8 compatible. (Erik Hatcher)
Optimizations Optimizations
---------------------- ----------------------

View File

@ -0,0 +1,53 @@
function get_class(name) {
var clazz;
try {
// Java8 Nashorn
clazz = eval("Java.type(name).class");
} catch(e) {
// Java7 Rhino
clazz = eval("Packages."+name);
}
return clazz;
}
function processAdd(cmd) {
var doc = cmd.getSolrInputDocument();
var analyzer =
req.getCore().getLatestSchema()
.getFieldTypeByName("text")
.getIndexAnalyzer();
var token_stream =
analyzer.tokenStream("subject", doc.getFieldValue("subject"));
var cta_class = get_class("org.apache.lucene.analysis.tokenattributes.CharTermAttribute");
var term_att = token_stream.getAttribute(cta_class);
token_stream.reset();
while (token_stream.incrementToken()) {
doc.addField("term_s", term_att.toString());
}
token_stream.end();
token_stream.close();
return true;
}
// // //
function processDelete() {
// NOOP
}
function processCommit() {
// NOOP
}
function processRollback() {
// NOOP
}
function processMergeIndexes() {
// NOOP
}
function finish() {
// NOOP
}

View File

@ -103,10 +103,17 @@
<str name="script">throw.error.on.add.updateprocessor.js</str> <str name="script">throw.error.on.add.updateprocessor.js</str>
</processor> </processor>
</updateRequestProcessorChain> </updateRequestProcessorChain>
<updateRequestProcessorChain name="missing-functions"> <updateRequestProcessorChain name="missing-functions">
<processor class="solr.StatelessScriptUpdateProcessorFactory"> <processor class="solr.StatelessScriptUpdateProcessorFactory">
<str name="script">missing.functions.updateprocessor.js</str> <str name="script">missing.functions.updateprocessor.js</str>
</processor> </processor>
</updateRequestProcessorChain> </updateRequestProcessorChain>
<updateRequestProcessorChain name="javascript-compatibility">
<processor class="solr.StatelessScriptUpdateProcessorFactory">
<str name="script">cross-compatible.js</str>
</processor>
</updateRequestProcessorChain>
</config> </config>

View File

@ -268,4 +268,14 @@ public class StatelessScriptUpdateProcessorFactoryTest extends UpdateProcessorTe
fail("Did not get exception from script"); fail("Did not get exception from script");
} }
public void testJavaScriptCompatibility() throws Exception {
final String chain = "javascript-compatibility";
SolrInputDocument d = processAdd(chain,
doc(f("id", "5"),
f("name", " foo "),
f("subject", "BAR")));
assertEquals("bar", d.getFieldValue("term_s"));
}
} }

View File

@ -1,6 +1,15 @@
/* function get_class(name) {
See http://wiki.apache.org/solr/ScriptUpdateProcessor for more details. var clazz;
*/ try {
// Java8 Nashorn
clazz = eval("Java.type(name).class");
} catch(e) {
// Java7 Rhino
clazz = eval("Packages."+name);
}
return clazz;
}
function processAdd(cmd) { function processAdd(cmd) {
@ -69,9 +78,9 @@ function processAdd(cmd) {
.getIndexAnalyzer(); .getIndexAnalyzer();
var token_stream = var token_stream =
analyzer.tokenStream("content", new java.io.StringReader(doc.getFieldValue("content"))); analyzer.tokenStream("content", doc.getFieldValue("content"));
var term_att = token_stream.getAttribute(org.apache.lucene.analysis.tokenattributes.CharTermAttribute.class); var term_att = token_stream.getAttribute(get_class("org.apache.lucene.analysis.tokenattributes.CharTermAttribute"));
var type_att = token_stream.getAttribute(org.apache.lucene.analysis.tokenattributes.TypeAttribute.class); var type_att = token_stream.getAttribute(get_class("org.apache.lucene.analysis.tokenattributes.TypeAttribute"));
token_stream.reset(); token_stream.reset();
while (token_stream.incrementToken()) { while (token_stream.incrementToken()) {
doc.addField(type_att.type().replace(/\<|\>/g,'').toLowerCase()+"_ss", term_att.toString()); doc.addField(type_att.type().replace(/\<|\>/g,'').toLowerCase()+"_ss", term_att.toString());