List of usage examples for org.apache.solr.client.solrj.util ClientUtils escapeQueryChars
public static String escapeQueryChars(String s)
From source file:org.phenotips.data.indexing.internal.SolrPatientIndexer.java
License:Open Source License
@Override public void delete(Patient patient) { try {// w w w. ja va 2 s . c o m this.server.deleteByQuery("document:" + ClientUtils.escapeQueryChars(patient.getDocument().toString())); this.server.commit(); } catch (SolrServerException ex) { this.logger.warn("Failed to delete from Solr: {}", ex.getMessage()); } catch (IOException ex) { this.logger.warn("Error occurred while deleting Solr documents: {}", ex.getMessage()); } }
From source file:org.phenotips.data.indexing.internal.SolrPatientIndexerTest.java
License:Open Source License
@Test public void deleteDefaultBehaviourTest() throws IOException, SolrServerException { doReturn(this.patientDocReference).when(this.patient).getDocument(); this.patientIndexer.delete(this.patient); verify(this.server) .deleteByQuery("document:" + ClientUtils.escapeQueryChars(this.patientDocReference.toString())); verify(this.server).commit(); }
From source file:org.phenotips.ontology.internal.GeneNomenclature.java
License:Open Source License
private StringBuilder processQueryPart(StringBuilder query, Map.Entry<String, ?> field, boolean includeOperator) { if (Collection.class.isInstance(field.getValue()) && ((Collection<?>) field.getValue()).isEmpty()) { return query; }//from w w w . ja v a 2 s . co m if (Map.class.isInstance(field.getValue())) { if (QUERY_OPERATORS.containsKey(field.getKey())) { @SuppressWarnings("unchecked") Map.Entry<String, Map<String, ?>> subquery = (Map.Entry<String, Map<String, ?>>) field; return processSubquery(query, subquery); } else { this.logger.warn("Invalid subquery operator: {}", field.getKey()); return query; } } query.append(' '); if (includeOperator) { query.append(QUERY_OPERATORS.get(DEFAULT_OPERATOR)); } query.append(ClientUtils.escapeQueryChars(field.getKey())); query.append(":("); if (Collection.class.isInstance(field.getValue())) { for (Object value : (Collection<?>) field.getValue()) { String svalue = String.valueOf(value); if (svalue.endsWith(WILDCARD)) { svalue = ClientUtils.escapeQueryChars(StringUtils.removeEnd(svalue, WILDCARD)) + WILDCARD; } else { svalue = ClientUtils.escapeQueryChars(svalue); } query.append(svalue); query.append(' '); } } else { String svalue = String.valueOf(field.getValue()); if (svalue.endsWith(WILDCARD)) { svalue = ClientUtils.escapeQueryChars(StringUtils.removeEnd(svalue, WILDCARD)) + WILDCARD; } else { svalue = ClientUtils.escapeQueryChars(svalue); } query.append(svalue); } query.append(')'); return query; }
From source file:org.phenotips.ontology.internal.solr.AbstractSolrOntologyService.java
License:Open Source License
@Override public OntologyTerm getTerm(String id) { OntologyTerm result = this.externalServicesAccess.getCache().get(id); if (result == null) { ModifiableSolrParams params = new ModifiableSolrParams(); params.set(CommonParams.Q, ID_FIELD_NAME + ':' + ClientUtils.escapeQueryChars(id)); SolrDocumentList allResults = this.search(params); if (allResults != null && !allResults.isEmpty()) { result = new SolrOntologyTerm(allResults.get(0), this); this.externalServicesAccess.getCache().set(id, result); } else {/*from w w w . jav a2 s.c o m*/ this.externalServicesAccess.getCache().set(id, EMPTY_MARKER); } } return (result == EMPTY_MARKER) ? null : result; }
From source file:org.phenotips.ontology.internal.solr.AbstractSolrOntologyService.java
License:Open Source License
@Override public Set<OntologyTerm> getTerms(Collection<String> ids) { Set<OntologyTerm> result = new LinkedHashSet<OntologyTerm>(); StringBuilder query = new StringBuilder("id:("); for (String id : ids) { OntologyTerm cachedTerm = this.externalServicesAccess.getCache().get(id); if (cachedTerm != null) { if (cachedTerm != EMPTY_MARKER) { result.add(cachedTerm);//from ww w . j a va 2 s . c o m } } else { query.append(ClientUtils.escapeQueryChars(id)); query.append(' '); } } query.append(')'); // There's at least one more term not found in the cache if (query.length() > 5) { for (SolrDocument doc : this.search(SolrQueryUtils.transformQueryToSolrParams(query.toString()))) { result.add(new SolrOntologyTerm(doc, this)); } } return result; }
From source file:org.phenotips.ontology.internal.solr.AbstractSolrOntologyService.java
License:Open Source License
/** * Generate a Lucene query from a map of parameters, to be used in the "q" parameter for Solr. * * @param fieldValues a map with term meta-property values that must be matched by the returned terms; the keys are * property names, like {@code id}, {@code description}, {@code is_a}, and the values can be either a * single value, or a collection of values that can (OR) be matched by the term; * @return the String representation of the equivalent Lucene query *//*from w w w.jav a 2 s .c o m*/ protected String generateLuceneQuery(Map<String, ?> fieldValues) { StringBuilder query = new StringBuilder(); for (Map.Entry<String, ?> field : fieldValues.entrySet()) { if (Collection.class.isInstance(field.getValue()) && ((Collection<?>) field.getValue()).isEmpty()) { continue; } query.append("+"); query.append(ClientUtils.escapeQueryChars(field.getKey())); query.append(":("); if (Collection.class.isInstance(field.getValue())) { for (Object value : (Collection<?>) field.getValue()) { query.append(ClientUtils.escapeQueryChars(String.valueOf(value))); query.append(' '); } } else { String value = String.valueOf(field.getValue()); if ("*".equals(value)) { query.append(value); } else { query.append(ClientUtils.escapeQueryChars(value)); } } query.append(')'); } return query.toString(); }
From source file:org.phenotips.similarity.internal.SolrSimilarPatientsFinder.java
License:Open Source License
/** * Generates a Solr query that tries to match patients similar to the reference. * * @param referencePatient the reference patient * @return a query populated with terms from the patient phenotype *//*from w w w . j a v a 2 s . c om*/ private SolrQuery generateQuery(Patient referencePatient, boolean prototypes) { SolrQuery query = new SolrQuery(); StringBuilder q = new StringBuilder(); // FIXME This is a very basic implementation, to be revisited for (Feature phenotype : referencePatient.getFeatures()) { if (StringUtils.isNotBlank(phenotype.getId())) { q.append(phenotype.getType() + ":" + ClientUtils.escapeQueryChars(phenotype.getId()) + " "); } } // Ignore the reference patient itself (unless reference patient is a temporary in-memory only // patient, e.g. a RemoteMatchingPatient created from remote patient data obtained via remote-matching API) if (referencePatient.getDocument() != null) { q.append("-document:" + ClientUtils.escapeQueryChars(referencePatient.getDocument().toString())); } q.append(prototypes ? " +" : " -").append("document:xwiki\\:data.MIM*"); query.add(CommonParams.Q, q.toString()); //logger.error("SOLRQUERY generated: {}", query.toString()); return query; }
From source file:org.phenotips.termrequester.db.solr.SolrDatabaseService.java
License:Open Source License
@Override public Phenotype getPhenotype(Phenotype other) throws IOException { checkUp();/*w w w .java 2 s .c om*/ Set<String> names = other.getSynonyms(); names.add(other.getName()); List<String> queryPieces = new ArrayList<>(names.size() * 2 + 2); if (other.getId().isPresent()) { queryPieces.add(String.format(FIELD_IS, Schema.ID, ClientUtils.escapeQueryChars(other.getId().get()))); } if (other.getIssueNumber().isPresent()) { queryPieces.add(String.format(FIELD_IS, Schema.ISSUE_NUMBER, ClientUtils.escapeQueryChars(other.getIssueNumber().get()))); } for (String name : names) { queryPieces.add(String.format(FIELD_IS, Schema.NAME_EXACT, ClientUtils.escapeQueryChars(name))); queryPieces.add(String.format(FIELD_IS, Schema.SYNONYM_EXACT, ClientUtils.escapeQueryChars(name))); } String queryString = OR_QUERY_JOINER.join(queryPieces); SolrQuery q = new SolrQuery().setQuery(queryString).setRows(1); return runQuery(q); }
From source file:org.phenotips.termrequester.db.solr.SolrDatabaseService.java
License:Open Source License
@Override public List<Phenotype> searchPhenotypes(String text) throws IOException { checkUp();/*from ww w . j av a2 s .co m*/ try { SolrQuery q = new SolrQuery(); String escaped = ClientUtils.escapeQueryChars(text); q.add(CommonParams.Q, escaped); q.add(SpellingParams.SPELLCHECK_Q, text); q.add(DisMaxParams.PF, String.format("%s^20 %s^36 %s^100 %s^30 %s^15 %s^25 %s^70 %s^20 %s^3 %s^5", Schema.NAME, Schema.NAME_SPELL, Schema.NAME_EXACT, Schema.NAME_PREFIX, Schema.SYNONYM, Schema.SYNONYM_SPELL, Schema.SYNONYM_EXACT, Schema.SYNONYM_PREFIX, Schema.TEXT, Schema.TEXT_SPELL)); String qstring = String.format("%s^10 %s^18 %s^5 %s^6 %s^10 %s^3 %s^1 %s^2 %s^0.5", Schema.NAME, Schema.NAME_SPELL, Schema.NAME_STUB, Schema.SYNONYM, Schema.SYNONYM_SPELL, Schema.SYNONYM_STUB, Schema.TEXT, Schema.TEXT, Schema.TEXT_SPELL, Schema.TEXT_STUB); qstring = addStatusFilter(qstring, Phenotype.Status.SYNONYM); q.add(DisMaxParams.QF, qstring); q.add("spellcheck", Boolean.toString(true)); q.add(SpellingParams.SPELLCHECK_COLLATE, Boolean.toString(true)); q.add(SpellingParams.SPELLCHECK_COUNT, "100"); q.add(SpellingParams.SPELLCHECK_MAX_COLLATION_TRIES, "3"); q.add("lowercaseOperators", Boolean.toString(false)); q.add("defType", "edismax"); QueryResponse resp = server.query(q); List<SolrDocument> results = resp.getResults(); List<Phenotype> retval = new ArrayList<>(results.size()); for (SolrDocument doc : results) { retval.add(mapper.fromDoc(doc)); } return retval; } catch (SolrServerException e) { throw new IOException(e); } }
From source file:org.phenotips.variantstore.db.solr.SolrController.java
License:Open Source License
@Override public List<GAVariant> getTopHarmfulWithGene(String id, int n, String gene, List<String> variantEffects, Map<String, Double> alleleFrequencies) { List<GAVariant> list = new ArrayList<>(); checkNotNull(id);// w w w . j ava2 s. co m checkArgument(!"".equals(id)); checkArgument(n != 0); checkNotNull(gene); checkArgument(!"".equals(gene)); checkNotNull(variantEffects); checkArgument(variantEffects.size() != 0); checkNotNull(alleleFrequencies); checkArgument(alleleFrequencies.size() != 0); /** Build Query String **/ String effectQuery = ""; for (String effect : variantEffects) { effectQuery += String.format("%s:%s OR ", VariantsSchema.GENE_EFFECT, ClientUtils.escapeQueryChars(effect)); } // Strip final ' OR ' effectQuery = effectQuery.substring(0, effectQuery.length() - 4); // Find ExAC AF under the specified frequency, or where ExAC is null. String exacQuery = String.format("(-%s:[* TO *] AND *:*) OR %s:[0 TO %s] ", VariantsSchema.EXAC_AF, VariantsSchema.EXAC_AF, ClientUtils.escapeQueryChars(String.valueOf(alleleFrequencies.get(EXAC_FREQUENCY_FIELD)))); String queryString = String.format("s:%s AND %s:%s AND (%s) AND (%s)", VariantsSchema.CALLSET_IDS, ClientUtils.escapeQueryChars(id), VariantsSchema.GENE, ClientUtils.escapeQueryChars(gene), effectQuery, exacQuery); SolrQuery q = new SolrQuery().setRows(n).setQuery(queryString); QueryResponse resp = null; try { resp = server.query(q); } catch (SolrServerException | IOException e) { logger.error("Error getting individuals with variants", e); return list; } Map<String, List<GAVariant>> map = SolrVariantUtils .variantListToCallsetMap(SolrVariantUtils.documentListToMapList(resp.getResults())); if (map.containsKey(id)) { list = map.get(id); } return list; }