RemoveOneTokenFromPhrase extends QueryRelaxerHeuristic { @Override public Set<RelaxerSuggestion> createSuggestions(SolrQueryRequest req) { List<String> tokens = new ArrayList<String>(); int countOfQuotes = 0; SolrParams params = req.getParams(); String userQuery = params.get(CommonParams.Q); countOfQuotes = ReSearcherUtils.tokenizeQueryString(userQuery, tokens); if ((tokens.size() - countOfQuotes) <= 1) { return null; } else { Set<RelaxerSuggestion> suggestions = new LinkedHashSet<RelaxerSuggestion>(); for (int i = 0; i < tokens.size(); i++) { StringBuilder sug = new StringBuilder(); if (tokens.get(i).equals("\"")) { continue; } int startingQuoteOpened = 0; boolean startingQuoteOmitted = false; for (int j = 0; j < tokens.size(); j++) { if (tokens.get(j).equals("\"")) { startingQuoteOpened++; if ((startingQuoteOpened % 2) == 1) { if ((tokens.size() > (j + 3)) && (tokens.get(j + 3).equals("\"")) && ((j + 1 == i) || (j + 2 == i))) { startingQuoteOmitted = true; } else if ((tokens.size() > (j + 2)) && (tokens.get(j + 2).equals("\"")) && ((j + 1 == i))) { startingQuoteOmitted = true; } else { sug.append("\""); } } else { if (startingQuoteOmitted == false) { sug.setCharAt(sug.length() - 1, '\"'); sug.append(" "); } startingQuoteOmitted = false; } } else if (j != i) { sug.append(tokens.get(j).trim()); sug.append(" "); } } String relaxedUserQuery = sug.toString().trim(); suggestions.add(new QueryRelaxerSuggestion(relaxedUserQuery, userQuery, relaxedUserQuery)); } return suggestions; } }  @Override Set<RelaxerSuggestion> createSuggestions(SolrQueryRequest req);  }
@Test public void testCreateSuggestions1() { Set<RelaxerSuggestion> sugs = heuristic.createSuggestions(req(CommonParams.Q, "some \"quoted query\" for test")); verify(sugs, "\"quoted query\" for test", "some query for test", "some quoted for test", "some \"quoted query\" test", "some \"quoted query\" for"); }
@Test public void testCreateSuggestions2() { Set<RelaxerSuggestion> sugs = heuristic.createSuggestions(req(CommonParams.Q, "some \" quoted query \" for test")); verify(sugs, "\"quoted query\" for test", "some query for test", "some quoted for test", "some \"quoted query\" test", "some \"quoted query\" for"); }
@Test public void testCreateSuggestions3() { Set<RelaxerSuggestion> sugs = heuristic.createSuggestions(req(CommonParams.Q, "\"some quoted query\" for test")); verify(sugs, "\"quoted query\" for test", "\"some query\" for test", "\"some quoted\" for test", "\"some quoted query\" test", "\"some quoted query\" for"); }
@Test public void testCreateSuggestions4() { Set<RelaxerSuggestion> sugs = heuristic.createSuggestions(req(CommonParams.Q, "some quoted query \"for\" test")); verify(sugs, "quoted query \"for\" test", "some query \"for\" test", "some quoted \"for\" test", "some quoted query test", "some quoted query \"for\""); }
@Test public void testCreateSuggestions5() { Set<RelaxerSuggestion> sugs = heuristic.createSuggestions(req(CommonParams.Q, "some quoted query \" for test\"")); verify(sugs, "quoted query \"for test\"", "some query \"for test\"", "some quoted \"for test\"", "some quoted query test", "some quoted query for"); }
@Test public void testCreateSuggestions6() { Set<RelaxerSuggestion> sugs = heuristic.createSuggestions(req(CommonParams.Q, "some quoted query for \"test\"")); verify(sugs, "quoted query for \"test\"", "some query for \"test\"", "some quoted for \"test\"", "some quoted query \"test\"", "some quoted query for"); }
@Test public void testCreateSuggestions7() { Set<RelaxerSuggestion> sugs = heuristic.createSuggestions(req(CommonParams.Q, "\"some\" quoted query for test")); verify(sugs, "quoted query for test", "\"some\" query for test", "\"some\" quoted for test", "\"some\" quoted query test", "\"some\" quoted query for"); }
@Test public void testCreateSuggestions8() { Set<RelaxerSuggestion> sugs = heuristic.createSuggestions(req(CommonParams.Q, "\"some\"")); assertNull(sugs); }
@Test public void testCreateSuggestions9() { Set<RelaxerSuggestion> sugs = heuristic.createSuggestions(req(CommonParams.Q, "\"some\" test \"")); verify(sugs, "test \"", "\"some\" \""); }
@Test public void testCreateSuggestions10() { Set<RelaxerSuggestion> sugs = heuristic.createSuggestions(req(CommonParams.Q, "\"rate and feedback\"")); verify(sugs, "\"and feedback\"", "\"rate feedback\"", "\"rate and\""); }