All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.solr.client.solrj.request.DocumentAnalysisRequest Maven / Gradle / Ivy

There is a newer version: 9.5.0
Show 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 org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrRequest;
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.
 *
 *
 * @since solr 1.4
 */
public class DocumentAnalysisRequest extends SolrRequest {

  private List documents = new ArrayList<>();
  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 getContentStreams() throws IOException {
    return ClientUtils.toContentStreams(getXML(), ClientUtils.TEXT_XML);
  }

  @Override
  protected DocumentAnalysisResponse createResponse(SolrClient client) {
    return new DocumentAnalysisResponse();
  }

  /**
   * {@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;
  }

  //================================================ 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("");
    for (SolrInputDocument document : documents) {
      ClientUtils.writeXML(document, writer);
    }
    writer.write("");
    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 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 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;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy