org.apache.solr.client.solrj.util.ClientUtils Java Examples

The following examples show how to use org.apache.solr.client.solrj.util.ClientUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AtlasJanusGraphIndexClient.java    From atlas with Apache License 2.0 6 votes vote down vote up
private SolrResponse performRequestHandlerAction(String collectionName,
                                               SolrClient solrClient,
                                               String actionPayLoad,
                                               Solr6Index.Mode mode) throws IOException, SolrServerException, AtlasBaseException {
    switch (mode) {
        case CLOUD:
            V2Request v2request = new V2Request.Builder(String.format("/collections/%s/config", collectionName))
                                                        .withMethod(SolrRequest.METHOD.POST)
                                                        .withPayload(actionPayLoad)
                                                        .build();

            return validateResponseForSuccess(v2request.process(solrClient));

        case HTTP:
            Collection<ContentStream> contentStreams = ClientUtils.toContentStreams(actionPayLoad, "application/json; charset=UTF-8");
            GenericSolrRequest        request        = new GenericSolrRequest(SolrRequest.METHOD.POST, String.format("/%s/config", collectionName), null);

            request.setContentStreams(contentStreams);
            request.setUseV2(false);

            return validateResponseForSuccess(request.process(solrClient));

        default:
            throw new IllegalArgumentException("Unsupported Solr operation mode: " + mode);
    }
}
 
Example #2
Source File: AbstractSolrSentryTestBase.java    From incubator-sentry with Apache License 2.0 6 votes vote down vote up
/**
 * Method to validate Solr update passes
 * @param solrUserName - User authenticated into Solr
 * @param collectionName - Name of the collection to which the data has to be updated
 * @param solrInputDoc - Instance of SolrInputDocument
 * @throws Exception
 */
protected void verifyUpdatePass(String solrUserName,
                                String collectionName,
                                SolrInputDocument solrInputDoc) throws Exception {
  String originalUser = getAuthenticatedUser();
  try {
    SolrDocumentList orginalSolrDocs = getSolrDocs(collectionName, ALL_DOCS, true);
    setAuthenticationUser(solrUserName);
    CloudSolrServer cloudSolrServer = getCloudSolrServer(collectionName);
    try {
      cloudSolrServer.add(solrInputDoc);
      cloudSolrServer.commit();
    } finally {
      cloudSolrServer.shutdown();
    }

    orginalSolrDocs.add(ClientUtils.toSolrDocument(solrInputDoc));
    SolrDocumentList solrRespDocs = getSolrDocs(collectionName, ALL_DOCS, true);
    // Validate Solr content to check whether the update command went through.
    validateSolrDocCountAndContent(orginalSolrDocs, solrRespDocs);
  }
  finally {
    setAuthenticationUser(originalUser);
  }
}
 
Example #3
Source File: LuceneIndexHandler.java    From FXDesktopSearch with Apache License 2.0 6 votes vote down vote up
public UpdateCheckResult checkIfModified(final String aFilename, final long aLastModified) throws IOException {

        final Map<String, Object> theParams = new HashMap<>();
        theParams.put("q", IndexFields.UNIQUEID + ":" + ClientUtils.escapeQueryChars(aFilename));

        try {
            final var theQueryResponse = solrClient.query(new SearchMapParams(theParams));
            if (theQueryResponse.getResults() == null || theQueryResponse.getResults().isEmpty()) {
                // Nothing in Index, hence mark it as updated
                return UpdateCheckResult.UPDATED;
            }
            final var theDocument = theQueryResponse.getResults().get(0);

            final long theStoredLastModified = Long.valueOf((String) theDocument.getFieldValue(IndexFields.LASTMODIFIED));
            if (theStoredLastModified != aLastModified) {
                return UpdateCheckResult.UPDATED;
            }
            return UpdateCheckResult.UNMODIFIED;
        } catch (final Exception e) {
            throw new IOException(e);
        }
    }
 
Example #4
Source File: DocumentShrinker.java    From thoth with BSD 3-Clause Clear License 6 votes vote down vote up
/**
 * Tag slower documents and add them to the shrank core
 */
private void tagAndAddSlowThothDocuments() throws IOException, SolrServerException {
  // Query to return top MAX_NUMBER_SLOW_THOTH_DOCS slower thoth documents
  QueryResponse qr = realTimeServer.query(
      new SolrQuery()
          .setQuery(createThothDocsAggregationQuery())
          .addSort(QTIME, SolrQuery.ORDER.desc)
          .setRows(MAX_NUMBER_SLOW_THOTH_DOCS)
  );

  for (SolrDocument solrDocument: qr.getResults()){
    SolrInputDocument si = ClientUtils.toSolrInputDocument(solrDocument);
    // Remove old ID and version
    si.removeField(ID);
    si.removeField("_version_");
    // Tag document as slow
    si.addField(SLOW_QUERY_DOCUMENT, true);
    LOG.debug("Adding slow query document for server " + serverDetail.getName());
    shrankServer.add(si);
  }
}
 
Example #5
Source File: SolrUtil.java    From ranger with Apache License 2.0 6 votes vote down vote up
public String andList(String fieldName, Collection<?> valueList) {
	if (valueList == null || valueList.isEmpty()) {
		return null;
	}
	String expr = "";
	int count = -1;
	for (Object value : valueList) {
		count++;
		if (count > 0) {
			expr += " AND ";
		}
		expr += fieldName
				+ ":"
				+ ClientUtils.escapeQueryChars(value.toString()
						.toLowerCase());
	}
	if (valueList.isEmpty()) {
		return expr;
	} else {
		return "(" + expr + ")";
	}
}
 
Example #6
Source File: SolrUtil.java    From ranger with Apache License 2.0 6 votes vote down vote up
public String orList(String fieldName, Collection<?> valueList) {
	if (valueList == null || valueList.isEmpty()) {
		return null;
	}
	String expr = "";
	int count = -1;
	for (Object value : valueList) {
		count++;
		if (count > 0) {
			expr += " OR ";
		}
		expr += fieldName
				+ ":"
				+ ClientUtils.escapeQueryChars(value.toString()
						.toLowerCase());
	}
	if (valueList.isEmpty()) {
		return expr;
	} else {
		return "(" + expr + ")";
	}

}
 
Example #7
Source File: ChildDocTransformerFactory.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
protected static String processPathHierarchyQueryString(String queryString) {
  // if the filter includes a path string, build a lucene query string to match those specific child documents.
  // e.g. /toppings/ingredients/name_s:cocoa -> +_nest_path_:/toppings/ingredients +(name_s:cocoa)
  // ingredients/name_s:cocoa -> +_nest_path_:*/ingredients +(name_s:cocoa)
  int indexOfFirstColon = queryString.indexOf(':');
  if (indexOfFirstColon <= 0) {
    return queryString;// give up
  }
  int indexOfLastPathSepChar = queryString.lastIndexOf(PATH_SEP_CHAR, indexOfFirstColon);
  if (indexOfLastPathSepChar < 0) {
    // regular filter, not hierarchy based.
    return ClientUtils.escapeQueryChars(queryString.substring(0, indexOfFirstColon))
        + ":" + ClientUtils.escapeQueryChars(queryString.substring(indexOfFirstColon + 1));
  }
  final boolean isAbsolutePath = queryString.charAt(0) == PATH_SEP_CHAR;
  String path = ClientUtils.escapeQueryChars(queryString.substring(0, indexOfLastPathSepChar));
  String remaining = queryString.substring(indexOfLastPathSepChar + 1); // last part of path hierarchy

  return
      "+" + NEST_PATH_FIELD_NAME + (isAbsolutePath? ":": ":*\\/") + path
      + " +(" + remaining + ")";
}
 
Example #8
Source File: JsonQueryRequestUnitTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private String writeRequestToJson(JsonQueryRequest request, boolean trimWhitespace) {
  final RequestWriter.ContentWriter writer = request.getContentWriter(ClientUtils.TEXT_JSON);
  final ByteArrayOutputStream os = new ByteArrayOutputStream();
  try {
    writer.write(os);
    final String rawJsonString = new String(os.toByteArray(), StandardCharsets.UTF_8);
    // Trimming whitespace makes our assertions in these tests more stable (independent of JSON formatting) so we do
    // it by default.  But we leave the option open in case the JSON fields have spaces.
    if (trimWhitespace) {
      return rawJsonString.replaceAll("\n", "").replaceAll(" ","");
    } else {
      return rawJsonString;
    }
  } catch (IOException e) {
    /* Unreachable in practice, since we're not doing any I/O here */
    throw new RuntimeException(e);
  }
}
 
Example #9
Source File: SolrParams.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Generates a local-params string of the form <pre>{! name=value name2=value2}</pre>.
 */
public String toLocalParamsString() {
  final StringBuilder sb = new StringBuilder(128);
  sb.append("{!");
  //TODO perhaps look for 'type' and add here?  but it doesn't matter.
  for (final Iterator<String> it = getParameterNamesIterator(); it.hasNext();) {
    final String name = it.next();
    for (String val : getParams(name)) {
      sb.append(' '); // do so even the first time; why not.
      sb.append(name); // no escaping for name; it must follow "Java Identifier" rules.
      sb.append('=');
      sb.append(ClientUtils.encodeLocalParamVal(val));
    }
  }
  sb.append('}');
  return sb.toString();
}
 
Example #10
Source File: RequestWriter.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Use this to do a push writing instead of pull. If this method returns null
 * {@link org.apache.solr.client.solrj.request.RequestWriter#getContentStreams(SolrRequest)} is
 * invoked to do a pull write.
 */
public ContentWriter getContentWriter(@SuppressWarnings({"rawtypes"})SolrRequest req) {
  if (req instanceof UpdateRequest) {
    UpdateRequest updateRequest = (UpdateRequest) req;
    if (isEmpty(updateRequest)) return null;
    return new ContentWriter() {
      @Override
      public void write(OutputStream os) throws IOException {
        OutputStreamWriter writer = new OutputStreamWriter(os, StandardCharsets.UTF_8);
        updateRequest.writeXML(writer);
        writer.flush();
      }

      @Override
      public String getContentType() {
        return ClientUtils.TEXT_XML;
      }
    };
  }
  return req.getContentWriter(ClientUtils.TEXT_XML);
}
 
Example #11
Source File: DocumentAnalysisRequest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public RequestWriter.ContentWriter getContentWriter(String expectedType) {

  return new RequestWriter.ContentWriter() {
    @Override
    public void write(OutputStream os) throws IOException {
      OutputStreamWriter outputStreamWriter = new OutputStreamWriter(os, UTF_8);
      try {
        getXML(outputStreamWriter);
      } finally {
        outputStreamWriter.flush();
      }
    }

    @Override
    public String getContentType() {
      return ClientUtils.TEXT_XML;
    }
  };

}
 
Example #12
Source File: AutocompleteUpdateRequestProcessor.java    From solr-autocomplete with Apache License 2.0 6 votes vote down vote up
private SolrInputDocument fetchExistingOrCreateNewSolrDoc(String id) throws SolrServerException, IOException {
  Map<String, String> p = new HashMap<String, String>();
  p.put("q", PHRASE + ":\"" + ClientUtils.escapeQueryChars(id) + "\"");
  
  SolrParams params = new MapSolrParams(p);
  
  QueryResponse res = solrAC.query(params);
  
  if (res.getResults().size() == 0) {
    return new SolrInputDocument();
  } else if (res.getResults().size() == 1) {
    SolrDocument doc = res.getResults().get(0);
    SolrInputDocument tmp = new SolrInputDocument();
    
    for (String fieldName : doc.getFieldNames()) {
      tmp.addField(fieldName, doc.getFieldValue(fieldName));
    }
    return tmp;
  } else {
    throw new IllegalStateException("Query with params : " + p + " returned more than 1 hit!");
  }
}
 
Example #13
Source File: SolrExprUtil.java    From scipio-erp with Apache License 2.0 6 votes vote down vote up
/**
     * Escapes all special solr/query characters in the given query term
     * <em>not</em> enclosed in quotes (single term).
     * At current time, this includes at least:
     * <code>+ - && || ! ( ) { } [ ] ^ " ~ * ? : \ /</code> and whitespace.
     * NOTE: The result should NOT be enclosed in quotes; use {@link #escapeTermForQuote} for that.
     * FIXME?: whitespace escaping appears to not always be honored by solr parser?...
     * @see #escapeTermForQuote
     */
    public static String escapeTermPlain(String term) {
        return ClientUtils.escapeQueryChars(term);
        // Reference implementation:
//        StringBuilder sb = new StringBuilder();
//        for (int i = 0; i < s.length(); i++) {
//          char c = s.charAt(i);
//          // These characters are part of the query syntax and must be escaped
//          if (c == '\\' || c == '+' || c == '-' || c == '!'  || c == '(' || c == ')' || c == ':'
//            || c == '^' || c == '[' || c == ']' || c == '\"' || c == '{' || c == '}' || c == '~'
//            || c == '*' || c == '?' || c == '|' || c == '&'  || c == ';' || c == '/'
//            || Character.isWhitespace(c)) {
//            sb.append('\\');
//          }
//          sb.append(c);
//        }
//        return sb.toString();
    }
 
Example #14
Source File: SolrSearchServer.java    From vind with Apache License 2.0 6 votes vote down vote up
private IndexResult indexSingleDocument(Document doc, int withinMs) {
    final SolrInputDocument document = createInputDocument(doc);
    try {
        if (solrClientLogger.isTraceEnabled()) {
            solrClientLogger.debug(">>> add({}): {}", doc.getId(), ClientUtils.toXML(document));
        } else {
            solrClientLogger.debug(">>> add({})", doc.getId());
        }

        removeNonParentDocument(doc, withinMs);
        final UpdateResponse response = withinMs < 0 ? this.solrClient.add(document) : this.solrClient.add(document, withinMs);
        return new IndexResult(Long.valueOf(response.getQTime())).setElapsedTime(response.getElapsedTime());

    } catch (SolrServerException | IOException e) {
        log.error("Cannot index document {}", document.getField(ID) , e);
        throw new SearchServerException("Cannot index document", e);
    }
}
 
Example #15
Source File: MCRNormalizeMCRObjectIDsFilter.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
private String retrieveMCRDerIDfromSOLR(String mcrid, String derid) {
    String key = derid.substring(0, derid.indexOf(":"));
    String value = derid.substring(derid.indexOf(":") + 1);
    if (SEARCHKEYS_FOR_DERIVATES.contains(key)) {
        ModifiableSolrParams params = new ModifiableSolrParams();
        params.set("start", 0);
        params.set("rows", 1);
        params.set("fl", "id");
        params.set("fq", "objectKind:mycorederivate");
        params.set("fq", "returnId:" + mcrid);
        params.set("q", key + ":" + ClientUtils.escapeQueryChars(value));
        params.set("sort", "derivateOrder asc");
        QueryResponse solrResponse = null;
        try {
            solrResponse = MCRSolrClientFactory.getMainSolrClient().query(params);
        } catch (Exception e) {
            LOGGER.error("Error retrieving derivate id from SOLR", e);
        }
        if (solrResponse != null) {
            SolrDocumentList solrResults = solrResponse.getResults();
            if (solrResults.getNumFound() == 1) {
                return String.valueOf(solrResults.get(0).getFieldValue("id"));
            }
            if (solrResults.getNumFound() == 0) {
                throw new NotFoundException("No MyCoRe Derivate ID found for query " + derid);
            }
            if (solrResults.getNumFound() > 1) {
                throw new BadRequestException(
                    "The query " + derid + " does not return a unique MyCoRe Derivate ID");
            }
        }
    }
    return derid;
}
 
Example #16
Source File: AbstractSolrSentryTestBase.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
/**
 * Function to validate the count and content of two SolrDocumentList's.
 * @param solrOriginalDocs - Instance of initial set of solr docs before processing
 * @param solrResponseDocs - Instance of response solr docs after processing
 */
protected void validateSolrDocCountAndContent(SolrDocumentList solrOriginalDocs,
                                              SolrDocumentList solrResponseDocs) {
  assertEquals("Expected number of Solr docs: " + solrOriginalDocs.size() + "; But found:" + solrResponseDocs.size(),
      solrOriginalDocs.size(), solrResponseDocs.size());
  for (SolrDocument solrDoc : solrOriginalDocs) {
    SolrInputDocument solrInputDoc = ClientUtils.toSolrInputDocument(solrDoc);
    validateSolrDocContent(solrInputDoc, solrResponseDocs);
  }
}
 
Example #17
Source File: GetSolr.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void process(OutputStream out) throws IOException {
    for (SolrDocument doc : response.getResults()) {
        String xml = ClientUtils.toXML(toSolrInputDocument(doc));
        IOUtils.write(xml, out, StandardCharsets.UTF_8);
    }
}
 
Example #18
Source File: SolrClient.java    From oodt with Apache License 2.0 5 votes vote down vote up
/**
 * Method to query the Solr index for a product with the specified name.
 * @param name
 * @param mimeType
 * @return
 */
public String queryProductByName(String name, String mimeType) throws CatalogException {

	ConcurrentHashMap<String, String[]> params = new ConcurrentHashMap<String, String[]>();
	params.put("q", new String[]{Parameters.PRODUCT_NAME+":"+ ClientUtils.escapeQueryChars(name)} );
	return query(params, mimeType);

}
 
Example #19
Source File: SolrUtils.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void process(OutputStream out) throws IOException {
    IOUtils.write("<docs>", out, StandardCharsets.UTF_8);
    for (SolrDocument doc : response.getResults()) {
        final String xml = ClientUtils.toXML(toSolrInputDocument(doc));
        IOUtils.write(xml, out, StandardCharsets.UTF_8);
    }
    IOUtils.write("</docs>", out, StandardCharsets.UTF_8);
}
 
Example #20
Source File: TextIndexSolr5.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, Node> get(String uri) {
	String escaped = ClientUtils.escapeQueryChars(uri) ;
	String qs = docDef.getEntityField() + ":" + escaped ;
	SolrDocumentList solrResults = solrQuery(qs, 1) ;

	List<Map<String, Node>> records = process(solrResults) ;
	if ( records.size() == 0 )
		return null ;
	if ( records.size() > 1 )
		log.warn("Multiple docs for one URI: " + uri) ;
	return records.get(0) ;
}
 
Example #21
Source File: SolrSearchEngine.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
protected String getQueryUrl(SolrQuery query, String baseUrl) {
	StringBuilder queryUrl = new StringBuilder(baseUrl);
	if (StringUtils.isBlank(query.getRequestHandler())) {
		queryUrl.append("/select");
	} else {
		queryUrl.append(query.getRequestHandler());
	}
	queryUrl.append(ClientUtils.toQueryString(query, false));

	return queryUrl.toString();
}
 
Example #22
Source File: SolrSearchEngine.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
protected String getQueryUrl(SolrQuery query, String baseUrl) {
	StringBuilder queryUrl = new StringBuilder(baseUrl);
	if (StringUtils.isBlank(query.getRequestHandler())) {
		queryUrl.append("/select");
	} else {
		queryUrl.append(query.getRequestHandler());
	}
	queryUrl.append(ClientUtils.toQueryString(query, false));

	return queryUrl.toString();
}
 
Example #23
Source File: DateTimeConverters.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Override
public String convert(ReadableInstant source) {
	if (source == null) {
		return null;
	}
	return (ClientUtils.escapeQueryChars(FORMATTER.print(source.getMillis())));
}
 
Example #24
Source File: SolrUtil.java    From ranger with Apache License 2.0 5 votes vote down vote up
public String setField(String fieldName, Object value) {
	if (value == null || value.toString().trim().length() == 0) {
		return null;
	}
	return fieldName
			+ ":"
			+ ClientUtils.escapeQueryChars(value.toString().trim()
					.toLowerCase());
}
 
Example #25
Source File: SolrMetaAlertSearchDao.java    From metron with Apache License 2.0 5 votes vote down vote up
@Override
public GroupResponse group(GroupRequest groupRequest) throws InvalidSearchException {
  // Make sure to escape any problematic characters here
  String sourceType = ClientUtils.escapeQueryChars(config.getSourceTypeField());
  String baseQuery = groupRequest.getQuery();
  String adjustedQuery = baseQuery + " -" + MetaAlertConstants.METAALERT_FIELD + ":[* TO *]"
      + " -" + sourceType + ":" + MetaAlertConstants.METAALERT_TYPE;
  LOG.debug("MetaAlert group adjusted query: {}", adjustedQuery);
  groupRequest.setQuery(adjustedQuery);
  return solrSearchDao.group(groupRequest);
}
 
Example #26
Source File: SolrSearchEngine.java    From testcontainers-java with MIT License 5 votes vote down vote up
@SneakyThrows
public SearchResult search(String term) {

    SolrQuery query = new SolrQuery();
    query.setQuery("title:" + ClientUtils.escapeQueryChars(term));
    QueryResponse response = client.query(COLLECTION_NAME, query);
    return createResult(response);
}
 
Example #27
Source File: MaxSizeAutoCommitTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Convert the given list of strings into a list of streams, for Solr update requests
 * @param strs strings to convert into streams
 * @return list of streams
 */
private List<ContentStream> toContentStreams(List<String> strs) {
  ArrayList<ContentStream> streams = new ArrayList<>();
  for (String str : strs) {
    streams.addAll(ClientUtils.toContentStreams(str, "text/xml"));
  }
  return streams;
}
 
Example #28
Source File: DateTimeConverters.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Override
public String convert(LocalDateTime source) {
	if (source == null) {
		return null;
	}
	return ClientUtils.escapeQueryChars(FORMATTER.print(source.toDateTime(DateTimeZone.UTC).getMillis()));
}
 
Example #29
Source File: SolrTestCaseJ4.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Generates a simple &lt;add&gt;&lt;doc&gt;... XML String with no options
 */
public static String adoc(SolrInputDocument sdoc) {
  StringWriter out = new StringWriter(512);
  try {
    out.append("<add>");
    ClientUtils.writeXML(sdoc, out);
    out.append("</add>");
  } catch (IOException e) {
    throw new RuntimeException("Inexplicable IO error from StringWriter", e);
  }
  return out.toString();
}
 
Example #30
Source File: MCRNormalizeMCRObjectIDsFilter.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
private String retrieveMCRObjIDfromSOLR(String mcrid) {
    String key = mcrid.substring(0, mcrid.indexOf(":"));
    String value = mcrid.substring(mcrid.indexOf(":") + 1);
    if (SEARCHKEYS_FOR_OBJECTS.contains(key)) {
        ModifiableSolrParams params = new ModifiableSolrParams();
        params.set("start", 0);
        params.set("rows", 1);
        params.set("fl", "id");
        params.set("fq", "objectKind:mycoreobject");
        params.set("q", key + ":" + ClientUtils.escapeQueryChars(value));
        QueryResponse solrResponse = null;
        try {
            solrResponse = MCRSolrClientFactory.getMainSolrClient().query(params);
        } catch (Exception e) {
            LOGGER.error("Error retrieving object id from SOLR", e);
        }
        if (solrResponse != null) {
            SolrDocumentList solrResults = solrResponse.getResults();
            if (solrResults.getNumFound() == 1) {
                return String.valueOf(solrResults.get(0).getFieldValue("id"));
            }
            if (solrResults.getNumFound() == 0) {
                throw new NotFoundException("No MyCoRe ID found for query " + mcrid);
            }
            if (solrResults.getNumFound() > 1) {
                throw new BadRequestException("The query " + mcrid + " does not return a unique MyCoRe ID");
            }
        }
    }
    return mcrid;
}