|
|
|
|
|
public interface QueryProcessor
|
{
|
void preprocess(QParser
qPlugin);
|
Query process(QParser
qPlugin, Query prevQuery) throws ParseException;
|
}
|
public class MinimumMatchFieldQueryProcessor implements QueryProcessor
{
|
private
Map
|
private String mmOP;
|
private String lang = null;
|
|
@Override
|
public void preprocess(QParser
qPlugin) {
|
String fieldsToMatch =
qPlugin.getParams().get("minmatch.mm");
|
mmOP =
qPlugin.getParams().get("minmatch.op", "AND");
|
minMatchFieldsMap =
new HashMap
|
String[] fields = fieldsToMatch.split("\\|\\|");
|
for (String field :
fields) {
|
int indx =
field.indexOf("=");
|
if (indx != -1)
|
{
|
minMatchFieldsMap.put(field.substring(0, indx).replaceAll("_mm",
"”),
|
field.substring(indx
+ 1));
|
}
|
}
|
}
|
|
@Override
|
public Query process(QParser
qPlugin, Query prevQuery) throws ParseException {
|
|
String queryString =
CommonUtils.extractPureQuery(qPlugin.getString());
|
|
if (StringUtils.isBlank(queryString))
return prevQuery;
|
|
BooleanQuery bq = new
BooleanQuery(true);
|
for (Map.Entry
|
{
|
String subQueryString =
String.format("_query_:\"{!edismax qf=%s mm=%s}%s\"", entry.getKey(),
entry.getValue(), queryString);
|
|
Query minMatchQuery =
qPlugin.subQuery(subQueryString, "lucene")
|
.getQuery();
|
|
if ("and".equalsIgnoreCase(mmOP))
|
{
|
bq.add(minMatchQuery,
Occur.MUST);
|
}
|
else
|
{
|
bq.add(minMatchQuery,
Occur.SHOULD);
|
}
|
}
|
return bq;
|
}
|
}
|