// (Not needed any more, but kept here for illustrative purposes)
//private static Pattern _luceneExactPattern = Pattern.compile("([\"+~*?:|&(){}\\[\\]\\^\\!\\-\\\\ ])");
private BaseQueryBuilder parseQueryTerm(AdvancedQueryPojo.QueryTermPojo qt, StringBuffer sQueryTerm) {
BaseQueryBuilder term = null;
BoolQueryBuilder boolTerm = null;
sQueryTerm.setLength(0);
sQueryTerm.append('(');
// 1.1] Free text (Lucene)
boolean nonEmpty = false;
if (null != qt.ftext) { // NOTE term building code below depends on this being 1st clause
nonEmpty = true;
if (qt.ftext.startsWith("$cache")) { // currently not supported
throw new RuntimeException("Don't currently support nested cached queries - coming soon.");
}
sQueryTerm.append('(');
if (null != qt.metadataField) {
sQueryTerm.append(qt.metadataField).append(':');
}
sQueryTerm.append(qt.ftext);
sQueryTerm.append(')');
if (null != qt.metadataField) { // Metadata only
term = QueryBuilders.queryString(qt.ftext).field(qt.metadataField);
}
else {
term = QueryBuilders.queryString(qt.ftext).field("_all").field(DocumentPojo.fullText_);
}
if (null == _scoringParams.adjustAggregateSig) { // auto-decide .. if ftext is set and is non-trivial
if (qt.ftext.contains(" ")) {
_scoringParams.adjustAggregateSig = true;
}
}
}//TESTED (logic0)
// 1.2] Exact text
if (null != qt.etext) { // NOTE term building code below depends on this being 2nd clause
nonEmpty = true;
BaseQueryBuilder termQ = null;
if (sQueryTerm.length() > 1) {
sQueryTerm.append(" AND ");
}
if (qt.etext.equals("*")) { // Special case
termQ = QueryBuilders.matchAllQuery();
}
else { // Normal query
if (null != qt.metadataField) { // Metadata only
termQ = CrossVersionQueryBuilders.matchPhraseQuery(qt.metadataField, qt.etext);
}
else { // Normal query
termQ = QueryBuilders.boolQuery().
should(CrossVersionQueryBuilders.matchPhraseQuery("_all", qt.etext)).
should(CrossVersionQueryBuilders.matchPhraseQuery(DocumentPojo.fullText_, qt.etext));
}
}
sQueryTerm.append('(');
if (null != qt.metadataField) {
sQueryTerm.append(qt.metadataField).append(':');
}
sQueryTerm.append('"');
sQueryTerm.append(qt.etext);
sQueryTerm.append("\")");
if (null == term) {
term = termQ;
}
else {
term = (boolTerm = QueryBuilders.boolQuery().must(term).must(termQ));
}
}//TESTED (logic1)
// Here's where it starts getting interesting:
// 1.3] Entity
if ((null != qt.entity) || (null != qt.entityValue) || ((null == qt.assoc) && (null != qt.sentiment))) { // (if no association specified then sentiment applies to entities)
nonEmpty = true;
if (sQueryTerm.length() > 1) {
sQueryTerm.append(" AND ");
}
sQueryTerm.append('(');
BaseQueryBuilder termQ = QueryBuilders.nestedQuery(DocumentPojo.entities_, this.parseEntityTerm(qt, sQueryTerm, EntityPojo.docQuery_index_)).scoreMode("max").boost((float)1.0);
if (null == term) {
term = termQ;
}
else if (null == boolTerm) {
term = (boolTerm = QueryBuilders.boolQuery().must(term).must(termQ));
}
else {
term = (boolTerm = boolTerm.must(termQ));
}
sQueryTerm.append(')');
}//TESTED: logic2* TOTEST: alias expansion code (logic3)
// 1.4] Dates
if (null != qt.time) {
nonEmpty = true;
if (sQueryTerm.length() > 1) {
sQueryTerm.append(" AND ");
}
sQueryTerm.append('(');
BaseQueryBuilder termQ = this.parseDateTerm(qt.time, sQueryTerm, (null != qt.entityOpt) && qt.entityOpt.lockDate);
if (null == term) {
term = termQ;
}
else if (null == boolTerm) {
term = (boolTerm = QueryBuilders.boolQuery().must(term).must(termQ));
}
else {
term = (boolTerm = boolTerm.must(termQ));
}
sQueryTerm.append(')');
}//TESTED (logic5-10)
// 1.5] Geo
if (null != qt.geo)
{
nonEmpty = true;
if (sQueryTerm.length() > 1)
{
sQueryTerm.append(" AND ");
}
sQueryTerm.append('(');
BaseQueryBuilder termQ = this.parseGeoTerm(qt.geo, sQueryTerm, GeoParseField.ALL);
if (null != termQ)
{
if (null == term)
{
term = termQ;
}
else if (null == boolTerm)
{
term = (boolTerm = QueryBuilders.boolQuery().must(term).must(termQ));
}
else
{
term = (boolTerm = boolTerm.must(termQ));
}
}
sQueryTerm.append(')');
} // (end geo)
if (null == qt.assoc) qt.assoc = qt.event;
//(continue to support the old "event" name for another release)
if (null != qt.assoc) {
nonEmpty = true;
if (sQueryTerm.length() > 1) {
sQueryTerm.append(" AND ");
}
sQueryTerm.append('(');
BaseQueryBuilder termQ = QueryBuilders.nestedQuery(DocumentPojo.associations_, this.parseAssociationTerm(qt.assoc, qt.sentiment, sQueryTerm));
if (null != termQ) {
if (null == term) {
term = termQ;
}
else if (null == boolTerm) {