mirror of https://github.com/apache/lucene.git
SOLR-2496: add array-of-object JSON format and commitWithin, overwrite request params
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1102058 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
e4f9bfdd06
commit
02bc22d21a
|
@ -270,6 +270,12 @@ Detailed Change List
|
||||||
New Features
|
New Features
|
||||||
----------------------
|
----------------------
|
||||||
|
|
||||||
|
* SOLR-2496: Add ability to specify overwrite and commitWithin as request
|
||||||
|
parameters (e.g. specified in the URL) when using the JSON update format,
|
||||||
|
and added a simplified format for specifying multiple documents.
|
||||||
|
Example: [{"id":"doc1"},{"id":"doc2"}]
|
||||||
|
(yonik)
|
||||||
|
|
||||||
Optimizations
|
Optimizations
|
||||||
----------------------
|
----------------------
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
{
|
[
|
||||||
|
{
|
||||||
"add": {
|
|
||||||
"doc": {
|
|
||||||
"id" : "978-0641723445",
|
"id" : "978-0641723445",
|
||||||
"cat" : ["book","hardcover"],
|
"cat" : ["book","hardcover"],
|
||||||
"title" : "The Lightning Thief",
|
"title" : "The Lightning Thief",
|
||||||
|
@ -13,11 +11,8 @@
|
||||||
"price" : 12.50,
|
"price" : 12.50,
|
||||||
"pages_i" : 384
|
"pages_i" : 384
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
,
|
,
|
||||||
"add": {
|
{
|
||||||
"doc": {
|
|
||||||
"id" : "978-1423103349",
|
"id" : "978-1423103349",
|
||||||
"cat" : ["book","paperback"],
|
"cat" : ["book","paperback"],
|
||||||
"title" : "The Sea of Monsters",
|
"title" : "The Sea of Monsters",
|
||||||
|
@ -29,6 +24,4 @@
|
||||||
"price" : 6.49,
|
"price" : 6.49,
|
||||||
"pages_i" : 304
|
"pages_i" : 304
|
||||||
}
|
}
|
||||||
}
|
]
|
||||||
|
|
||||||
}
|
|
||||||
|
|
|
@ -23,6 +23,7 @@ import java.util.Stack;
|
||||||
|
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.apache.noggit.JSONParser;
|
import org.apache.noggit.JSONParser;
|
||||||
|
import org.apache.solr.common.SolrException;
|
||||||
import org.apache.solr.common.SolrInputDocument;
|
import org.apache.solr.common.SolrInputDocument;
|
||||||
import org.apache.solr.common.SolrInputField;
|
import org.apache.solr.common.SolrInputField;
|
||||||
import org.apache.solr.common.util.ContentStream;
|
import org.apache.solr.common.util.ContentStream;
|
||||||
|
@ -43,10 +44,18 @@ import org.slf4j.LoggerFactory;
|
||||||
class JsonLoader extends ContentStreamLoader {
|
class JsonLoader extends ContentStreamLoader {
|
||||||
final static Logger log = LoggerFactory.getLogger( JsonLoader.class );
|
final static Logger log = LoggerFactory.getLogger( JsonLoader.class );
|
||||||
|
|
||||||
protected UpdateRequestProcessor processor;
|
protected final UpdateRequestProcessor processor;
|
||||||
|
protected final SolrQueryRequest req;
|
||||||
|
protected JSONParser parser;
|
||||||
|
protected final int commitWithin;
|
||||||
|
protected final boolean overwrite;
|
||||||
|
|
||||||
public JsonLoader(UpdateRequestProcessor processor) {
|
public JsonLoader(SolrQueryRequest req, UpdateRequestProcessor processor) {
|
||||||
this.processor = processor;
|
this.processor = processor;
|
||||||
|
this.req = req;
|
||||||
|
|
||||||
|
commitWithin = req.getParams().getInt(XmlUpdateRequestHandler.COMMIT_WITHIN, -1);
|
||||||
|
overwrite = req.getParams().getBool(XmlUpdateRequestHandler.OVERWRITE, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -55,14 +64,14 @@ class JsonLoader extends ContentStreamLoader {
|
||||||
Reader reader = null;
|
Reader reader = null;
|
||||||
try {
|
try {
|
||||||
reader = stream.getReader();
|
reader = stream.getReader();
|
||||||
if (XmlUpdateRequestHandler.log.isTraceEnabled()) {
|
if (log.isTraceEnabled()) {
|
||||||
String body = IOUtils.toString(reader);
|
String body = IOUtils.toString(reader);
|
||||||
XmlUpdateRequestHandler.log.trace("body", body);
|
log.trace("body", body);
|
||||||
reader = new StringReader(body);
|
reader = new StringReader(body);
|
||||||
}
|
}
|
||||||
|
|
||||||
JSONParser parser = new JSONParser(reader);
|
parser = new JSONParser(reader);
|
||||||
this.processUpdate(req, processor, parser);
|
this.processUpdate();
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
IOUtils.closeQuietly(reader);
|
IOUtils.closeQuietly(reader);
|
||||||
|
@ -70,39 +79,50 @@ class JsonLoader extends ContentStreamLoader {
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("fallthrough")
|
@SuppressWarnings("fallthrough")
|
||||||
void processUpdate(SolrQueryRequest req, UpdateRequestProcessor processor, JSONParser parser) throws IOException
|
void processUpdate() throws IOException
|
||||||
{
|
{
|
||||||
int ev = parser.nextEvent();
|
int ev = parser.nextEvent();
|
||||||
while( ev != JSONParser.EOF ) {
|
while( ev != JSONParser.EOF ) {
|
||||||
|
|
||||||
switch( ev )
|
switch( ev )
|
||||||
{
|
{
|
||||||
|
case JSONParser.ARRAY_START:
|
||||||
|
handleAdds();
|
||||||
|
break;
|
||||||
|
|
||||||
case JSONParser.STRING:
|
case JSONParser.STRING:
|
||||||
if( parser.wasKey() ) {
|
if( parser.wasKey() ) {
|
||||||
String v = parser.getString();
|
String v = parser.getString();
|
||||||
if( v.equals( XmlUpdateRequestHandler.ADD ) ) {
|
if( v.equals( XmlUpdateRequestHandler.ADD ) ) {
|
||||||
processor.processAdd( parseAdd(req, parser ) );
|
int ev2 = parser.nextEvent();
|
||||||
|
if (ev2 == JSONParser.OBJECT_START) {
|
||||||
|
processor.processAdd( parseAdd() );
|
||||||
|
} else if (ev2 == JSONParser.ARRAY_START) {
|
||||||
|
handleAdds();
|
||||||
|
} else {
|
||||||
|
assertEvent(ev2, JSONParser.OBJECT_START);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if( v.equals( XmlUpdateRequestHandler.COMMIT ) ) {
|
else if( v.equals( XmlUpdateRequestHandler.COMMIT ) ) {
|
||||||
CommitUpdateCommand cmd = new CommitUpdateCommand(req, false );
|
CommitUpdateCommand cmd = new CommitUpdateCommand(req, false );
|
||||||
cmd.waitFlush = cmd.waitSearcher = true;
|
cmd.waitFlush = cmd.waitSearcher = true;
|
||||||
parseCommitOptions( parser, cmd );
|
parseCommitOptions( cmd );
|
||||||
processor.processCommit( cmd );
|
processor.processCommit( cmd );
|
||||||
}
|
}
|
||||||
else if( v.equals( XmlUpdateRequestHandler.OPTIMIZE ) ) {
|
else if( v.equals( XmlUpdateRequestHandler.OPTIMIZE ) ) {
|
||||||
CommitUpdateCommand cmd = new CommitUpdateCommand(req, true );
|
CommitUpdateCommand cmd = new CommitUpdateCommand(req, true );
|
||||||
cmd.waitFlush = cmd.waitSearcher = true;
|
cmd.waitFlush = cmd.waitSearcher = true;
|
||||||
parseCommitOptions( parser, cmd );
|
parseCommitOptions( cmd );
|
||||||
processor.processCommit( cmd );
|
processor.processCommit( cmd );
|
||||||
}
|
}
|
||||||
else if( v.equals( XmlUpdateRequestHandler.DELETE ) ) {
|
else if( v.equals( XmlUpdateRequestHandler.DELETE ) ) {
|
||||||
processor.processDelete( parseDelete(req, parser ) );
|
processor.processDelete( parseDelete() );
|
||||||
}
|
}
|
||||||
else if( v.equals( XmlUpdateRequestHandler.ROLLBACK ) ) {
|
else if( v.equals( XmlUpdateRequestHandler.ROLLBACK ) ) {
|
||||||
processor.processRollback( parseRollback(req, parser ) );
|
processor.processRollback( parseRollback() );
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new IOException( "Unknown command: "+v+" ["+parser.getPosition()+"]" );
|
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Unknown command: "+v+" ["+parser.getPosition()+"]" );
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -117,12 +137,11 @@ class JsonLoader extends ContentStreamLoader {
|
||||||
|
|
||||||
case JSONParser.OBJECT_START:
|
case JSONParser.OBJECT_START:
|
||||||
case JSONParser.OBJECT_END:
|
case JSONParser.OBJECT_END:
|
||||||
case JSONParser.ARRAY_START:
|
|
||||||
case JSONParser.ARRAY_END:
|
case JSONParser.ARRAY_END:
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
System.out.println("UNKNOWN_EVENT_ID:"+ev);
|
log.info("Noggit UNKNOWN_EVENT_ID:"+ev);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// read the next event
|
// read the next event
|
||||||
|
@ -130,187 +149,211 @@ class JsonLoader extends ContentStreamLoader {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
DeleteUpdateCommand parseDelete(SolrQueryRequest req, JSONParser js) throws IOException {
|
DeleteUpdateCommand parseDelete() throws IOException {
|
||||||
assertNextEvent( js, JSONParser.OBJECT_START );
|
assertNextEvent( JSONParser.OBJECT_START );
|
||||||
|
|
||||||
DeleteUpdateCommand cmd = new DeleteUpdateCommand(req);
|
DeleteUpdateCommand cmd = new DeleteUpdateCommand(req);
|
||||||
|
|
||||||
while( true ) {
|
while( true ) {
|
||||||
int ev = js.nextEvent();
|
int ev = parser.nextEvent();
|
||||||
if( ev == JSONParser.STRING ) {
|
if( ev == JSONParser.STRING ) {
|
||||||
String key = js.getString();
|
String key = parser.getString();
|
||||||
if( js.wasKey() ) {
|
if( parser.wasKey() ) {
|
||||||
if( "id".equals( key ) ) {
|
if( "id".equals( key ) ) {
|
||||||
cmd.id = js.getString();
|
cmd.id = parser.getString();
|
||||||
}
|
}
|
||||||
else if( "query".equals(key) ) {
|
else if( "query".equals(key) ) {
|
||||||
cmd.query = js.getString();
|
cmd.query = parser.getString();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new IOException( "Unknown key: "+key+" ["+js.getPosition()+"]" );
|
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Unknown key: "+key+" ["+parser.getPosition()+"]" );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new IOException(
|
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
|
||||||
"invalid string: " + key
|
"invalid string: " + key
|
||||||
+" at ["+js.getPosition()+"]" );
|
+" at ["+parser.getPosition()+"]" );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if( ev == JSONParser.OBJECT_END ) {
|
else if( ev == JSONParser.OBJECT_END ) {
|
||||||
if( cmd.id == null && cmd.query == null ) {
|
if( cmd.id == null && cmd.query == null ) {
|
||||||
throw new IOException( "Missing id or query for delete ["+js.getPosition()+"]" );
|
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Missing id or query for delete ["+parser.getPosition()+"]" );
|
||||||
}
|
}
|
||||||
return cmd;
|
return cmd;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new IOException(
|
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
|
||||||
"Got: "+JSONParser.getEventString( ev )
|
"Got: "+JSONParser.getEventString( ev )
|
||||||
+" at ["+js.getPosition()+"]" );
|
+" at ["+parser.getPosition()+"]" );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
RollbackUpdateCommand parseRollback(SolrQueryRequest req, JSONParser js) throws IOException {
|
RollbackUpdateCommand parseRollback() throws IOException {
|
||||||
assertNextEvent( js, JSONParser.OBJECT_START );
|
assertNextEvent( JSONParser.OBJECT_START );
|
||||||
assertNextEvent( js, JSONParser.OBJECT_END );
|
assertNextEvent( JSONParser.OBJECT_END );
|
||||||
return new RollbackUpdateCommand(req);
|
return new RollbackUpdateCommand(req);
|
||||||
}
|
}
|
||||||
|
|
||||||
void parseCommitOptions( JSONParser js, CommitUpdateCommand cmd ) throws IOException
|
void parseCommitOptions(CommitUpdateCommand cmd ) throws IOException
|
||||||
{
|
{
|
||||||
assertNextEvent( js, JSONParser.OBJECT_START );
|
assertNextEvent( JSONParser.OBJECT_START );
|
||||||
|
|
||||||
while( true ) {
|
while( true ) {
|
||||||
int ev = js.nextEvent();
|
int ev = parser.nextEvent();
|
||||||
if( ev == JSONParser.STRING ) {
|
if( ev == JSONParser.STRING ) {
|
||||||
String key = js.getString();
|
String key = parser.getString();
|
||||||
if( js.wasKey() ) {
|
if( parser.wasKey() ) {
|
||||||
if( XmlUpdateRequestHandler.WAIT_SEARCHER.equals( key ) ) {
|
if( XmlUpdateRequestHandler.WAIT_SEARCHER.equals( key ) ) {
|
||||||
cmd.waitSearcher = js.getBoolean();
|
cmd.waitSearcher = parser.getBoolean();
|
||||||
}
|
}
|
||||||
else if( XmlUpdateRequestHandler.WAIT_FLUSH.equals( key ) ) {
|
else if( XmlUpdateRequestHandler.WAIT_FLUSH.equals( key ) ) {
|
||||||
cmd.waitFlush = js.getBoolean();
|
cmd.waitFlush = parser.getBoolean();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new IOException( "Unknown key: "+key+" ["+js.getPosition()+"]" );
|
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Unknown key: "+key+" ["+parser.getPosition()+"]" );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new IOException(
|
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
|
||||||
"invalid string: " + key
|
"invalid string: " + key
|
||||||
+" at ["+js.getPosition()+"]" );
|
+" at ["+parser.getPosition()+"]" );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if( ev == JSONParser.OBJECT_END ) {
|
else if( ev == JSONParser.OBJECT_END ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new IOException(
|
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
|
||||||
"Got: "+JSONParser.getEventString( ev )
|
"Got: "+JSONParser.getEventString( ev )
|
||||||
+" at ["+js.getPosition()+"]" );
|
+" at ["+parser.getPosition()+"]" );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
AddUpdateCommand parseAdd(SolrQueryRequest req, JSONParser js ) throws IOException
|
AddUpdateCommand parseAdd() throws IOException
|
||||||
{
|
{
|
||||||
assertNextEvent( js, JSONParser.OBJECT_START );
|
|
||||||
AddUpdateCommand cmd = new AddUpdateCommand(req);
|
AddUpdateCommand cmd = new AddUpdateCommand(req);
|
||||||
|
cmd.commitWithin = commitWithin;
|
||||||
|
cmd.overwrite = overwrite;
|
||||||
|
|
||||||
float boost = 1.0f;
|
float boost = 1.0f;
|
||||||
|
|
||||||
while( true ) {
|
while( true ) {
|
||||||
int ev = js.nextEvent();
|
int ev = parser.nextEvent();
|
||||||
if( ev == JSONParser.STRING ) {
|
if( ev == JSONParser.STRING ) {
|
||||||
if( js.wasKey() ) {
|
if( parser.wasKey() ) {
|
||||||
String key = js.getString();
|
String key = parser.getString();
|
||||||
if( "doc".equals( key ) ) {
|
if( "doc".equals( key ) ) {
|
||||||
if( cmd.solrDoc != null ) {
|
if( cmd.solrDoc != null ) {
|
||||||
throw new IOException( "multiple docs in same add command" );
|
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "multiple docs in same add command" );
|
||||||
}
|
}
|
||||||
ev = assertNextEvent( js, JSONParser.OBJECT_START );
|
ev = assertNextEvent( JSONParser.OBJECT_START );
|
||||||
cmd.solrDoc = parseDoc( ev, js );
|
cmd.solrDoc = parseDoc( ev );
|
||||||
}
|
}
|
||||||
else if( XmlUpdateRequestHandler.OVERWRITE.equals( key ) ) {
|
else if( XmlUpdateRequestHandler.OVERWRITE.equals( key ) ) {
|
||||||
cmd.overwrite = js.getBoolean(); // reads next boolean
|
cmd.overwrite = parser.getBoolean(); // reads next boolean
|
||||||
}
|
}
|
||||||
else if( XmlUpdateRequestHandler.COMMIT_WITHIN.equals( key ) ) {
|
else if( XmlUpdateRequestHandler.COMMIT_WITHIN.equals( key ) ) {
|
||||||
cmd.commitWithin = (int)js.getLong();
|
cmd.commitWithin = (int)parser.getLong();
|
||||||
}
|
}
|
||||||
else if( "boost".equals( key ) ) {
|
else if( "boost".equals( key ) ) {
|
||||||
boost = Float.parseFloat( js.getNumberChars().toString() );
|
boost = Float.parseFloat( parser.getNumberChars().toString() );
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new IOException( "Unknown key: "+key+" ["+js.getPosition()+"]" );
|
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Unknown key: "+key+" ["+parser.getPosition()+"]" );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new IOException(
|
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
|
||||||
"Should be a key "
|
"Should be a key "
|
||||||
+" at ["+js.getPosition()+"]" );
|
+" at ["+parser.getPosition()+"]" );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if( ev == JSONParser.OBJECT_END ) {
|
else if( ev == JSONParser.OBJECT_END ) {
|
||||||
if( cmd.solrDoc == null ) {
|
if( cmd.solrDoc == null ) {
|
||||||
throw new IOException("missing solr document. "+js.getPosition() );
|
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,"missing solr document. "+parser.getPosition() );
|
||||||
}
|
}
|
||||||
cmd.solrDoc.setDocumentBoost( boost );
|
cmd.solrDoc.setDocumentBoost( boost );
|
||||||
return cmd;
|
return cmd;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new IOException(
|
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
|
||||||
"Got: "+JSONParser.getEventString( ev )
|
"Got: "+JSONParser.getEventString( ev )
|
||||||
+" at ["+js.getPosition()+"]" );
|
+" at ["+parser.getPosition()+"]" );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int assertNextEvent( JSONParser parser, int ev ) throws IOException
|
|
||||||
|
void handleAdds() throws IOException
|
||||||
|
{
|
||||||
|
while( true ) {
|
||||||
|
AddUpdateCommand cmd = new AddUpdateCommand(req);
|
||||||
|
cmd.commitWithin = commitWithin;
|
||||||
|
cmd.overwrite = overwrite;
|
||||||
|
|
||||||
|
int ev = parser.nextEvent();
|
||||||
|
if (ev == JSONParser.ARRAY_END) break;
|
||||||
|
|
||||||
|
assertEvent(ev, JSONParser.OBJECT_START);
|
||||||
|
cmd.solrDoc = parseDoc(ev);
|
||||||
|
processor.processAdd(cmd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int assertNextEvent(int expected ) throws IOException
|
||||||
{
|
{
|
||||||
int got = parser.nextEvent();
|
int got = parser.nextEvent();
|
||||||
if( ev != got ) {
|
assertEvent(got, expected);
|
||||||
throw new IOException(
|
|
||||||
"Expected: "+JSONParser.getEventString( ev )
|
|
||||||
+" but got "+JSONParser.getEventString( got )
|
|
||||||
+" at ["+parser.getPosition()+"]" );
|
|
||||||
}
|
|
||||||
return got;
|
return got;
|
||||||
}
|
}
|
||||||
|
|
||||||
SolrInputDocument parseDoc( int ev, JSONParser js ) throws IOException
|
void assertEvent(int ev, int expected) {
|
||||||
|
if( ev != expected ) {
|
||||||
|
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
|
||||||
|
"Expected: "+JSONParser.getEventString( expected )
|
||||||
|
+" but got "+JSONParser.getEventString( ev )
|
||||||
|
+" at ["+parser.getPosition()+"]" );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SolrInputDocument parseDoc(int ev) throws IOException
|
||||||
{
|
{
|
||||||
Stack<Object> stack = new Stack<Object>();
|
Stack<Object> stack = new Stack<Object>();
|
||||||
Object obj = null;
|
Object obj = null;
|
||||||
boolean inArray = false;
|
boolean inArray = false;
|
||||||
|
|
||||||
if( ev != JSONParser.OBJECT_START ) {
|
if( ev != JSONParser.OBJECT_START ) {
|
||||||
throw new IOException( "object should already be started" );
|
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "object should already be started" );
|
||||||
}
|
}
|
||||||
|
|
||||||
while( true ) {
|
while( true ) {
|
||||||
//System.out.println( ev + "["+JSONParser.getEventString(ev)+"] "+js.wasKey() ); //+ js.getString() );
|
//System.out.println( ev + "["+JSONParser.getEventString(ev)+"] "+parser.wasKey() ); //+ parser.getString() );
|
||||||
|
|
||||||
switch (ev) {
|
switch (ev) {
|
||||||
case JSONParser.STRING:
|
case JSONParser.STRING:
|
||||||
if( js.wasKey() ) {
|
if( parser.wasKey() ) {
|
||||||
obj = stack.peek();
|
obj = stack.peek();
|
||||||
String v = js.getString();
|
String v = parser.getString();
|
||||||
if( obj instanceof SolrInputField ) {
|
if( obj instanceof SolrInputField ) {
|
||||||
SolrInputField field = (SolrInputField)obj;
|
SolrInputField field = (SolrInputField)obj;
|
||||||
if( "boost".equals( v ) ) {
|
if( "boost".equals( v ) ) {
|
||||||
ev = js.nextEvent();
|
ev = parser.nextEvent();
|
||||||
if( ev != JSONParser.NUMBER &&
|
if( ev != JSONParser.NUMBER &&
|
||||||
ev != JSONParser.LONG &&
|
ev != JSONParser.LONG &&
|
||||||
ev != JSONParser.BIGNUMBER ) {
|
ev != JSONParser.BIGNUMBER ) {
|
||||||
throw new IOException( "boost should have number! "+JSONParser.getEventString(ev) );
|
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "boost should have number! "+JSONParser.getEventString(ev) );
|
||||||
}
|
}
|
||||||
field.setBoost( Float.valueOf( js.getNumberChars().toString() ) );
|
field.setBoost( Float.valueOf( parser.getNumberChars().toString() ) );
|
||||||
}
|
}
|
||||||
else if( "value".equals( v ) ) {
|
else if( "value".equals( v ) ) {
|
||||||
// nothing special...
|
// nothing special...
|
||||||
stack.push( field ); // so it can be popped
|
stack.push( field ); // so it can be popped
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new IOException( "invalid key: "+v + " ["+js.getPosition()+"]" );
|
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "invalid key: "+v + " ["+ parser.getPosition()+"]" );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if( obj instanceof SolrInputDocument ) {
|
else if( obj instanceof SolrInputDocument ) {
|
||||||
|
@ -323,22 +366,22 @@ class JsonLoader extends ContentStreamLoader {
|
||||||
stack.push( f );
|
stack.push( f );
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new IOException( "hymmm ["+js.getPosition()+"]" );
|
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "hymmm ["+ parser.getPosition()+"]" );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
addValToField(stack, js.getString(), inArray, js);
|
addValToField(stack, parser.getString(), inArray, parser);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case JSONParser.LONG:
|
case JSONParser.LONG:
|
||||||
case JSONParser.NUMBER:
|
case JSONParser.NUMBER:
|
||||||
case JSONParser.BIGNUMBER:
|
case JSONParser.BIGNUMBER:
|
||||||
addValToField(stack, js.getNumberChars().toString(), inArray, js);
|
addValToField(stack, parser.getNumberChars().toString(), inArray, parser);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case JSONParser.BOOLEAN:
|
case JSONParser.BOOLEAN:
|
||||||
addValToField(stack, js.getBoolean(),inArray, js);
|
addValToField(stack, parser.getBoolean(),inArray, parser);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case JSONParser.OBJECT_START:
|
case JSONParser.OBJECT_START:
|
||||||
|
@ -351,7 +394,7 @@ class JsonLoader extends ContentStreamLoader {
|
||||||
// should alreay be pushed...
|
// should alreay be pushed...
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new IOException( "should not start new object with: "+obj + " ["+js.getPosition()+"]" );
|
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "should not start new object with: "+obj + " ["+ parser.getPosition()+"]" );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -365,7 +408,7 @@ class JsonLoader extends ContentStreamLoader {
|
||||||
// should already be pushed...
|
// should already be pushed...
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new IOException( "should not start new object with: "+obj + " ["+js.getPosition()+"]" );
|
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "should not start new object with: "+obj + " ["+ parser.getPosition()+"]" );
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -383,18 +426,18 @@ class JsonLoader extends ContentStreamLoader {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
ev = js.nextEvent();
|
ev = parser.nextEvent();
|
||||||
if( ev == JSONParser.EOF ) {
|
if( ev == JSONParser.EOF ) {
|
||||||
throw new IOException( "should finish doc first!" );
|
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "should finish doc first!" );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void addValToField( Stack stack, Object val, boolean inArray, JSONParser js ) throws IOException
|
static void addValToField( Stack stack, Object val, boolean inArray, JSONParser parser ) throws IOException
|
||||||
{
|
{
|
||||||
Object obj = stack.peek();
|
Object obj = stack.peek();
|
||||||
if( !(obj instanceof SolrInputField) ) {
|
if( !(obj instanceof SolrInputField) ) {
|
||||||
throw new IOException( "hymmm ["+js.getPosition()+"]" );
|
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "hymmm ["+parser.getPosition()+"]" );
|
||||||
}
|
}
|
||||||
|
|
||||||
SolrInputField f = inArray
|
SolrInputField f = inArray
|
||||||
|
|
|
@ -37,7 +37,7 @@ public class JsonUpdateRequestHandler extends ContentStreamHandlerBase {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected ContentStreamLoader newLoader(SolrQueryRequest req, UpdateRequestProcessor processor) {
|
protected ContentStreamLoader newLoader(SolrQueryRequest req, UpdateRequestProcessor processor) {
|
||||||
return new JsonLoader(processor);
|
return new JsonLoader(req, processor);
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////// SolrInfoMBeans methods //////////////////////
|
//////////////////////// SolrInfoMBeans methods //////////////////////
|
||||||
|
|
|
@ -26,7 +26,9 @@ import org.apache.lucene.util.LuceneTestCase;
|
||||||
import org.apache.solr.SolrTestCaseJ4;
|
import org.apache.solr.SolrTestCaseJ4;
|
||||||
import org.apache.solr.common.SolrInputDocument;
|
import org.apache.solr.common.SolrInputDocument;
|
||||||
import org.apache.solr.common.SolrInputField;
|
import org.apache.solr.common.SolrInputField;
|
||||||
|
import org.apache.solr.common.util.ContentStreamBase;
|
||||||
import org.apache.solr.request.SolrQueryRequest;
|
import org.apache.solr.request.SolrQueryRequest;
|
||||||
|
import org.apache.solr.response.SolrQueryResponse;
|
||||||
import org.apache.solr.update.AddUpdateCommand;
|
import org.apache.solr.update.AddUpdateCommand;
|
||||||
import org.apache.solr.update.CommitUpdateCommand;
|
import org.apache.solr.update.CommitUpdateCommand;
|
||||||
import org.apache.solr.update.DeleteUpdateCommand;
|
import org.apache.solr.update.DeleteUpdateCommand;
|
||||||
|
@ -81,12 +83,10 @@ public class JsonLoaderTest extends SolrTestCaseJ4 {
|
||||||
public void testParsing() throws Exception
|
public void testParsing() throws Exception
|
||||||
{
|
{
|
||||||
SolrQueryRequest req = req();
|
SolrQueryRequest req = req();
|
||||||
Reader reader = new StringReader(input);
|
SolrQueryResponse rsp = new SolrQueryResponse();
|
||||||
|
|
||||||
BufferingRequestProcessor p = new BufferingRequestProcessor(null);
|
BufferingRequestProcessor p = new BufferingRequestProcessor(null);
|
||||||
JsonLoader loader = new JsonLoader( p );
|
JsonLoader loader = new JsonLoader( req, p );
|
||||||
|
loader.load(req, rsp, new ContentStreamBase.StringStream(input));
|
||||||
loader.processUpdate(req, p, new JSONParser(reader) );
|
|
||||||
|
|
||||||
assertEquals( 2, p.addCommands.size() );
|
assertEquals( 2, p.addCommands.size() );
|
||||||
|
|
||||||
|
@ -133,8 +133,67 @@ public class JsonLoaderTest extends SolrTestCaseJ4 {
|
||||||
|
|
||||||
req.close();
|
req.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void testSimpleFormat() throws Exception
|
||||||
|
{
|
||||||
|
String str = "[{'id':'1'},{'id':'2'}]".replace('\'', '"');
|
||||||
|
SolrQueryRequest req = req("commitWithin","100", "overwrite","false");
|
||||||
|
SolrQueryResponse rsp = new SolrQueryResponse();
|
||||||
|
BufferingRequestProcessor p = new BufferingRequestProcessor(null);
|
||||||
|
JsonLoader loader = new JsonLoader( req, p );
|
||||||
|
loader.load(req, rsp, new ContentStreamBase.StringStream(str));
|
||||||
|
|
||||||
|
assertEquals( 2, p.addCommands.size() );
|
||||||
|
|
||||||
|
AddUpdateCommand add = p.addCommands.get(0);
|
||||||
|
SolrInputDocument d = add.solrDoc;
|
||||||
|
SolrInputField f = d.getField( "id" );
|
||||||
|
assertEquals("1", f.getValue());
|
||||||
|
assertEquals(add.commitWithin, 100);
|
||||||
|
assertEquals(add.overwrite, false);
|
||||||
|
|
||||||
|
add = p.addCommands.get(1);
|
||||||
|
d = add.solrDoc;
|
||||||
|
f = d.getField( "id" );
|
||||||
|
assertEquals("2", f.getValue());
|
||||||
|
assertEquals(add.commitWithin, 100);
|
||||||
|
assertEquals(add.overwrite, false);
|
||||||
|
|
||||||
|
req.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testSimpleFormatInAdd() throws Exception
|
||||||
|
{
|
||||||
|
String str = "{'add':[{'id':'1'},{'id':'2'}]}".replace('\'', '"');
|
||||||
|
SolrQueryRequest req = req();
|
||||||
|
SolrQueryResponse rsp = new SolrQueryResponse();
|
||||||
|
BufferingRequestProcessor p = new BufferingRequestProcessor(null);
|
||||||
|
JsonLoader loader = new JsonLoader( req, p );
|
||||||
|
loader.load(req, rsp, new ContentStreamBase.StringStream(str));
|
||||||
|
|
||||||
|
assertEquals( 2, p.addCommands.size() );
|
||||||
|
|
||||||
|
AddUpdateCommand add = p.addCommands.get(0);
|
||||||
|
SolrInputDocument d = add.solrDoc;
|
||||||
|
SolrInputField f = d.getField( "id" );
|
||||||
|
assertEquals("1", f.getValue());
|
||||||
|
assertEquals(add.commitWithin, -1);
|
||||||
|
assertEquals(add.overwrite, true);
|
||||||
|
|
||||||
|
add = p.addCommands.get(1);
|
||||||
|
d = add.solrDoc;
|
||||||
|
f = d.getField( "id" );
|
||||||
|
assertEquals("2", f.getValue());
|
||||||
|
assertEquals(add.commitWithin, -1);
|
||||||
|
assertEquals(add.overwrite, true);
|
||||||
|
|
||||||
|
req.close();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class BufferingRequestProcessor extends UpdateRequestProcessor
|
class BufferingRequestProcessor extends UpdateRequestProcessor
|
||||||
{
|
{
|
||||||
List<AddUpdateCommand> addCommands = new ArrayList<AddUpdateCommand>();
|
List<AddUpdateCommand> addCommands = new ArrayList<AddUpdateCommand>();
|
||||||
|
|
Loading…
Reference in New Issue