List of usage examples for org.apache.solr.common.params DisMaxParams PF
String PF
To view the source code for org.apache.solr.common.params DisMaxParams PF.
Click Source Link
From source file:org.alfresco.solr.component.spellcheck.AlfrescoSpellCheckCollator.java
License:Open Source License
public List<AlfrescoSpellCheckCollation> collate(SpellingResult result, String originalQuery, ResponseBuilder ultimateResponse) { List<AlfrescoSpellCheckCollation> collations = new ArrayList<>(); QueryComponent queryComponent = null; if (ultimateResponse.components != null) { for (SearchComponent sc : ultimateResponse.components) { if (sc instanceof QueryComponent) { queryComponent = (QueryComponent) sc; break; }/*from w w w. jav a 2s . c om*/ } } boolean verifyCandidateWithQuery = true; int maxTries = maxCollationTries; int maxNumberToIterate = maxTries; if (maxTries < 1) { maxTries = 1; maxNumberToIterate = maxCollations; verifyCandidateWithQuery = false; } if (queryComponent == null && verifyCandidateWithQuery) { LOG.info( "Could not find an instance of QueryComponent. Disabling collation verification against the index."); maxTries = 1; verifyCandidateWithQuery = false; } docCollectionLimit = docCollectionLimit > 0 ? docCollectionLimit : 0; int maxDocId = -1; if (verifyCandidateWithQuery && docCollectionLimit > 0) { IndexReader reader = ultimateResponse.req.getSearcher().getIndexReader(); maxDocId = reader.maxDoc(); } JSONObject alfrescoJSON = (JSONObject) ultimateResponse.req.getContext().get(AbstractQParser.ALFRESCO_JSON); String originalAftsQuery = alfrescoJSON != null ? alfrescoJSON.getString("query") : ultimateResponse.getQueryString(); int tryNo = 0; int collNo = 0; PossibilityIterator possibilityIter = new PossibilityIterator(result.getSuggestions(), maxNumberToIterate, maxCollationEvaluations, suggestionsMayOverlap); while (tryNo < maxTries && collNo < maxCollations && possibilityIter.hasNext()) { PossibilityIterator.RankedSpellPossibility possibility = possibilityIter.next(); String collationQueryStr = getCollation(originalQuery, possibility.corrections); int hits = 0; String aftsQuery = null; if (verifyCandidateWithQuery) { tryNo++; SolrQueryRequest req = ultimateResponse.req; SolrParams origParams = req.getParams(); ModifiableSolrParams params = new ModifiableSolrParams(origParams); Iterator<String> origParamIterator = origParams.getParameterNamesIterator(); int pl = SpellingParams.SPELLCHECK_COLLATE_PARAM_OVERRIDE.length(); while (origParamIterator.hasNext()) { String origParamName = origParamIterator.next(); if (origParamName.startsWith(SpellingParams.SPELLCHECK_COLLATE_PARAM_OVERRIDE) && origParamName.length() > pl) { String[] val = origParams.getParams(origParamName); if (val.length == 1 && val[0].length() == 0) { params.set(origParamName.substring(pl), (String[]) null); } else { params.set(origParamName.substring(pl), val); } } } // we don't set the 'q' param, as we'll pass the query via JSON. // params.set(CommonParams.Q, collationQueryStr); params.remove(CommonParams.START); params.set(CommonParams.ROWS, "" + docCollectionLimit); // we don't want any stored fields params.set(CommonParams.FL, "id"); // we'll sort by doc id to ensure no scoring is done. params.set(CommonParams.SORT, "_docid_ asc"); // If a dismax query, don't add unnecessary clauses for scoring params.remove(DisMaxParams.TIE); params.remove(DisMaxParams.PF); params.remove(DisMaxParams.PF2); params.remove(DisMaxParams.PF3); params.remove(DisMaxParams.BQ); params.remove(DisMaxParams.BF); // Collate testing does not support Grouping (see SOLR-2577) params.remove(GroupParams.GROUP); boolean useQStr = true; if (alfrescoJSON != null) { try { aftsQuery = originalAftsQuery.replaceAll(Pattern.quote(originalQuery), Matcher.quoteReplacement(collationQueryStr)); alfrescoJSON.put("query", aftsQuery); req.getContext().put(AbstractQParser.ALFRESCO_JSON, alfrescoJSON); useQStr = false; } catch (JSONException e) { LOG.warn("Exception trying to get/set the query from/to ALFRESCO_JSON.]" + e); } } else { aftsQuery = collationQueryStr; } req.setParams(params); // creating a request here... make sure to close it! ResponseBuilder checkResponse = new ResponseBuilder(req, new SolrQueryResponse(), Arrays.<SearchComponent>asList(queryComponent)); checkResponse.setQparser(ultimateResponse.getQparser()); checkResponse.setFilters(ultimateResponse.getFilters()); checkResponse.components = Arrays.<SearchComponent>asList(queryComponent); if (useQStr) { checkResponse.setQueryString(collationQueryStr); } try { queryComponent.prepare(checkResponse); if (docCollectionLimit > 0) { int f = checkResponse.getFieldFlags(); checkResponse.setFieldFlags(f |= SolrIndexSearcher.TERMINATE_EARLY); } queryComponent.process(checkResponse); hits = (Integer) checkResponse.rsp.getToLog().get("hits"); } catch (EarlyTerminatingCollectorException etce) { assert (docCollectionLimit > 0); assert 0 < etce.getNumberScanned(); assert 0 < etce.getNumberCollected(); if (etce.getNumberScanned() == maxDocId) { hits = etce.getNumberCollected(); } else { hits = (int) (((float) (maxDocId * etce.getNumberCollected())) / (float) etce.getNumberScanned()); } } catch (Exception e) { LOG.warn( "Exception trying to re-query to check if a spell check possibility would return any hits." + e); } finally { checkResponse.req.close(); } } if (hits > 0 || !verifyCandidateWithQuery) { collNo++; AlfrescoSpellCheckCollation collation = new AlfrescoSpellCheckCollation(); collation.setCollationQuery(aftsQuery); collation.setCollationQueryString(collationQueryStr); collation.setHits(hits); collation.setInternalRank( suggestionsMayOverlap ? ((possibility.rank * 1000) + possibility.index) : possibility.rank); NamedList<String> misspellingsAndCorrections = new NamedList<>(); for (SpellCheckCorrection corr : possibility.corrections) { misspellingsAndCorrections.add(corr.getOriginal().toString(), corr.getCorrection()); } collation.setMisspellingsAndCorrections(misspellingsAndCorrections); collations.add(collation); } if (LOG.isDebugEnabled()) { LOG.debug("Collation: " + aftsQuery + (verifyCandidateWithQuery ? (" will return " + hits + " hits.") : "")); } } return collations; }
From source file:org.phenotips.termrequester.db.solr.SolrDatabaseService.java
License:Open Source License
@Override public List<Phenotype> searchPhenotypes(String text) throws IOException { checkUp();//w ww . ja v a2 s . c o 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.vocabulary.internal.solr.AbstractCSVSolrVocabulary.java
License:Open Source License
protected VocabularyTerm requestTerm(String queryString, String phraseFields) { QueryResponse response;/*from w w w .j a va 2 s . c om*/ SolrQuery query = new SolrQuery(); SolrDocumentList termList; VocabularyTerm term; query.setQuery(queryString); query.setRows(1); if (phraseFields != null) { query.set(DisMaxParams.PF, phraseFields); } try { response = this.externalServicesAccess.getSolrConnection().query(query); termList = response.getResults(); if (!termList.isEmpty()) { term = new SolrVocabularyTerm(termList.get(0), this); return term; } } catch (SolrServerException | SolrException ex) { this.logger.warn("Failed to query ontology term: {} ", ex.getMessage()); } catch (IOException ex) { this.logger.error("IOException while getting ontology term ", ex); } return null; }
From source file:org.phenotips.vocabulary.internal.solr.FrenchHPOTranslationTest.java
License:Open Source License
@Test public void queriesAreExtendedToIncludeFrenchFieldsWhenLocaleIsFr() { SolrQuery query = new SolrQuery("seizures"); query.set(DisMaxParams.QF, "name"); query.set(DisMaxParams.PF, "name"); this.component.extendQuery(query, this.vocabulary); Assert.assertEquals("name name_fr^60 synonym_fr^45 def_fr^12 ", query.get(DisMaxParams.PF)); Assert.assertEquals("name name_fr^30 synonym_fr^21 def_fr^6 ", query.get(DisMaxParams.QF)); }
From source file:org.phenotips.vocabulary.internal.solr.FrenchHPOTranslationTest.java
License:Open Source License
@Test public void queriesAreExtendedToIncludeFrenchFieldsWhenLocaleIsFrFR() { when(this.localizationContext.getCurrentLocale()).thenReturn(Locale.FRANCE); SolrQuery query = new SolrQuery("seizures"); query.set(DisMaxParams.QF, "name"); query.set(DisMaxParams.PF, "name"); this.component.extendQuery(query, this.vocabulary); Assert.assertEquals("name name_fr^60 synonym_fr^45 def_fr^12 ", query.get(DisMaxParams.PF)); Assert.assertEquals("name name_fr^30 synonym_fr^21 def_fr^6 ", query.get(DisMaxParams.QF)); }
From source file:org.phenotips.vocabulary.internal.solr.FrenchHPOTranslationTest.java
License:Open Source License
@Test public void queriesAreNotModifiedWhenLocaleIsDe() { when(this.localizationContext.getCurrentLocale()).thenReturn(Locale.GERMAN); SolrQuery query = new SolrQuery("seizures"); query.set(DisMaxParams.QF, "name"); query.set(DisMaxParams.PF, "name"); this.component.extendQuery(query, this.vocabulary); Assert.assertEquals("name", query.get(DisMaxParams.PF)); Assert.assertEquals("name", query.get(DisMaxParams.QF)); }
From source file:org.phenotips.vocabulary.internal.solr.FrenchHPOTranslationTest.java
License:Open Source License
@Test public void queriesAreNotModifiedWhenLocaleIsNull() { when(this.localizationContext.getCurrentLocale()).thenReturn(null); SolrQuery query = new SolrQuery("seizures"); query.set(DisMaxParams.QF, "name"); query.set(DisMaxParams.PF, "name"); this.component.extendQuery(query, this.vocabulary); Assert.assertEquals("name", query.get(DisMaxParams.PF)); Assert.assertEquals("name", query.get(DisMaxParams.QF)); }
From source file:org.phenotips.vocabulary.internal.solr.FrenchHPOTranslationTest.java
License:Open Source License
@Test public void queriesAreNotModifiedWhenFieldsAreInitiallyEmpty() { when(this.localizationContext.getCurrentLocale()).thenReturn(Locale.FRENCH); SolrQuery query = new SolrQuery("seizures"); query.set(DisMaxParams.BQ, "hello"); this.component.extendQuery(query, this.vocabulary); Assert.assertNull(query.get(DisMaxParams.PF)); Assert.assertNull(query.get(DisMaxParams.QF)); }
From source file:org.phenotips.vocabulary.internal.solr.GermanHPOTranslationTest.java
License:Open Source License
@Test public void queriesAreExtendedToIncludeGermanFieldsWhenLocaleIsDe() { SolrQuery query = new SolrQuery("seizures"); query.set(DisMaxParams.QF, "name"); query.set(DisMaxParams.PF, "name"); this.component.extendQuery(query, this.vocabulary); Assert.assertEquals("name name_de^60 synonym_de^45 def_de^12 ", query.get(DisMaxParams.PF)); Assert.assertEquals("name name_de^30 synonym_de^21 def_de^6 ", query.get(DisMaxParams.QF)); }
From source file:org.phenotips.vocabulary.internal.solr.GermanHPOTranslationTest.java
License:Open Source License
@Test public void queriesAreExtendedToIncludeGermanFieldsWhenLocaleIsDeDE() { when(this.localizationContext.getCurrentLocale()).thenReturn(Locale.GERMANY); SolrQuery query = new SolrQuery("seizures"); query.set(DisMaxParams.QF, "name"); query.set(DisMaxParams.PF, "name"); this.component.extendQuery(query, this.vocabulary); Assert.assertEquals("name name_de^60 synonym_de^45 def_de^12 ", query.get(DisMaxParams.PF)); Assert.assertEquals("name name_de^30 synonym_de^21 def_de^6 ", query.get(DisMaxParams.QF)); }