
org.apache.solr.client.solrj.request.UpdateRequest Maven / Gradle / Ivy
The newest version!
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.solr.client.solrj.request;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Iterator;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.client.solrj.util.ClientUtils;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.params.UpdateParams;
import org.apache.solr.common.util.ContentStream;
import org.apache.solr.common.util.XML;
/**
*
* @version $Id$
* @since solr 1.3
*/
public class UpdateRequest extends AbstractUpdateRequest {
/**
* Kept for back compatibility.
*
* @deprecated Use {@link AbstractUpdateRequest.ACTION} instead
*/
public enum ACTION {
COMMIT,
OPTIMIZE
};
private List documents = null;
private Iterator docIterator = null;
private List deleteById = null;
private List deleteQuery = null;
public UpdateRequest()
{
super( METHOD.POST, "/update" );
}
public UpdateRequest(String url) {
super(METHOD.POST, url);
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
/**
* clear the pending documents and delete commands
*/
public void clear()
{
if( documents != null ) {
documents.clear();
}
if( deleteById != null ) {
deleteById.clear();
}
if( deleteQuery != null ) {
deleteQuery.clear();
}
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
public UpdateRequest add( final SolrInputDocument doc )
{
if( documents == null ) {
documents = new ArrayList( 2 );
}
documents.add( doc );
return this;
}
public UpdateRequest add( final Collection docs )
{
if( documents == null ) {
documents = new ArrayList( docs.size()+1 );
}
documents.addAll( docs );
return this;
}
public UpdateRequest deleteById( String id )
{
if( deleteById == null ) {
deleteById = new ArrayList();
}
deleteById.add( id );
return this;
}
public UpdateRequest deleteById( List ids )
{
if( deleteById == null ) {
deleteById = new ArrayList(ids);
} else {
deleteById.addAll(ids);
}
return this;
}
public UpdateRequest deleteByQuery( String q )
{
if( deleteQuery == null ) {
deleteQuery = new ArrayList();
}
deleteQuery.add( q );
return this;
}
/** Sets appropriate parameters for the given ACTION
*
* @deprecated Use {@link org.apache.solr.client.solrj.request.AbstractUpdateRequest.ACTION} instead
* */
public UpdateRequest setAction(ACTION action, boolean waitFlush, boolean waitSearcher ) {
return setAction(action, waitFlush, waitSearcher, 1);
}
/**
*
* @deprecated Use {@link org.apache.solr.client.solrj.request.AbstractUpdateRequest.ACTION} instead
*/
public UpdateRequest setAction(ACTION action, boolean waitFlush, boolean waitSearcher, int maxSegments ) {
if (params == null)
params = new ModifiableSolrParams();
if( action == ACTION.OPTIMIZE ) {
params.set( UpdateParams.OPTIMIZE, "true" );
params.set(UpdateParams.MAX_OPTIMIZE_SEGMENTS, maxSegments);
}
else if( action == ACTION.COMMIT ) {
params.set( UpdateParams.COMMIT, "true" );
}
params.set( UpdateParams.WAIT_FLUSH, waitFlush+"" );
params.set( UpdateParams.WAIT_SEARCHER, waitSearcher+"" );
return this;
}
/**
*
*
* @deprecated Use {@link org.apache.solr.client.solrj.request.AbstractUpdateRequest.ACTION} instead
*/
public UpdateRequest setAction(ACTION action, boolean waitFlush, boolean waitSearcher, int maxSegments , boolean expungeDeletes) {
setAction(action, waitFlush, waitSearcher,maxSegments) ;
params.set(UpdateParams.EXPUNGE_DELETES,""+expungeDeletes);
return this;
}
public void setDocIterator(Iterator docIterator) {
this.docIterator = docIterator;
}
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
@Override
public Collection getContentStreams() throws IOException {
return ClientUtils.toContentStreams( getXML(), ClientUtils.TEXT_XML );
}
public String getXML() throws IOException {
StringWriter writer = new StringWriter();
writeXML( writer );
writer.flush();
// If action is COMMIT or OPTIMIZE, it is sent with params
String xml = writer.toString();
//System.out.println( "SEND:"+xml );
return (xml.length() > 0) ? xml : null;
}
/**
* @since solr 1.4
*/
public void writeXML( Writer writer ) throws IOException {
if( (documents != null && documents.size() > 0) || docIterator != null) {
if( commitWithin > 0 ) {
writer.write("");
}
else {
writer.write("");
}
if(documents != null) {
for (SolrInputDocument doc : documents) {
if (doc != null) {
ClientUtils.writeXML(doc, writer);
}
}
}
if (docIterator != null) {
while (docIterator.hasNext()) {
SolrInputDocument doc = docIterator.next();
if (doc != null) {
ClientUtils.writeXML(doc, writer);
}
}
}
writer.write(" ");
}
// Add the delete commands
boolean deleteI = deleteById != null && deleteById.size() > 0;
boolean deleteQ = deleteQuery != null && deleteQuery.size() > 0;
if( deleteI || deleteQ ) {
writer.append( "" );
if( deleteI ) {
for( String id : deleteById ) {
writer.append( "" );
XML.escapeCharData( id, writer );
writer.append( " " );
}
}
if( deleteQ ) {
for( String q : deleteQuery ) {
writer.append( "" );
XML.escapeCharData( q, writer );
writer.append( " " );
}
}
writer.append( " " );
}
}
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
//
//--------------------------------------------------------------------------
public List getDocuments() {
return documents;
}
public Iterator getDocIterator() {
return docIterator;
}
public List getDeleteById() {
return deleteById;
}
public List getDeleteQuery() {
return deleteQuery;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy