/** * 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 org.apache.solr.client.solrj.SolrRequest; import org.apache.solr.client.solrj.SolrServer; import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.response.DocumentAnalysisResponse; import org.apache.solr.client.solrj.util.ClientUtils; import org.apache.solr.common.SolrInputDocument; import org.apache.solr.common.params.AnalysisParams; import org.apache.solr.common.params.ModifiableSolrParams; import org.apache.solr.common.util.ContentStream; import java.io.IOException; import java.io.StringWriter; import java.util.ArrayList; import java.util.Collection; import java.util.List; /** * A request for the org.apache.solr.handler.DocumentAnalysisRequestHandler. * * @version $Id: DocumentAnalysisRequest.java 767412 2009-04-22 08:53:28Z shalin $ * @since solr 1.4 */ public class DocumentAnalysisRequest extends SolrRequest { private List<SolrInputDocument> documents = new ArrayList<SolrInputDocument>(); private String query; private boolean showMatch = false; /** * Constructs a new request with a default uri of "/documentanalysis". */ public DocumentAnalysisRequest() { super(METHOD.POST, "/analysis/document"); } /** * Constructs a new request with the given request handler uri. * * @param uri The of the request handler. */ public DocumentAnalysisRequest(String uri) { super(METHOD.POST, uri); } /** * {@inheritDoc} */ @Override public Collection<ContentStream> getContentStreams() throws IOException { return ClientUtils.toContentStreams(getXML(), ClientUtils.TEXT_XML); } /** * {@inheritDoc} */ @Override public ModifiableSolrParams getParams() { ModifiableSolrParams params = new ModifiableSolrParams(); if (query != null) { params.add(AnalysisParams.QUERY, query); params.add(AnalysisParams.SHOW_MATCH, String.valueOf(showMatch)); } return params; } /** * {@inheritDoc} */ @Override public DocumentAnalysisResponse process(SolrServer server) throws SolrServerException, IOException { long startTime = System.currentTimeMillis(); DocumentAnalysisResponse res = new DocumentAnalysisResponse(); res.setResponse(server.request(this)); res.setElapsedTime(System.currentTimeMillis() - startTime); return res; } //================================================ Helper Methods ================================================== /** * Returns the xml be be set as the request body. * * @return The xml be be set as the request body. * * @throws IOException When constructing the xml fails */ String getXML() throws IOException { StringWriter writer = new StringWriter(); writer.write("<docs>"); for (SolrInputDocument document : documents) { ClientUtils.writeXML(document, writer); } writer.write("</docs>"); writer.flush(); String xml = writer.toString(); return (xml.length() > 0) ? xml : null; } //============================================ Setter/Getter Methods =============================================== /** * Adds a document to be analyzed. * * @param doc The document to be analyzed. * * @return This DocumentAnalysisRequest (fluent interface support). */ public DocumentAnalysisRequest addDocument(SolrInputDocument doc) { documents.add(doc); return this; } /** * Adds a collection of documents to be analyzed. * * @param docs The documents to be analyzed. * * @return This DocumentAnalysisRequest (fluent interface support). * * @see #addDocument(org.apache.solr.common.SolrInputDocument) */ public DocumentAnalysisRequest addDocuments(Collection<SolrInputDocument> docs) { documents.addAll(docs); return this; } /** * Sets the query to be analyzed. By default the query is set to null, meaning no query analysis will be performed. * * @param query The query to be analyzed. * * @return This DocumentAnalysisRequest (fluent interface support). */ public DocumentAnalysisRequest setQuery(String query) { this.query = query; return this; } /** * Sets whether index time tokens that match query time tokens should be marked as a "match". By default this is set * to {@code false}. Obviously, this flag is ignored if when the query is set to {@code null}. * * @param showMatch Sets whether index time tokens that match query time tokens should be marked as a "match". * * @return This DocumentAnalysisRequest (fluent interface support). */ public DocumentAnalysisRequest setShowMatch(boolean showMatch) { this.showMatch = showMatch; return this; } /** * Returns all documents that will be analyzed when processing the request. * * @return All documents that will be analyzed when processing the request. * * @see #addDocument(org.apache.solr.common.SolrInputDocument) */ public List<SolrInputDocument> getDocuments() { return documents; } /** * Returns the query that will be analyzed when processing the request. May return {@code null} indicating that no * query time analysis is taking place. * * @return The query that will be analyzed when processing the request. * * @see #setQuery(String) */ public String getQuery() { return query; } /** * Returns whether index time tokens that match query time tokens will be marked as a "match". * * @return Whether index time tokens that match query time tokens will be marked as a "match". * * @see #setShowMatch(boolean) */ public boolean isShowMatch() { return showMatch; } }