RemoveOneClauseHeuristic extends QueryRelaxerHeuristic { @Override public Set<RelaxerSuggestion> createSuggestions(SolrQueryRequest req) { SolrParams params = req.getParams(); int longQueryLength = params.getInt(RelaxerParams.QUERY_RELAXER_LONG_QUERY_TERMS, 5); String userQuery = userQueryExtractor.extract(params); String highlightQuery = params.get(HighlightParams.Q); List<Clause> clauses = queryConverter.convert(userQuery, req); int clauseLength = clauseLength(clauses); if (clauseLength <= 1) { return null; } else if (clauseLength >= longQueryLength) { Set<RelaxerSuggestion> suggestions = new LinkedHashSet<RelaxerSuggestion>(); String relaxedMM = req.getParams().get(RelaxerParams.QUERY_RELAXER_LONG_QUERY_MM); if(relaxedMM != null) { MMRelaxerSuggestion suggestion = new MMRelaxerSuggestion(relaxedMM); String relaxedQuery = userQueryExtractor.relaxMM(params, relaxedMM); suggestion.setRelaxedQuery(relaxedQuery); suggestions.add(suggestion); } return suggestions; } else { Set<RelaxerSuggestion> suggestions = new LinkedHashSet<RelaxerSuggestion>(); for (int i = 0; i < clauses.size(); i++) { Clause clauseToRemove = clauses.get(i); boolean multipleTokenClause = clauseToRemove.getTokens() != null && clauseToRemove.getTokens().length > 1; String text = clauseToRemove.getRaw(); if ("NOT".equals(text) || "AND".equals(text) || "OR".equals(text)) { continue; } StringBuilder before = new StringBuilder(); StringBuilder after = new StringBuilder(); for (int j = 0; j < i - 1; j++) { before.append(clauses.get(j).getRaw()); before.append(" "); } if (i >= 1) { String raw = clauses.get(i - 1).getRaw(); if (multipleTokenClause || (!"NOT".equals(raw) && !"AND".equals(raw) && !"OR".equals(raw))) { before.append(raw); before.append(" "); } } if (i < clauses.size() - 1) { String raw = clauses.get(i + 1).getRaw(); if (multipleTokenClause || (!"AND".equals(raw) && !"OR".equals(raw))) { after.append(raw); after.append(" "); } } for (int j = i + 2; j < clauses.size(); j++) { after.append(clauses.get(j).getRaw()); after.append(" "); } if (multipleTokenClause) { for (int k = 0; k < clauseToRemove.getTokens().length; k++ ) { StringBuilder sug = new StringBuilder(before.toString()); if (clauseToRemove.getMust() == '+' || clauseToRemove.getMust() == '-') { sug.append(clauseToRemove.getMust()); } if (clauseToRemove.getField() != null) { sug.append(clauseToRemove.getField()); sug.append(":"); } for (int t = 0; t < clauseToRemove.getTokens().length; t++) { if (t != k) { sug.append(clauseToRemove.getTokens()[t]); } } sug.append(" "); sug.append(after.toString()); String relaxedUserQuery = sug.toString().trim(); String relaxedQuery = userQueryExtractor.relaxQuery(params, userQuery, relaxedUserQuery); QueryRelaxerSuggestion suggestion = new QueryRelaxerSuggestion(relaxedQuery, userQuery, relaxedUserQuery); if (highlightQuery != null) { String relaxedHighlightQuery = userQueryExtractor.relaxHighlightQuery(highlightQuery, params, highlightQuery, relaxedUserQuery); suggestion.setRelaxedHighlightQuery(relaxedHighlightQuery); } suggestions.add(suggestion); } } else { String relaxedUserQuery = before.append(after.toString()).toString().trim(); String relaxedQuery = userQueryExtractor.relaxQuery(params, userQuery, relaxedUserQuery); QueryRelaxerSuggestion suggestion = new QueryRelaxerSuggestion(relaxedQuery, userQuery, relaxedUserQuery); if (highlightQuery != null) { String relaxedHighlightQuery = userQueryExtractor.relaxHighlightQuery(highlightQuery, params, highlightQuery, relaxedUserQuery); suggestion.setRelaxedHighlightQuery(relaxedHighlightQuery); } suggestions.add(suggestion); } } return suggestions; } }  @Override Set<RelaxerSuggestion> createSuggestions(SolrQueryRequest req); @Override void setFieldAnalyzerMaps(Map<Pattern, Analyzer> fieldAnalyzerMaps);  }
@Test public void testCreateSuggestions() { Set<RelaxerSuggestion> sugs = heuristic.createSuggestions( req(CommonParams.Q, "foo AND bar", CommonParams.DF, "foo")); verify(sugs, "bar", "foo"); }
@Test public void testCreateSuggestions2() { Set<RelaxerSuggestion> sugs = heuristic.createSuggestions( req(CommonParams.Q, "foo OR bar", CommonParams.DF, "foo")); verify(sugs, "bar", "foo"); }
@Test public void testCreateSuggestions3() { Set<RelaxerSuggestion> sugs = heuristic.createSuggestions( req(CommonParams.Q, "foo NOT bar", CommonParams.DF, "foo")); verify(sugs, "NOT bar", "foo"); }
@Test public void testCreateSuggestions4() { Set<RelaxerSuggestion> sugs = heuristic.createSuggestions( req(CommonParams.Q, "some \"quoted query\"", CommonParams.DF, "foo")); verify(sugs, "\"quoted query\"", "some"); }
@Test public void testCreateSuggestionsOneTerm() { Set<RelaxerSuggestion> sugs = heuristic.createSuggestions( req(CommonParams.Q, "foo", CommonParams.DF, "foo")); assertNull(sugs); }
@Test public void testCreateSuggestionsOnePhrase() { Set<RelaxerSuggestion> sugs = heuristic.createSuggestions( req(CommonParams.Q, "\"foo bar\"", CommonParams.DF, "foo")); assertNull(sugs); }