Backend support for Inverting Text search facets
This commit is contained in:
parent
ae6e8bed86
commit
135052d033
@ -60,6 +60,7 @@ public class TextSearchFacet implements Facet {
|
||||
protected String _query;
|
||||
protected String _mode;
|
||||
protected boolean _caseSensitive;
|
||||
protected boolean _invert;
|
||||
|
||||
/*
|
||||
* Derived configuration
|
||||
@ -80,6 +81,7 @@ public class TextSearchFacet implements Facet {
|
||||
writer.key("query"); writer.value(_query);
|
||||
writer.key("mode"); writer.value(_mode);
|
||||
writer.key("caseSensitive"); writer.value(_caseSensitive);
|
||||
writer.key("invert"); writer.value(_invert);
|
||||
writer.endObject();
|
||||
}
|
||||
|
||||
@ -110,6 +112,7 @@ public class TextSearchFacet implements Facet {
|
||||
_query = _query.toLowerCase();
|
||||
}
|
||||
}
|
||||
_invert = o.has("invert") && o.getBoolean("invert");
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -123,14 +126,14 @@ public class TextSearchFacet implements Facet {
|
||||
Evaluable eval = new VariableExpr("value");
|
||||
|
||||
if ("regex".equals(_mode)) {
|
||||
return new ExpressionStringComparisonRowFilter(eval, _columnName, _cellIndex) {
|
||||
return new ExpressionStringComparisonRowFilter(eval, _invert, _columnName, _cellIndex) {
|
||||
@Override
|
||||
protected boolean checkValue(String s) {
|
||||
return _pattern.matcher(s).find();
|
||||
};
|
||||
};
|
||||
} else {
|
||||
return new ExpressionStringComparisonRowFilter(eval, _columnName, _cellIndex) {
|
||||
return new ExpressionStringComparisonRowFilter(eval, _invert, _columnName, _cellIndex) {
|
||||
@Override
|
||||
protected boolean checkValue(String s) {
|
||||
return (_caseSensitive ? s : s.toLowerCase()).contains(_query);
|
||||
|
@ -52,11 +52,13 @@ import com.google.refine.model.Row;
|
||||
*/
|
||||
abstract public class ExpressionStringComparisonRowFilter implements RowFilter {
|
||||
final protected Evaluable _evaluable;
|
||||
final protected Boolean _invert;
|
||||
final protected String _columnName;
|
||||
final protected int _cellIndex;
|
||||
|
||||
public ExpressionStringComparisonRowFilter(Evaluable evaluable, String columnName, int cellIndex) {
|
||||
public ExpressionStringComparisonRowFilter(Evaluable evaluable, Boolean invert, String columnName, int cellIndex) {
|
||||
_evaluable = evaluable;
|
||||
_invert = invert;
|
||||
_columnName = columnName;
|
||||
_cellIndex = cellIndex;
|
||||
}
|
||||
@ -67,23 +69,23 @@ abstract public class ExpressionStringComparisonRowFilter implements RowFilter {
|
||||
|
||||
Properties bindings = ExpressionUtils.createBindings(project);
|
||||
ExpressionUtils.bind(bindings, row, rowIndex, _columnName, cell);
|
||||
|
||||
Boolean invert = _invert;
|
||||
Object value = _evaluable.evaluate(bindings);
|
||||
if (value != null) {
|
||||
if (value.getClass().isArray()) {
|
||||
Object[] a = (Object[]) value;
|
||||
for (Object v : a) {
|
||||
if (checkValue(v instanceof String ? ((String) v) : v.toString())) {
|
||||
return true;
|
||||
return (invert) ? false : true;
|
||||
}
|
||||
}
|
||||
} else if (value instanceof Collection<?>) {
|
||||
for (Object v : ExpressionUtils.toObjectCollection(value)) {
|
||||
if (checkValue(v.toString())) {
|
||||
return true;
|
||||
return (invert) ? false : true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return (invert) ? true : false;
|
||||
} else if (value instanceof JSONArray) {
|
||||
JSONArray a = (JSONArray) value;
|
||||
int l = a.length();
|
||||
@ -91,20 +93,20 @@ abstract public class ExpressionStringComparisonRowFilter implements RowFilter {
|
||||
for (int i = 0; i < l; i++) {
|
||||
try {
|
||||
if (checkValue(a.get(i).toString())) {
|
||||
return true;
|
||||
return (invert) ? false : true;
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return (invert) ? true : false;
|
||||
} else {
|
||||
if (checkValue(value instanceof String ? ((String) value) : value.toString())) {
|
||||
return true;
|
||||
return (invert) ? false : true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return (invert) ? true : false;
|
||||
}
|
||||
|
||||
abstract protected boolean checkValue(String s);
|
||||
|
Loading…
Reference in New Issue
Block a user