Added generic support for lucene scalar functions and used the to implement CMIS Upper/Lower string functions (MOB-221)

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@14578 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Andrew Hind
2009-06-08 10:42:01 +00:00
parent 2c122bca4e
commit 1a261b54fd
33 changed files with 1385 additions and 242 deletions

View File

@@ -28,53 +28,184 @@ import java.io.Serializable;
import java.util.Collection;
import java.util.Map;
import org.alfresco.repo.search.impl.lucene.LuceneFunction;
import org.alfresco.repo.search.impl.lucene.LuceneQueryParser;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.namespace.QName;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.search.Query;
/**
* The function evaluation context for lucene query implementations.
*
* This context is used at query time and also when navigating the results to get column values.
*
* @author andyh
*/
public interface FunctionEvaluationContext
{
/**
* @return the matching nodes by selector (at navigation time)
*/
public Map<String, NodeRef> getNodeRefs();
/**
* @return the scores by selector (at navigation time)
*/
public Map<String, Float> getScores();
/**
* Get a property
* @param nodeRef
* @param propertyName
* @return the property (at navigation time)
*/
public Serializable getProperty(NodeRef nodeRef, String propertyName);
/**
* @return the node service
*/
public NodeService getNodeService();
/**
* @return the score (at navigation time)
*/
public Float getScore();
public Query buildLuceneEquality(LuceneQueryParser lqp, String propertyName, Serializable value, PredicateMode mode) throws ParseException;
/**
* @param lqp
* @param propertyName
* @param value
* @param mode
* @param luceneFunction
* @return the query
* @throws ParseException
*/
public Query buildLuceneEquality(LuceneQueryParser lqp, String propertyName, Serializable value, PredicateMode mode, LuceneFunction luceneFunction) throws ParseException;
/**
* Note: null and not null are not required to support functions from the spec
* @param lqp
* @param propertyName
* @param not
* @return the query
* @throws ParseException
*/
public Query buildLuceneExists(LuceneQueryParser lqp, String propertyName, Boolean not) throws ParseException;
public Query buildLuceneGreaterThan(LuceneQueryParser lqp, String propertyName, Serializable value, PredicateMode mode) throws ParseException;
/**
* @param lqp
* @param propertyName
* @param value
* @param mode
* @param luceneFunction
* @return the query
* @throws ParseException
*/
public Query buildLuceneGreaterThan(LuceneQueryParser lqp, String propertyName, Serializable value, PredicateMode mode, LuceneFunction luceneFunction) throws ParseException;
public Query buildLuceneGreaterThanOrEquals(LuceneQueryParser lqp, String propertyName, Serializable value, PredicateMode mode) throws ParseException;
/**
* @param lqp
* @param propertyName
* @param value
* @param mode
* @param luceneFunction
* @return the query
* @throws ParseException
*/
public Query buildLuceneGreaterThanOrEquals(LuceneQueryParser lqp, String propertyName, Serializable value, PredicateMode mode, LuceneFunction luceneFunction) throws ParseException;
public Query buildLuceneLessThan(LuceneQueryParser lqp, String propertyName, Serializable value, PredicateMode mode) throws ParseException;
/**
* @param lqp
* @param propertyName
* @param value
* @param mode
* @param luceneFunction
* @return the query
* @throws ParseException
*/
public Query buildLuceneLessThan(LuceneQueryParser lqp, String propertyName, Serializable value, PredicateMode mode, LuceneFunction luceneFunction) throws ParseException;
public Query buildLuceneLessThanOrEquals(LuceneQueryParser lqp, String propertyName, Serializable value, PredicateMode mode) throws ParseException;
/**
* @param lqp
* @param propertyName
* @param value
* @param mode
* @param luceneFunction
* @return the query
* @throws ParseException
*/
public Query buildLuceneLessThanOrEquals(LuceneQueryParser lqp, String propertyName, Serializable value, PredicateMode mode, LuceneFunction luceneFunction) throws ParseException;
/**
* Note: Like is not required to support functions from the spec
* @param lqp
* @param propertyName
* @param value
* @param not
* @return the query
* @throws ParseException
*/
public Query buildLuceneLike(LuceneQueryParser lqp, String propertyName, Serializable value, Boolean not) throws ParseException;
public Query buildLuceneInequality(LuceneQueryParser lqp, String propertyName, Serializable value, PredicateMode mode) throws ParseException;
/**
* @param lqp
* @param propertyName
* @param value
* @param mode
* @param luceneFunction
* @return the query
* @throws ParseException
*/
public Query buildLuceneInequality(LuceneQueryParser lqp, String propertyName, Serializable value, PredicateMode mode, LuceneFunction luceneFunction) throws ParseException;
/**
* Note: In is not required to support functions from the spec
* @param lqp
* @param propertyName
* @param values
* @param not
* @param mode
* @return the query
* @throws ParseException
*/
public Query buildLuceneIn(LuceneQueryParser lqp, String propertyName, Collection<Serializable> values, Boolean not, PredicateMode mode) throws ParseException;
/**
* @param propertyName
* @return the field used for sorting the given property
*/
public String getLuceneSortField(String propertyName);
/**
* @param propertyName
* @return - is this an object id
*/
public boolean isObjectId(String propertyName);
/**
* @param propertyName
* @return is this property queryable
*/
public boolean isQueryable(String propertyName);
/**
* @param propertyName
* @return Is this property orderable
*/
public boolean isOrderable(String propertyName);
/**
* @param propertyName
* @return the lucene field name for the property
*/
public String getLuceneFieldName(String propertyName);
/**
* @param functionArgument
* @return the lucene function appropriate to a function argument
*/
public LuceneFunction getLuceneFunction(FunctionArgument functionArgument);
}

View File

@@ -29,10 +29,13 @@ import java.util.Map;
import org.alfresco.repo.search.impl.querymodel.Argument;
import org.alfresco.repo.search.impl.querymodel.ArgumentDefinition;
import org.alfresco.repo.search.impl.querymodel.FunctionArgument;
import org.alfresco.repo.search.impl.querymodel.Multiplicity;
import org.alfresco.repo.search.impl.querymodel.PropertyArgument;
import org.alfresco.repo.search.impl.querymodel.QueryModelException;
import org.alfresco.repo.search.impl.querymodel.StaticArgument;
import org.alfresco.repo.search.impl.querymodel.impl.functions.Lower;
import org.alfresco.repo.search.impl.querymodel.impl.functions.Upper;
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
import org.alfresco.service.namespace.QName;
@@ -41,21 +44,32 @@ import org.alfresco.service.namespace.QName;
*/
public abstract class BaseComparison extends BaseFunction
{
/**
* Left hand side
*/
public final static String ARG_LHS = "LHS";
/**
* Right hand side
*/
public final static String ARG_RHS = "RHS";
public static LinkedHashMap<String, ArgumentDefinition> args;
/**
* Args
*/
public static LinkedHashMap<String, ArgumentDefinition> ARGS;
private PropertyArgument propertyArgument;
private StaticArgument staticArgument;
private FunctionArgument functionArgument;
static
{
args = new LinkedHashMap<String, ArgumentDefinition>();
args.put(ARG_LHS, new BaseArgumentDefinition(Multiplicity.ANY, ARG_LHS, DataTypeDefinition.ANY, true));
args.put(ARG_RHS, new BaseArgumentDefinition(Multiplicity.ANY, ARG_RHS, DataTypeDefinition.ANY, true));
ARGS = new LinkedHashMap<String, ArgumentDefinition>();
ARGS.put(ARG_LHS, new BaseArgumentDefinition(Multiplicity.ANY, ARG_LHS, DataTypeDefinition.ANY, true));
ARGS.put(ARG_RHS, new BaseArgumentDefinition(Multiplicity.ANY, ARG_RHS, DataTypeDefinition.ANY, true));
}
/**
@@ -68,14 +82,14 @@ public abstract class BaseComparison extends BaseFunction
super(name, returnType, argumentDefinitions);
}
public void setPropertyAndStaticArguments(Map<String, Argument> functionArgs)
protected void setPropertyAndStaticArguments(Map<String, Argument> functionArgs)
{
Argument lhs = functionArgs.get(ARG_LHS);
Argument rhs = functionArgs.get(ARG_RHS);
if (lhs instanceof PropertyArgument)
{
if (rhs instanceof PropertyArgument)
if ((rhs instanceof PropertyArgument) || (rhs instanceof FunctionArgument))
{
throw new QueryModelException("Implicit join is not supported");
}
@@ -89,9 +103,29 @@ public abstract class BaseComparison extends BaseFunction
throw new QueryModelException("Argument of type " + rhs.getClass().getName() + " is not supported");
}
}
else if (lhs instanceof FunctionArgument)
{
if ((rhs instanceof PropertyArgument) || (rhs instanceof FunctionArgument))
{
throw new QueryModelException("Implicit join is not supported");
}
else if (rhs instanceof StaticArgument)
{
functionArgument = (FunctionArgument) lhs;
staticArgument = (StaticArgument) rhs;
}
else
{
throw new QueryModelException("Argument of type " + rhs.getClass().getName() + " is not supported");
}
}
else if (rhs instanceof PropertyArgument)
{
if (lhs instanceof StaticArgument)
if ((lhs instanceof PropertyArgument) || (lhs instanceof FunctionArgument))
{
throw new QueryModelException("Implicit join is not supported");
}
else if (lhs instanceof StaticArgument)
{
propertyArgument = (PropertyArgument) rhs;
staticArgument = (StaticArgument) lhs;
@@ -101,6 +135,22 @@ public abstract class BaseComparison extends BaseFunction
throw new QueryModelException("Argument of type " + lhs.getClass().getName() + " is not supported");
}
}
else if (rhs instanceof FunctionArgument)
{
if ((lhs instanceof PropertyArgument) || (lhs instanceof FunctionArgument))
{
throw new QueryModelException("Implicit join is not supported");
}
else if (lhs instanceof StaticArgument)
{
functionArgument = (FunctionArgument) rhs;
staticArgument = (StaticArgument) lhs;
}
else
{
throw new QueryModelException("Argument of type " + lhs.getClass().getName() + " is not supported");
}
}
else
{
throw new QueryModelException("Equals must have one property argument");
@@ -108,7 +158,7 @@ public abstract class BaseComparison extends BaseFunction
}
/**
* @return the propertyArgument
* @return the propertyArgument - there must be a property argument of a function argument
*/
protected PropertyArgument getPropertyArgument()
{
@@ -116,13 +166,63 @@ public abstract class BaseComparison extends BaseFunction
}
/**
* @return the staticArgument
* @return the staticArgument - must be set
*/
protected StaticArgument getStaticArgument()
{
return staticArgument;
}
/**
* @return the functionArgument
*/
protected FunctionArgument getFunctionArgument()
{
return functionArgument;
}
protected String getPropertyName()
{
if (propertyArgument != null)
{
return propertyArgument.getPropertyName();
}
else if (functionArgument != null)
{
String functionName = functionArgument.getFunction().getName();
if (functionName.equals(Upper.NAME))
{
Argument arg = functionArgument.getFunctionArguments().get(Upper.ARG_ARG);
if (arg instanceof PropertyArgument)
{
return ((PropertyArgument) arg).getPropertyName();
}
else
{
throw new QueryModelException("Upper must have a column argument " + arg);
}
}
else if (functionName.equals(Lower.NAME))
{
Argument arg = functionArgument.getFunctionArguments().get(Lower.ARG_ARG);
if (arg instanceof PropertyArgument)
{
return ((PropertyArgument) arg).getPropertyName();
}
else
{
throw new QueryModelException("Lower must have a column argument " + arg);
}
}
else
{
throw new QueryModelException("Unsupported function: " + functionName);
}
}
else
{
throw new QueryModelException("A property of function argument must be provided");
}
}
}

View File

@@ -47,7 +47,7 @@ public class Equals extends BaseComparison
*/
public Equals()
{
super(NAME, DataTypeDefinition.BOOLEAN, args);
super(NAME, DataTypeDefinition.BOOLEAN, ARGS);
}
/* (non-Javadoc)

View File

@@ -47,7 +47,7 @@ public class GreaterThan extends BaseComparison
*/
public GreaterThan()
{
super(NAME, DataTypeDefinition.BOOLEAN, args);
super(NAME, DataTypeDefinition.BOOLEAN, ARGS);
}
/* (non-Javadoc)

View File

@@ -47,7 +47,7 @@ public class GreaterThanOrEquals extends BaseComparison
*/
public GreaterThanOrEquals()
{
super(NAME, DataTypeDefinition.BOOLEAN, args);
super(NAME, DataTypeDefinition.BOOLEAN, ARGS);
}
/* (non-Javadoc)

View File

@@ -46,7 +46,7 @@ public class LessThan extends BaseComparison
*/
public LessThan()
{
super(NAME, DataTypeDefinition.BOOLEAN, args);
super(NAME, DataTypeDefinition.BOOLEAN, ARGS);
}
/*

View File

@@ -46,7 +46,7 @@ public class LessThanOrEquals extends BaseComparison
*/
public LessThanOrEquals()
{
super(NAME, DataTypeDefinition.BOOLEAN, args);
super(NAME, DataTypeDefinition.BOOLEAN, ARGS);
}
/*

View File

@@ -46,7 +46,7 @@ public class NotEquals extends BaseComparison
*/
public NotEquals()
{
super(NAME, DataTypeDefinition.BOOLEAN, args);
super(NAME, DataTypeDefinition.BOOLEAN, ARGS);
}
/*

View File

@@ -65,7 +65,7 @@ public class LuceneEquals extends Equals implements LuceneQueryBuilderComponent
LuceneQueryParser lqp = luceneContext.getLuceneQueryParser();
setPropertyAndStaticArguments(functionArgs);
Query query = functionContext.buildLuceneEquality(lqp, getPropertyArgument().getPropertyName(), getStaticArgument().getValue(functionContext), PredicateMode.ANY);
Query query = functionContext.buildLuceneEquality(lqp, getPropertyName(), getStaticArgument().getValue(functionContext), PredicateMode.ANY, functionContext.getLuceneFunction(getFunctionArgument()));
if(query == null)
{

View File

@@ -28,6 +28,7 @@ import java.util.Map;
import java.util.Set;
import org.alfresco.repo.search.impl.lucene.AnalysisMode;
import org.alfresco.repo.search.impl.lucene.LuceneFunction;
import org.alfresco.repo.search.impl.lucene.LuceneQueryParser;
import org.alfresco.repo.search.impl.querymodel.Argument;
import org.alfresco.repo.search.impl.querymodel.FunctionEvaluationContext;
@@ -78,11 +79,11 @@ public class LuceneFTSPhrase extends FTSPhrase implements LuceneQueryBuilderComp
if (propArg != null)
{
String prop = propArg.getPropertyName();
query = lqp.getFieldQuery(functionContext.getLuceneFieldName(prop), term, AnalysisMode.TOKENISE, slop);
query = lqp.getFieldQuery(functionContext.getLuceneFieldName(prop), term, AnalysisMode.TOKENISE, slop, LuceneFunction.FIELD);
}
else
{
query = lqp.getFieldQuery("TEXT", term, AnalysisMode.TOKENISE, slop);
query = lqp.getFieldQuery("TEXT", term, AnalysisMode.TOKENISE, slop, LuceneFunction.FIELD);
}
return query;

View File

@@ -28,6 +28,7 @@ import java.util.Map;
import java.util.Set;
import org.alfresco.repo.search.impl.lucene.AnalysisMode;
import org.alfresco.repo.search.impl.lucene.LuceneFunction;
import org.alfresco.repo.search.impl.lucene.LuceneQueryParser;
import org.alfresco.repo.search.impl.querymodel.Argument;
import org.alfresco.repo.search.impl.querymodel.FunctionEvaluationContext;
@@ -78,11 +79,11 @@ public class LuceneFTSRange extends FTSRange implements LuceneQueryBuilderCompon
if (propArg != null)
{
String prop = propArg.getPropertyName();
query = lqp.getRangeQuery(functionContext.getLuceneFieldName(prop), from, to, fromInc, toInc, AnalysisMode.DEFAULT);
query = lqp.getRangeQuery(functionContext.getLuceneFieldName(prop), from, to, fromInc, toInc, AnalysisMode.DEFAULT, LuceneFunction.FIELD);
}
else
{
query = lqp.getRangeQuery("TEXT", from, to, fromInc, toInc, AnalysisMode.DEFAULT);
query = lqp.getRangeQuery("TEXT", from, to, fromInc, toInc, AnalysisMode.DEFAULT, LuceneFunction.FIELD);
}
return query;
}

View File

@@ -27,6 +27,7 @@ package org.alfresco.repo.search.impl.querymodel.impl.lucene.functions;
import java.util.Map;
import java.util.Set;
import org.alfresco.repo.search.impl.lucene.LuceneFunction;
import org.alfresco.repo.search.impl.lucene.LuceneQueryParser;
import org.alfresco.repo.search.impl.lucene.AnalysisMode;
import org.alfresco.repo.search.impl.querymodel.Argument;
@@ -72,11 +73,11 @@ public class LuceneFTSTerm extends FTSTerm implements LuceneQueryBuilderComponen
if (propArg != null)
{
String prop = propArg.getPropertyName();
query = lqp.getFieldQuery(functionContext.getLuceneFieldName(prop), term, mode);
query = lqp.getFieldQuery(functionContext.getLuceneFieldName(prop), term, mode, LuceneFunction.FIELD);
}
else
{
query = lqp.getFieldQuery("TEXT", term, mode);
query = lqp.getFieldQuery("TEXT", term, mode, LuceneFunction.FIELD);
}
return query;

View File

@@ -65,7 +65,7 @@ public class LuceneGreaterThan extends GreaterThan implements LuceneQueryBuilder
LuceneQueryParser lqp = luceneContext.getLuceneQueryParser();
setPropertyAndStaticArguments(functionArgs);
Query query = functionContext.buildLuceneGreaterThan(lqp, getPropertyArgument().getPropertyName(), getStaticArgument().getValue(functionContext), PredicateMode.ANY);
Query query = functionContext.buildLuceneGreaterThan(lqp, getPropertyName(), getStaticArgument().getValue(functionContext), PredicateMode.ANY, functionContext.getLuceneFunction(getFunctionArgument()));
if(query == null)
{

View File

@@ -66,7 +66,7 @@ public class LuceneGreaterThanOrEquals extends GreaterThanOrEquals implements Lu
LuceneQueryParser lqp = luceneContext.getLuceneQueryParser();
setPropertyAndStaticArguments(functionArgs);
Query query = functionContext.buildLuceneGreaterThanOrEquals(lqp, getPropertyArgument().getPropertyName(), getStaticArgument().getValue(functionContext), PredicateMode.ANY);
Query query = functionContext.buildLuceneGreaterThanOrEquals(lqp, getPropertyName(), getStaticArgument().getValue(functionContext), PredicateMode.ANY, functionContext.getLuceneFunction(getFunctionArgument()));
if(query == null)
{

View File

@@ -66,7 +66,7 @@ public class LuceneLessThan extends LessThan implements LuceneQueryBuilderCompon
LuceneQueryParser lqp = luceneContext.getLuceneQueryParser();
setPropertyAndStaticArguments(functionArgs);
Query query = functionContext.buildLuceneLessThan(lqp, getPropertyArgument().getPropertyName(), getStaticArgument().getValue(functionContext), PredicateMode.ANY);
Query query = functionContext.buildLuceneLessThan(lqp, getPropertyName(), getStaticArgument().getValue(functionContext), PredicateMode.ANY, functionContext.getLuceneFunction(getFunctionArgument()));
if(query == null)
{

View File

@@ -66,7 +66,7 @@ public class LuceneLessThanOrEquals extends LessThanOrEquals implements LuceneQu
LuceneQueryParser lqp = luceneContext.getLuceneQueryParser();
setPropertyAndStaticArguments(functionArgs);
Query query = functionContext.buildLuceneLessThanOrEquals(lqp, getPropertyArgument().getPropertyName(), getStaticArgument().getValue(functionContext), PredicateMode.ANY);
Query query = functionContext.buildLuceneLessThanOrEquals(lqp, getPropertyName(), getStaticArgument().getValue(functionContext), PredicateMode.ANY, functionContext.getLuceneFunction(getFunctionArgument()));
if(query == null)
{
throw new QueryModelException("No query time mapping for property "+getPropertyArgument().getPropertyName()+", it should not be allowed in predicates");

View File

@@ -40,7 +40,6 @@ import org.apache.lucene.search.Query;
/**
* @author andyh
*
*/
public class LuceneNotEquals extends NotEquals implements LuceneQueryBuilderComponent
{
@@ -65,16 +64,16 @@ public class LuceneNotEquals extends NotEquals implements LuceneQueryBuilderComp
{
LuceneQueryParser lqp = luceneContext.getLuceneQueryParser();
setPropertyAndStaticArguments(functionArgs);
Query query = functionContext.buildLuceneInequality(lqp, getPropertyArgument().getPropertyName(), getStaticArgument().getValue(functionContext), PredicateMode.ANY);
if(query == null)
Query query = functionContext.buildLuceneInequality(lqp, getPropertyName(), getStaticArgument().getValue(functionContext), PredicateMode.ANY, functionContext
.getLuceneFunction(getFunctionArgument()));
if (query == null)
{
throw new QueryModelException("No query time mapping for property "+getPropertyArgument().getPropertyName()+", it should not be allowed in predicates");
throw new QueryModelException("No query time mapping for property " + getPropertyArgument().getPropertyName() + ", it should not be allowed in predicates");
}
return query;
}
}