List of usage examples for org.apache.solr.client.solrj.util ClientUtils escapeQueryChars
public static String escapeQueryChars(String s)
From source file:com.smartitengineering.cms.spi.impl.content.search.ContentSearcherImpl.java
License:Open Source License
@Override public SearchResult<Content> search(Filter filter) { final StringBuilder finalQuery = new StringBuilder(); String disjunctionSeperator = " OR "; String conjunctionSeperator = " AND "; String seperator = filter.isDisjunction() ? disjunctionSeperator : conjunctionSeperator; int count = 0; Set<ContentTypeId> contentTypeIds = filter.getContentTypeFilters(); finalQuery.append(SolrFieldNames.TYPE).append(": ").append(ContentHelper.CONTENT); final WorkspaceId workspaceId = filter.getWorkspaceId(); if (workspaceId != null) { finalQuery.append(conjunctionSeperator); finalQuery.append((" (")); finalQuery.append(SolrFieldNames.WORKSPACEID).append(": ") .append(ClientUtils.escapeQueryChars(workspaceId.toString())); if (filter.isFriendliesIncluded()) { Collection<WorkspaceId> friendlies = workspaceId.getWorkspae().getFriendlies(); if (friendlies != null && !friendlies.isEmpty()) { finalQuery.append(disjunctionSeperator).append("(private: false AND ("); boolean first = true; for (WorkspaceId friendly : friendlies) { if (friendly != null) { if (first) { first = false; } else { finalQuery.append(disjunctionSeperator); }/* w ww. j a va2 s. c o m*/ finalQuery.append(SolrFieldNames.WORKSPACEID).append(": ") .append(ClientUtils.escapeQueryChars(friendly.toString())); } } finalQuery.append("))"); } } finalQuery.append((") ")); } final StringBuilder query = new StringBuilder(); if (contentTypeIds != null && !contentTypeIds.isEmpty()) { if (query.length() > 0) { query.append(seperator); } query.append("("); } for (ContentTypeId contentTypeId : contentTypeIds) { if (count > 0) { query.append(disjunctionSeperator); } if (contentTypeId != null) { query.append(SolrFieldNames.INSTANCE_OF).append(": ") .append(ClientUtils.escapeQueryChars(contentTypeId.toString())); } count++; } if (contentTypeIds != null && !contentTypeIds.isEmpty()) { query.append(")"); } if (filter.getCreationDateFilter() != null) { if (query.length() > 0) { query.append(seperator); } QueryParameter<Date> creationDateFilter = filter.getCreationDateFilter(); String queryStr = generateDateQuery(SolrFieldNames.CREATIONDATE, creationDateFilter); query.append(queryStr); } if (filter.getLastModifiedDateFilter() != null) { if (query.length() > 0) { query.append(seperator); } QueryParameter<Date> lastModifiedDateFilter = filter.getLastModifiedDateFilter(); String queryStr = generateDateQuery(SolrFieldNames.LASTMODIFIEDDATE, lastModifiedDateFilter); query.append(queryStr); } if (StringUtils.isNotBlank(filter.getSearchTerms())) { if (query.length() > 0) { query.append(seperator); } query.append(SolrFieldNames.ALL_TEXT).append(": ") .append(ClientUtils.escapeQueryChars(filter.getSearchTerms())); } Set<ContentStatus> statuses = filter.getStatusFilters(); for (ContentStatus contentStatus : statuses) { if (query.length() > 0) { query.append(seperator); } if (StringUtils.isNotBlank(contentStatus.getName())) { query.append(SolrFieldNames.STATUS).append(": ") .append(ClientUtils.escapeQueryChars(contentStatus.getName())); } } Collection<QueryParameter> fieldQuery = filter.getFieldFilters(); if (fieldQuery != null && !fieldQuery.isEmpty()) { for (QueryParameter parameter : fieldQuery) { if (parameter.getParameterType().equals(ParameterType.PARAMETER_TYPE_PROPERTY) && parameter instanceof StringLikeQueryParameter) { if (query.length() > 0) { query.append(seperator); } StringLikeQueryParameter param = QueryParameterCastHelper.STRING_PARAM_HELPER.cast(parameter); query.append(param.getPropertyName()).append(": ") .append(ClientUtils.escapeQueryChars(param.getValue())); } } } if (query.length() > 0) { finalQuery.append(conjunctionSeperator).append('(').append(query.toString()).append(')'); } if (logger.isInfoEnabled()) { logger.info("Query q = " + finalQuery.toString()); } final com.smartitengineering.common.dao.search.SearchResult<Content> searchResult = textSearchDao .detailedSearch(QueryParameterFactory.getStringLikePropertyParam("q", finalQuery.toString()), QueryParameterFactory.getFirstResultParam(filter.getStartFrom()), QueryParameterFactory.getMaxResultsParam(filter.getMaxContents())); final Collection<Content> result; if (searchResult == null || searchResult.getResult() == null || searchResult.getResult().isEmpty()) { result = Collections.emptyList(); } else { result = new ArrayList<Content>(); for (Content content : searchResult.getResult()) { if (content != null) { result.add(content); } } } return SmartContentAPI.getInstance().getContentLoader().createSearchResult(result, searchResult.getTotalResults()); }
From source file:com.smartitengineering.cms.spi.impl.type.search.ContentTypeSearcherImpl.java
License:Open Source License
@Override public SearchResult<ContentType> search(Filter filter) { final StringBuilder finalQuery = new StringBuilder(); String disjunctionSeperator = " OR "; String conjunctionSeperator = " AND "; String seperator = filter.isDisjunction() ? disjunctionSeperator : conjunctionSeperator; int count = 0; finalQuery.append(SolrFieldNames.TYPE).append(": ").append(ContentTypeHelper.CONTENT_TYPE); final WorkspaceId workspaceId = filter.getWorkspaceId(); if (workspaceId != null) { finalQuery.append(conjunctionSeperator); finalQuery.append((" (")); finalQuery.append(SolrFieldNames.WORKSPACEID).append(": ") .append(ClientUtils.escapeQueryChars(workspaceId.toString())); if (filter.isFriendliesIncluded()) { Collection<WorkspaceId> friendlies = workspaceId.getWorkspae().getFriendlies(); if (friendlies != null && !friendlies.isEmpty()) { finalQuery.append(disjunctionSeperator).append("(private: false AND ("); boolean first = true; for (WorkspaceId friendly : friendlies) { if (friendly != null) { if (first) { first = false; } else { finalQuery.append(disjunctionSeperator); }/*ww w . j ava 2 s . c o m*/ finalQuery.append(SolrFieldNames.WORKSPACEID).append(": ") .append(ClientUtils.escapeQueryChars(friendly.toString())); } } finalQuery.append("))"); } } finalQuery.append((") ")); } final StringBuilder query = new StringBuilder(); ContentTypeId parentId = filter.getChildOf(); if (parentId != null) { if (query.length() > 0) { query.append(seperator); } query.append(SolrFieldNames.CONTENTTYPEID).append(": ") .append(ClientUtils.escapeQueryChars(parentId.toString())); } Set<ContentTypeId> contentTypeIds = filter.getInstanceOfContentTypeFilters(); if (contentTypeIds != null && !contentTypeIds.isEmpty()) { if (query.length() > 0) { query.append(seperator); } query.append("("); } for (ContentTypeId contentTypeId : contentTypeIds) { if (count > 0) { query.append(disjunctionSeperator); } if (contentTypeId != null) { query.append(SolrFieldNames.INSTANCE_OF).append(": ") .append(ClientUtils.escapeQueryChars(contentTypeId.toString())); } count++; } if (contentTypeIds != null && !contentTypeIds.isEmpty()) { query.append(")"); } if (filter.getCreationDateFilter() != null) { if (query.length() > 0) { query.append(seperator); } QueryParameter<Date> creationDateFilter = filter.getCreationDateFilter(); String queryStr = ContentSearcherImpl.generateDateQuery(SolrFieldNames.CREATIONDATE, creationDateFilter); query.append(queryStr); } if (filter.getLastModifiedDateFilter() != null) { if (query.length() > 0) { query.append(seperator); } QueryParameter<Date> lastModifiedDateFilter = filter.getLastModifiedDateFilter(); String queryStr = ContentSearcherImpl.generateDateQuery(SolrFieldNames.LASTMODIFIEDDATE, lastModifiedDateFilter); query.append(queryStr); } if (StringUtils.isNotBlank(filter.getSearchTerms())) { if (query.length() > 0) { query.append(seperator); } query.append(SolrFieldNames.ALL_TEXT).append(": ") .append(ClientUtils.escapeQueryChars(filter.getSearchTerms())); } if (query.length() > 0) { finalQuery.append(conjunctionSeperator).append('(').append(query.toString()).append(')'); } if (logger.isInfoEnabled()) { logger.info("Query q = " + finalQuery.toString()); } final com.smartitengineering.common.dao.search.SearchResult<ContentType> searchResult = textSearchDao .detailedSearch(QueryParameterFactory.getStringLikePropertyParam("q", finalQuery.toString()), QueryParameterFactory.getFirstResultParam(filter.getStartFrom()), QueryParameterFactory.getMaxResultsParam(filter.getMaxContents())); final Collection<ContentType> result; if (searchResult == null || searchResult.getResult() == null || searchResult.getResult().isEmpty()) { result = Collections.emptyList(); } else { result = new ArrayList<ContentType>(); for (ContentType content : searchResult.getResult()) { if (content != null) { result.add(content); } } } return SmartContentAPI.getInstance().getContentTypeLoader().createSearchResult(result, searchResult.getTotalResults()); }
From source file:com.thinkaurelius.titan.diskstorage.solr.Solr5Index.java
License:Apache License
private static String escapeValue(Object value) { return ClientUtils.escapeQueryChars(value.toString()); }
From source file:de.hybris.platform.commerceservices.search.solrfacetsearch.querybuilder.impl.AbstractFreeTextQueryBuilder.java
License:Open Source License
/** * Add a search term/*from w w w . jav a 2s . c om*/ * * @param searchQuery * the search query * @param field * the field to search in * @param value * the value to search for (not solr escaped) * @param suffixOp * suffix field operator * @param boost * the boost factor for the term */ @SuppressWarnings("deprecation") protected void addFreeTextQuery(final SearchQuery searchQuery, final String field, final String value, final String suffixOp, final double boost) { final RawQuery rawQuery = new RawQuery(field, ClientUtils.escapeQueryChars(value) + suffixOp + "^" + boost, Operator.OR); searchQuery.addRawQuery(rawQuery); }
From source file:edu.unc.lib.dl.cdr.services.rest.ContainerDataFileChecksumsController.java
License:Apache License
@RequestMapping(value = "/edit/fileinfo/{pid}") public void get(@PathVariable("pid") String pid, HttpServletResponse response) throws SolrServerException, IOException { String fid = pid.replace(":", "_"); response.addHeader("Content-Disposition", "attachment; filename=\"" + fid + "-fileinfo.csv\""); try (ServletOutputStream out = response.getOutputStream()) { out.print("title"); out.print(','); out.print("pid"); out.print(','); out.print("mimetype"); out.print(','); out.print("length"); out.print(','); out.println("checksum"); if (server == null) initializeSolrServer();//from ww w . j ava2 s . c om SolrQuery parameters = new SolrQuery(); parameters.setQuery("contentModel:" + ClientUtils.escapeQueryChars("info:fedora/cdr-model:Simple") + " ancestorPath:*" + ClientUtils.escapeQueryChars("," + pid + ",") + "*"); parameters.addSort("filesizeTotal", ORDER.desc); parameters.addField("title"); parameters.addField("id"); parameters.addField("datastream"); QueryResponse solrResponse = server.query(parameters); for (SolrDocument doc : solrResponse.getResults()) { Map<String, String> line = new HashMap<String, String>(); line.put("pid", (String) doc.getFieldValue("id")); line.put("title", (String) doc.getFieldValue("title")); String[] dsValues = new String[5]; for (Object ds : doc.getFieldValues("datastream")) { String dstr = (String) ds; if (dstr.startsWith("DATA_FILE|")) { dsValues = dstr.split(Pattern.quote("|")); break; } } line.put("md5sum", dsValues[4]); line.put("length", dsValues[3]); line.put("mimetype", dsValues[1]); outputCSV(line, out); } } }
From source file:edu.unc.lib.dl.cdr.services.rest.ContainerManifestController.java
License:Apache License
@SuppressWarnings("unchecked") @RequestMapping(value = "/edit/manifest-json/{pid}") public @ResponseBody Map<String, Object> getJSON(@PathVariable("pid") String pid) throws SolrServerException { if (server == null) initializeSolrServer();// w w w . ja va 2s .c o m SolrQuery parameters = new SolrQuery(); parameters.setQuery("ancestorPath:*" + ClientUtils.escapeQueryChars("," + pid + ",") + "*"); parameters.addSort("ancestorNames", ORDER.asc); parameters.addSort("isPart", ORDER.asc); parameters.addSort("displayOrder", ORDER.asc); parameters.addField("ancestorNames"); parameters.addField("ancestorPath"); parameters.addField("id"); parameters.addField("filesizeTotal"); parameters.addField("title"); QueryResponse solrResponse = server.query(parameters); // objects map is a local index of pid to map Map<String, Map<String, Object>> objects = new HashMap<String, Map<String, Object>>(); Map<String, Object> coll = new HashMap<String, Object>(); coll.put("pid", pid); String title = tripleStoreQueryService.lookupLabel(pid); coll.put("title", title); objects.put(pid, coll); for (SolrDocument doc : solrResponse.getResults()) { Map<String, Object> line = new HashMap<String, Object>(); objects.put((String) doc.getFieldValue("id"), line); Collection<Object> ancestorPaths = doc.getFieldValues("ancestorPath"); line.put("depth", String.valueOf(ancestorPaths.size())); // get parent id String[] ancestorPathsArray = ancestorPaths.toArray(new String[] {}); String lastAncestor = ancestorPathsArray[ancestorPathsArray.length - 1]; int start = lastAncestor.indexOf(",") + 1; int end = lastAncestor.indexOf(",", start); String parent = lastAncestor.substring(start, end); // file object record line.put("pid", doc.getFieldValue("id")); line.put("parentPid", parent); line.put("title", doc.getFieldValue("title")); line.put("filesizeTotal", doc.getFieldValue("filesizeTotal")); // TODO get checksum of data file } for (Map<String, Object> record : objects.values()) { if (pid.equals(record.get("pid"))) continue; String parentPid = (String) record.get("parentPid"); // file object as child Map<String, Object> parentHash = objects.get(parentPid); if (parentHash == null) { LOG.warn("Cannot find expected pid in index: {}", parentPid); continue; } List<Map<String, Object>> children = null; if (!parentHash.containsKey("children")) { children = new ArrayList<Map<String, Object>>(); parentHash.put("children", children); } else { children = (List<Map<String, Object>>) parentHash.get("children"); } children.add(record); } return coll; }
From source file:edu.unc.lib.dl.cdr.services.rest.ContainerManifestController.java
License:Apache License
@RequestMapping(value = "/edit/manifest-csv/{pid}") public void downloadCSV(@PathVariable("pid") String pid, HttpServletResponse response) throws SolrServerException, IOException { if (server == null) initializeSolrServer();/*w ww . j ava 2 s . com*/ SolrQuery parameters = new SolrQuery(); parameters.setQuery("ancestorPath:*" + ClientUtils.escapeQueryChars("," + pid + ",") + "*"); parameters.addSort("ancestorNames", ORDER.asc); parameters.addSort("isPart", ORDER.asc); parameters.addSort("displayOrder", ORDER.asc); parameters.addField("ancestorPath"); parameters.addField("ancestorNames"); parameters.addField("id"); parameters.addField("title"); QueryResponse solrResponse = server.query(parameters); String id = pid.replace(":", "_"); response.addHeader("Content-Disposition", "attachment; filename=\"" + id + "-manifest.csv\""); try (ServletOutputStream out = response.getOutputStream()) { out.print("depth"); out.print(','); out.print("pid"); out.print(','); out.println("title"); for (SolrDocument doc : solrResponse.getResults()) { String title = (String) doc.getFieldValue("title"); String p = (String) doc.getFieldValue("id"); String anc = (String) doc.getFieldValue("ancestorNames"); int depth = doc.getFieldValues("ancestorPath").size(); outputCSV(p, title, depth, anc, out); } } }
From source file:edu.unc.lib.dl.search.solr.util.SolrSettings.java
License:Apache License
public static String sanitize(String value) { if (value == null) return value; return escapeReservedWords.matcher(ClientUtils.escapeQueryChars(value)).replaceAll("'$1'"); }
From source file:eu.clarin.cmdi.vlo.importer.MetadataImporter.java
/** * Retrieve all files with VALID_CMDI_EXTENSIONS from all DataRoot entries * and starts processing for every single file * * @throws MalformedURLException//from w w w. j ava 2 s . c om */ void startImport() throws MalformedURLException { initSolrServer(); List<DataRoot> dataRoots = checkDataRoots(); dataRoots = filterDataRootsWithCLArgs(dataRoots); long start = System.currentTimeMillis(); try { // Delete the whole Solr db if (config.getDeleteAllFirst()) { LOG.info("Deleting original data..."); solrServer.deleteByQuery("*:*"); solrServer.commit(); LOG.info("Deleting original data done."); } // Import the specified data roots for (DataRoot dataRoot : dataRoots) { LOG.info("Start of processing: " + dataRoot.getOriginName()); if (dataRoot.deleteFirst()) { LOG.info("Deleting data for data provider: " + dataRoot.getOriginName()); solrServer.deleteByQuery(FacetConstants.FIELD_DATA_PROVIDER + ":" + ClientUtils.escapeQueryChars(dataRoot.getOriginName())); LOG.info("Deleting data of provider done."); } CMDIDataProcessor processor = new CMDIParserVTDXML(POST_PROCESSORS, config, false); List<List<File>> centreFilesList = getFilesFromDataRoot(dataRoot.getRootFile()); // import files from every endpoint for (List<File> centreFiles : centreFilesList) { LOG.info("Processing directory: {}", centreFiles.get(0).getParent()); // identify mdSelfLinks and remove too large files from center file list LOG.info("Extracting mdSelfLinks"); Set<String> mdSelfLinkSet = new HashSet<>(); Set<File> ignoredFileSet = new HashSet<>(); for (File file : centreFiles) { if (config.getMaxFileSize() > 0 && file.length() > config.getMaxFileSize()) { LOG.info("Skipping " + file.getAbsolutePath() + " because it is too large."); nrOfFilesTooLarge++; ignoredFileSet.add(file); } else { String mdSelfLink = null; try { mdSelfLink = processor.extractMdSelfLink(file); } catch (Exception e) { LOG.error("error in file: {}", file, e); nrOfFilesWithError++; } if (mdSelfLink != null) { mdSelfLinkSet.add(StringUtils.normalizeIdString(mdSelfLink)); } } } centreFiles.removeAll(ignoredFileSet); // inform structure graph about MdSelfLinks of all files in this collection ResourceStructureGraph.setOccurringMdSelfLinks(mdSelfLinkSet); LOG.info("...extracted {} mdSelfLinks", mdSelfLinkSet.size()); // process every file in this collection for (File file : centreFiles) { LOG.debug("PROCESSING FILE: {}", file.getAbsolutePath()); processCmdi(file, dataRoot, processor); } if (!docs.isEmpty()) { sendDocs(); } solrServer.commit(); if (config.isProcessHierarchies()) { updateDocumentHierarchy(); } } updateDaysSinceLastImport(dataRoot); LOG.info("End of processing: " + dataRoot.getOriginName()); } // delete outdated entries (based on maxDaysInSolr parameter) if (config.getMaxDaysInSolr() > 0 && config.getDeleteAllFirst() == false) { LOG.info("Deleting old files that were not seen for more than " + config.getMaxDaysInSolr() + " days..."); solrServer.deleteByQuery( FacetConstants.FIELD_LAST_SEEN + ":[* TO NOW-" + config.getMaxDaysInSolr() + "DAYS]"); LOG.info("Deleting old files done."); } } catch (SolrServerException e) { LOG.error("error updating files:\n", e); LOG.error("Also see vlo_solr server logs for more information"); } catch (IOException e) { LOG.error("error updating files:\n", e); } finally { try { if (solrServer != null) { solrServer.commit(); buildSuggesterIndex(); } } catch (SolrServerException | IOException e) { LOG.error("cannot commit:\n", e); } } long took = (System.currentTimeMillis() - start) / 1000; LOG.info("Found " + nrOfFilesWithoutId + " file(s) without an id. (id is generated based on fileName but that may not be unique)"); LOG.info("Found " + nrOfFilesWithError + " file(s) with errors."); LOG.info("Found " + nrOfFilesTooLarge + " file(s) too large."); LOG.info("Update of " + nrOFDocumentsSend + " took " + took + " secs. Total nr of files analyzed " + nrOfFilesAnalyzed); solrServer.shutdown(); }
From source file:eu.clarin.cmdi.vlo.importer.MetadataImporter.java
/** * Update "days since last import" field for all Solr records of dataRoot. * Notice that it will not touch records that have a "last seen" value newer * than today. Therefore this should be called <em>after</em> normal * processing of data root!//from w ww . j av a 2s . c o m * * @param dataRoot * @throws SolrServerException * @throws IOException */ private void updateDaysSinceLastImport(DataRoot dataRoot) throws SolrServerException, IOException { LOG.info("Updating \"days since last import\" in Solr for: {}", dataRoot.getOriginName()); SolrQuery query = new SolrQuery(); query.setQuery( //we're going to process all records in the current data root... FacetConstants.FIELD_DATA_PROVIDER + ":" + ClientUtils.escapeQueryChars(dataRoot.getOriginName()) + " AND " // ...that have a "last seen" value _older_ than today (on update/initialisation all records get 0 so we can skip the rest) + FacetConstants.FIELD_LAST_SEEN + ":[* TO NOW-1DAY]"); query.setFields(FacetConstants.FIELD_ID, FacetConstants.FIELD_LAST_SEEN); int fetchSize = 1000; query.setRows(fetchSize); QueryResponse rsp = solrServer.query(query); final long totalResults = rsp.getResults().getNumFound(); final LocalDate nowDate = LocalDate.now(); final int docsListSize = config.getMaxDocsInList(); List<SolrInputDocument> updateDocs = new ArrayList<>(docsListSize); Boolean updatedDocs = false; int offset = 0; while (offset < totalResults) { query.setStart(offset); query.setRows(fetchSize); for (SolrDocument doc : solrServer.query(query).getResults()) { updatedDocs = true; String recordId = (String) doc.getFieldValue(FacetConstants.FIELD_ID); Date lastImportDate = (Date) doc.getFieldValue(FacetConstants.FIELD_LAST_SEEN); LocalDate oldDate = lastImportDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); long daysSinceLastSeen = DAYS.between(oldDate, nowDate); SolrInputDocument updateDoc = new SolrInputDocument(); updateDoc.setField(FacetConstants.FIELD_ID, recordId); Map<String, Long> partialUpdateMap = new HashMap<>(); partialUpdateMap.put("set", daysSinceLastSeen); updateDoc.setField(FacetConstants.FIELD_DAYS_SINCE_LAST_SEEN, partialUpdateMap); updateDocs.add(updateDoc); if (updateDocs.size() == docsListSize) { solrServer.add(updateDocs); if (serverError != null) { throw new SolrServerException(serverError); } updateDocs = new ArrayList<>(docsListSize); } } offset += fetchSize; LOG.info("Updating \"days since last import\": {} out of {} records updated", offset, totalResults); } if (!updateDocs.isEmpty()) { solrServer.add(updateDocs); if (serverError != null) { throw new SolrServerException(serverError); } } if (updatedDocs) { solrServer.commit(); } LOG.info("Updating \"days since last import\" done."); }