@Test public void testApplySpellcheckSuggestionsWithNull() { SolrQuery output = SolrQueryUtils.applySpellcheckSuggestion(null, null); Assert.assertNull(output); SolrQuery input = new SolrQuery("original"); output = SolrQueryUtils.applySpellcheckSuggestion(input, null); Assert.assertNotNull(output); Assert.assertEquals("original", output.get(CommonParams.Q)); output = SolrQueryUtils.applySpellcheckSuggestion(null, "fixed"); Assert.assertNull(output); }
public static SolrQuery applySpellcheckSuggestion(SolrQuery originalQuery, String suggestedQuery) { if (originalQuery == null) { return null; } if (StringUtils.isBlank(suggestedQuery)) { return originalQuery; } String newQueryString = suggestedQuery; if (StringUtils.isNotEmpty(originalQuery.get(SpellingParams.SPELLCHECK_Q))) { newQueryString = "(" + originalQuery.getQuery() + ")^10 " + suggestedQuery; } Matcher originalStub = WORD_STUB.matcher(originalQuery.getQuery()); Matcher newStub = WORD_STUB.matcher(suggestedQuery); if (originalStub.find() && newStub.find() && !StringUtils.equals(originalStub.group(2), newStub.group(2))) { newQueryString += ' ' + originalStub.group() + "^1.5"; String boostQuery = originalQuery.get(DisMaxParams.BQ); if (StringUtils.isNotEmpty(boostQuery)) { originalQuery.add(DisMaxParams.BQ, boostQuery.replace(originalStub.group(2), newStub.group(2))); } } originalQuery.setQuery(newQueryString); return originalQuery; }
SolrQueryUtils { public static SolrQuery applySpellcheckSuggestion(SolrQuery originalQuery, String suggestedQuery) { if (originalQuery == null) { return null; } if (StringUtils.isBlank(suggestedQuery)) { return originalQuery; } String newQueryString = suggestedQuery; if (StringUtils.isNotEmpty(originalQuery.get(SpellingParams.SPELLCHECK_Q))) { newQueryString = "(" + originalQuery.getQuery() + ")^10 " + suggestedQuery; } Matcher originalStub = WORD_STUB.matcher(originalQuery.getQuery()); Matcher newStub = WORD_STUB.matcher(suggestedQuery); if (originalStub.find() && newStub.find() && !StringUtils.equals(originalStub.group(2), newStub.group(2))) { newQueryString += ' ' + originalStub.group() + "^1.5"; String boostQuery = originalQuery.get(DisMaxParams.BQ); if (StringUtils.isNotEmpty(boostQuery)) { originalQuery.add(DisMaxParams.BQ, boostQuery.replace(originalStub.group(2), newStub.group(2))); } } originalQuery.setQuery(newQueryString); return originalQuery; } }
SolrQueryUtils { public static SolrQuery applySpellcheckSuggestion(SolrQuery originalQuery, String suggestedQuery) { if (originalQuery == null) { return null; } if (StringUtils.isBlank(suggestedQuery)) { return originalQuery; } String newQueryString = suggestedQuery; if (StringUtils.isNotEmpty(originalQuery.get(SpellingParams.SPELLCHECK_Q))) { newQueryString = "(" + originalQuery.getQuery() + ")^10 " + suggestedQuery; } Matcher originalStub = WORD_STUB.matcher(originalQuery.getQuery()); Matcher newStub = WORD_STUB.matcher(suggestedQuery); if (originalStub.find() && newStub.find() && !StringUtils.equals(originalStub.group(2), newStub.group(2))) { newQueryString += ' ' + originalStub.group() + "^1.5"; String boostQuery = originalQuery.get(DisMaxParams.BQ); if (StringUtils.isNotEmpty(boostQuery)) { originalQuery.add(DisMaxParams.BQ, boostQuery.replace(originalStub.group(2), newStub.group(2))); } } originalQuery.setQuery(newQueryString); return originalQuery; } private  SolrQueryUtils(); }
SolrQueryUtils { public static SolrQuery applySpellcheckSuggestion(SolrQuery originalQuery, String suggestedQuery) { if (originalQuery == null) { return null; } if (StringUtils.isBlank(suggestedQuery)) { return originalQuery; } String newQueryString = suggestedQuery; if (StringUtils.isNotEmpty(originalQuery.get(SpellingParams.SPELLCHECK_Q))) { newQueryString = "(" + originalQuery.getQuery() + ")^10 " + suggestedQuery; } Matcher originalStub = WORD_STUB.matcher(originalQuery.getQuery()); Matcher newStub = WORD_STUB.matcher(suggestedQuery); if (originalStub.find() && newStub.find() && !StringUtils.equals(originalStub.group(2), newStub.group(2))) { newQueryString += ' ' + originalStub.group() + "^1.5"; String boostQuery = originalQuery.get(DisMaxParams.BQ); if (StringUtils.isNotEmpty(boostQuery)) { originalQuery.add(DisMaxParams.BQ, boostQuery.replace(originalStub.group(2), newStub.group(2))); } } originalQuery.setQuery(newQueryString); return originalQuery; } private  SolrQueryUtils(); static SolrQuery generateQuery(SolrQuery originalParams, Map<String, String> queryOptions); static SolrQuery applySpellcheckSuggestion(SolrQuery originalQuery, String suggestedQuery); }
SolrQueryUtils { public static SolrQuery applySpellcheckSuggestion(SolrQuery originalQuery, String suggestedQuery) { if (originalQuery == null) { return null; } if (StringUtils.isBlank(suggestedQuery)) { return originalQuery; } String newQueryString = suggestedQuery; if (StringUtils.isNotEmpty(originalQuery.get(SpellingParams.SPELLCHECK_Q))) { newQueryString = "(" + originalQuery.getQuery() + ")^10 " + suggestedQuery; } Matcher originalStub = WORD_STUB.matcher(originalQuery.getQuery()); Matcher newStub = WORD_STUB.matcher(suggestedQuery); if (originalStub.find() && newStub.find() && !StringUtils.equals(originalStub.group(2), newStub.group(2))) { newQueryString += ' ' + originalStub.group() + "^1.5"; String boostQuery = originalQuery.get(DisMaxParams.BQ); if (StringUtils.isNotEmpty(boostQuery)) { originalQuery.add(DisMaxParams.BQ, boostQuery.replace(originalStub.group(2), newStub.group(2))); } } originalQuery.setQuery(newQueryString); return originalQuery; } private  SolrQueryUtils(); static SolrQuery generateQuery(SolrQuery originalParams, Map<String, String> queryOptions); static SolrQuery applySpellcheckSuggestion(SolrQuery originalQuery, String suggestedQuery);  }