public void testFQ6() throws Exception {
   Query q =
       new FilteredQuery(
           new TermQuery(new Term(FIELD, "xx")), new ItemizedFilter(new int[] {1, 3}));
   q.setBoost(1000);
   qtest(q, new int[] {3});
 }
Esempio n. 2
0
  /**
   * @param query
   * @param virtualWikiNames comma separated list of virtual wiki names
   * @param languages comma separated list of language codes to search in, may be null to search all
   *     languages
   */
  private Query buildQuery(String query, String virtualWikiNames, String languages)
      throws ParseException {
    // build a query like this: <user query string> AND <wikiNamesQuery> AND
    // <languageQuery>
    BooleanQuery bQuery = new BooleanQuery();
    Query parsedQuery = null;

    // for object search
    if (query.startsWith("PROP ")) {
      String property = query.substring(0, query.indexOf(":"));
      query = query.substring(query.indexOf(":") + 1, query.length());
      QueryParser qp = new QueryParser(Version.LUCENE_36, property, this.analyzer);
      parsedQuery = qp.parse(query);
      bQuery.add(parsedQuery, BooleanClause.Occur.MUST);
    } else if (query.startsWith("MULTI ")) {
      // for fulltext search
      List<String> fieldList = IndexUpdater.fields;
      String[] fields = fieldList.toArray(new String[fieldList.size()]);
      BooleanClause.Occur[] flags = new BooleanClause.Occur[fields.length];
      for (int i = 0; i < flags.length; i++) {
        flags[i] = BooleanClause.Occur.SHOULD;
      }
      parsedQuery =
          MultiFieldQueryParser.parse(Version.LUCENE_36, query, fields, flags, this.analyzer);
      bQuery.add(parsedQuery, BooleanClause.Occur.MUST);
    } else {
      String[] fields =
          new String[] {
            IndexFields.FULLTEXT,
            IndexFields.DOCUMENT_TITLE,
            IndexFields.DOCUMENT_NAME,
            IndexFields.FILENAME
          };
      BooleanClause.Occur[] flags = new BooleanClause.Occur[fields.length];
      for (int i = 0; i < flags.length; i++) {
        flags[i] = BooleanClause.Occur.SHOULD;
      }
      QueryParser parser = new MultiFieldQueryParser(Version.LUCENE_36, fields, this.analyzer);
      parsedQuery = parser.parse(query);
      // Since the sub-queries are OR-ed, each sub-query score is normally divided by the number of
      // sub-queries,
      // which would cause extra-small scores whenever there's a hit on only one sub-query;
      // compensate this by boosting the whole outer query
      parsedQuery.setBoost(fields.length);
      bQuery.add(parsedQuery, BooleanClause.Occur.MUST);
    }

    if (virtualWikiNames != null && virtualWikiNames.length() > 0) {
      bQuery.add(
          buildOredTermQuery(virtualWikiNames, IndexFields.DOCUMENT_WIKI),
          BooleanClause.Occur.MUST);
    }
    if (languages != null && languages.length() > 0) {
      bQuery.add(
          buildOredTermQuery(languages, IndexFields.DOCUMENT_LANGUAGE), BooleanClause.Occur.MUST);
    }

    return bQuery;
  }
 public void testFQ6() throws Exception {
   Query q =
       new FilteredQuery(
           new TermQuery(new Term(FIELD, "xx")),
           new QueryWrapperFilter(matchTheseItems(new int[] {1, 3})));
   q.setBoost(1000);
   qtest(q, new int[] {3});
 }
Esempio n. 4
0
  public void testEqualsHashcode() {
    Query query = new RangeQuery(new Term("content", "A"), new Term("content", "C"), true);
    query.setBoost(1.0f);
    Query other = new RangeQuery(new Term("content", "A"), new Term("content", "C"), true);
    other.setBoost(1.0f);

    assertEquals("query equals itself is true", query, query);
    assertEquals("equivalent queries are equal", query, other);
    assertEquals(
        "hashcode must return same value when equals is true", query.hashCode(), other.hashCode());

    other.setBoost(2.0f);
    assertFalse("Different boost queries are not equal", query.equals(other));

    other = new RangeQuery(new Term("notcontent", "A"), new Term("notcontent", "C"), true);
    assertFalse("Different fields are not equal", query.equals(other));

    other = new RangeQuery(new Term("content", "X"), new Term("content", "C"), true);
    assertFalse("Different lower terms are not equal", query.equals(other));

    other = new RangeQuery(new Term("content", "A"), new Term("content", "Z"), true);
    assertFalse("Different upper terms are not equal", query.equals(other));

    query = new RangeQuery(null, new Term("content", "C"), true);
    other = new RangeQuery(null, new Term("content", "C"), true);
    assertEquals("equivalent queries with null lowerterms are equal()", query, other);
    assertEquals(
        "hashcode must return same value when equals is true", query.hashCode(), other.hashCode());

    query = new RangeQuery(new Term("content", "C"), null, true);
    other = new RangeQuery(new Term("content", "C"), null, true);
    assertEquals("equivalent queries with null upperterms are equal()", query, other);
    assertEquals("hashcode returns same value", query.hashCode(), other.hashCode());

    query = new RangeQuery(null, new Term("content", "C"), true);
    other = new RangeQuery(new Term("content", "C"), null, true);
    assertFalse("queries with different upper and lower terms are not equal", query.equals(other));

    query = new RangeQuery(new Term("content", "A"), new Term("content", "C"), false);
    other = new RangeQuery(new Term("content", "A"), new Term("content", "C"), true);
    assertFalse("queries with different inclusive are not equal", query.equals(other));
  }
 @Override
 public Query rewrite(IndexReader reader) throws IOException {
   if (query != null) {
     Query rewritten = query.rewrite(reader);
     if (rewritten != query) {
       rewritten = new ConstantScoreQuery(rewritten);
       rewritten.setBoost(this.getBoost());
       return rewritten;
     }
   }
   return this;
 }
 public static JsonObject fromLuceneFilter(Filter luceneFilter) {
   if (luceneFilter instanceof QueryWrapperFilter) {
     Query query = ((QueryWrapperFilter) luceneFilter).getQuery();
     query.setBoost(luceneFilter.getBoost() * query.getBoost());
     return fromLuceneQuery(query);
   } else if (luceneFilter instanceof DistanceFilter) {
     return convertDistanceFilter((DistanceFilter) luceneFilter);
   } else if (luceneFilter instanceof SpatialHashFilter) {
     return convertSpatialHashFilter((SpatialHashFilter) luceneFilter);
   } else if (luceneFilter instanceof CachingWrapperFilter) {
     return fromLuceneFilter(((CachingWrapperFilter) luceneFilter).getCachedFilter());
   }
   throw LOG.cannotTransformLuceneFilterIntoEsQuery(luceneFilter);
 }
 @Override
 public Query rewrite(IndexReader reader) throws IOException {
   if (query != null) {
     Query rewritten = query.rewrite(reader);
     if (rewritten != query) {
       rewritten = new ImageFeatureScoreQuery(rewritten);
       rewritten.setBoost(this.getBoost());
       return rewritten;
     }
   } else {
     assert filter != null;
     // Fix outdated usage pattern from Lucene 2.x/early-3.x:
     // because ConstantScoreQuery only accepted filters,
     // QueryWrapperFilter was used to wrap queries.
     if (filter instanceof QueryWrapperFilter) {
       final QueryWrapperFilter qwf = (QueryWrapperFilter) filter;
       final Query rewritten = new ImageFeatureScoreQuery(qwf.getQuery().rewrite(reader));
       rewritten.setBoost(this.getBoost());
       return rewritten;
     }
   }
   return this;
 }
 private static Query createKeywordQuery(
     final Analyzer queryAnalyser,
     final String keywords,
     final Collection<DtField> searchedFieldList,
     final Optional<DtField> boostedField)
     throws IOException {
   if (StringUtil.isEmpty(keywords)) {
     return new MatchAllDocsQuery();
   }
   // -----
   final BooleanQuery query = new BooleanQuery();
   for (final DtField dtField : searchedFieldList) {
     final Query queryWord = createParsedKeywordsQuery(queryAnalyser, dtField.name(), keywords);
     if (boostedField.isPresent() && dtField.equals(boostedField.get())) {
       queryWord.setBoost(4);
     }
     query.add(queryWord, BooleanClause.Occur.SHOULD);
   }
   return query;
 }
 /**
  * Should be called before executing the main query and after all other parameters have been set.
  */
 public void preProcess() {
   if (query() == null) {
     parsedQuery(ParsedQuery.parsedMatchAllQuery());
   }
   if (queryBoost() != 1.0f) {
     parsedQuery(
         new ParsedQuery(
             new FunctionScoreQuery(query(), new BoostScoreFunction(queryBoost)), parsedQuery()));
   }
   Filter searchFilter = searchFilter(types());
   if (searchFilter != null) {
     if (Queries.isConstantMatchAllQuery(query())) {
       Query q = new XConstantScoreQuery(searchFilter);
       q.setBoost(query().getBoost());
       parsedQuery(new ParsedQuery(q, parsedQuery()));
     } else {
       parsedQuery(new ParsedQuery(new XFilteredQuery(query(), searchFilter), parsedQuery()));
     }
   }
 }
 @Override
 public Query getQuery(Element e) throws ParserException {
   String text = DOMUtils.getText(e);
   try {
     Query q = null;
     if (unSafeParser != null) {
       // synchronize on unsafe parser
       synchronized (unSafeParser) {
         q = unSafeParser.parse(text);
       }
     } else {
       String fieldName = DOMUtils.getAttribute(e, "fieldName", defaultField);
       // Create new parser
       QueryParser parser = createQueryParser(fieldName, analyzer);
       q = parser.parse(text);
     }
     q.setBoost(DOMUtils.getAttribute(e, "boost", 1.0f));
     return q;
   } catch (ParseException e1) {
     throw new ParserException(e1.getMessage());
   }
 }
  /**
   * Should be called before executing the main query and after all other parameters have been set.
   */
  @Override
  public void preProcess() {
    if (!(from() == -1 && size() == -1)) {
      // from and size have been set.
      int numHits = from() + size();
      if (numHits < 0) {
        String msg =
            "Result window is too large, from + size must be less than or equal to: ["
                + Integer.MAX_VALUE
                + "] but was ["
                + (((long) from()) + ((long) size()))
                + "]";
        throw new QueryPhaseExecutionException(this, msg);
      }
    }

    if (query() == null) {
      parsedQuery(ParsedQuery.parsedMatchAllQuery());
    }
    if (queryBoost() != 1.0f) {
      parsedQuery(
          new ParsedQuery(
              new FunctionScoreQuery(query(), new BoostScoreFunction(queryBoost)), parsedQuery()));
    }
    Query searchFilter = searchFilter(types());
    if (searchFilter != null) {
      if (Queries.isConstantMatchAllQuery(query())) {
        Query q = new ConstantScoreQuery(searchFilter);
        q.setBoost(query().getBoost());
        parsedQuery(new ParsedQuery(q, parsedQuery()));
      } else {
        BooleanQuery filtered = new BooleanQuery();
        filtered.add(query(), Occur.MUST);
        filtered.add(searchFilter, Occur.FILTER);
        parsedQuery(new ParsedQuery(filtered, parsedQuery()));
      }
    }
  }
Esempio n. 12
0
  /** {@inheritDoc} */
  @Override
  public Query query(Options schema) {

    if (field == null || field.trim().isEmpty()) {
      throw new IllegalArgumentException("Field name required");
    }
    if (value == null || value.trim().isEmpty()) {
      throw new IllegalArgumentException("Field value required");
    }

    Query query;
    Properties properties = schema.getProperties(field);
    Properties.Type fieldType = properties != null ? properties.getType() : Properties.Type.text;
    if (fieldType.isCharSeq()) {
      Term term = new Term(field, value);
      query = new RegexpQuery(term);
    } else {
      String message = String.format("Regexp queries are not supported by %s mapper", fieldType);
      throw new UnsupportedOperationException(message);
    }
    query.setBoost(boost);
    return query;
  }
 /**
  * Optimize our representation and our subqueries representations
  *
  * @param reader the IndexReader we query
  * @return an optimized copy of us (which may not be a copy if there is nothing to optimize)
  */
 @Override
 public Query rewrite(IndexReader reader) throws IOException {
   int numDisjunctions = disjuncts.size();
   if (numDisjunctions == 1) {
     Query singleton = disjuncts.get(0);
     Query result = singleton.rewrite(reader);
     if (getBoost() != 1.0f) {
       if (result == singleton) result = result.clone();
       result.setBoost(getBoost() * result.getBoost());
     }
     return result;
   }
   DisjunctionMaxQuery clone = null;
   for (int i = 0; i < numDisjunctions; i++) {
     Query clause = disjuncts.get(i);
     Query rewrite = clause.rewrite(reader);
     if (rewrite != clause) {
       if (clone == null) clone = this.clone();
       clone.disjuncts.set(i, rewrite);
     }
   }
   if (clone != null) return clone;
   else return this;
 }
 @Override
 protected void setFinalBoost(Query query) {
   query.setBoost(boost * query.getBoost());
 }
  @Override
  public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
    XContentParser parser = parseContext.parser();

    XContentParser.Token token = parser.nextToken();
    if (token != XContentParser.Token.FIELD_NAME) {
      throw new QueryParsingException(
          parseContext.index(), "[range] query malformed, no field to indicate field name");
    }
    String fieldName = parser.currentName();
    token = parser.nextToken();
    if (token != XContentParser.Token.START_OBJECT) {
      throw new QueryParsingException(
          parseContext.index(), "[range] query malformed, after field missing start object");
    }

    BytesRef from = null;
    BytesRef to = null;
    boolean includeLower = true;
    boolean includeUpper = true;
    float boost = 1.0f;

    String currentFieldName = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
      if (token == XContentParser.Token.FIELD_NAME) {
        currentFieldName = parser.currentName();
      } else {
        if ("from".equals(currentFieldName)) {
          from = parser.bytesOrNull();
        } else if ("to".equals(currentFieldName)) {
          to = parser.bytesOrNull();
        } else if ("include_lower".equals(currentFieldName)
            || "includeLower".equals(currentFieldName)) {
          includeLower = parser.booleanValue();
        } else if ("include_upper".equals(currentFieldName)
            || "includeUpper".equals(currentFieldName)) {
          includeUpper = parser.booleanValue();
        } else if ("boost".equals(currentFieldName)) {
          boost = parser.floatValue();
        } else if ("gt".equals(currentFieldName)) {
          from = parser.bytesOrNull();
          includeLower = false;
        } else if ("gte".equals(currentFieldName) || "ge".equals(currentFieldName)) {
          from = parser.bytesOrNull();
          includeLower = true;
        } else if ("lt".equals(currentFieldName)) {
          to = parser.bytesOrNull();
          includeUpper = false;
        } else if ("lte".equals(currentFieldName) || "le".equals(currentFieldName)) {
          to = parser.bytesOrNull();
          includeUpper = true;
        } else {
          throw new QueryParsingException(
              parseContext.index(), "[range] query does not support [" + currentFieldName + "]");
        }
      }
    }

    // move to the next end object, to close the field name
    token = parser.nextToken();
    if (token != XContentParser.Token.END_OBJECT) {
      throw new QueryParsingException(
          parseContext.index(), "[range] query malformed, does not end with an object");
    }

    Query query = null;
    MapperService.SmartNameFieldMappers smartNameFieldMappers =
        parseContext.smartFieldMappers(fieldName);
    if (smartNameFieldMappers != null) {
      if (smartNameFieldMappers.hasMapper()) {
        // LUCENE 4 UPGRADE Mapper#rangeQuery should use bytesref as well?
        query =
            smartNameFieldMappers
                .mapper()
                .rangeQuery(
                    from != null ? from.utf8ToString() : null,
                    to != null ? to.utf8ToString() : null,
                    includeLower,
                    includeUpper,
                    parseContext);
      }
    }
    if (query == null) {
      query = new TermRangeQuery(fieldName, from, to, includeLower, includeUpper);
    }
    query.setBoost(boost);
    return wrapSmartNameQuery(query, smartNameFieldMappers, parseContext);
  }
 /** macro */
 protected Query tq(String f, String t, float b) {
   Query q = tq(f, t);
   q.setBoost(b);
   return q;
 }
  @Override
  public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
    XContentParser parser = parseContext.parser();

    Query query = null;
    boolean queryFound = false;
    Filter filter = null;
    boolean filterFound = false;
    float boost = 1.0f;
    boolean cache = false;
    CacheKeyFilter.Key cacheKey = null;

    String currentFieldName = null;
    XContentParser.Token token;
    FilteredQuery.FilterStrategy filterStrategy = XFilteredQuery.CUSTOM_FILTER_STRATEGY;

    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
      if (token == XContentParser.Token.FIELD_NAME) {
        currentFieldName = parser.currentName();
      } else if (token == XContentParser.Token.START_OBJECT) {
        if ("query".equals(currentFieldName)) {
          queryFound = true;
          query = parseContext.parseInnerQuery();
        } else if ("filter".equals(currentFieldName)) {
          filterFound = true;
          filter = parseContext.parseInnerFilter();
        } else {
          throw new QueryParsingException(
              parseContext.index(), "[filtered] query does not support [" + currentFieldName + "]");
        }
      } else if (token.isValue()) {
        if ("strategy".equals(currentFieldName)) {
          String value = parser.text();
          if ("query_first".equals(value) || "queryFirst".equals(value)) {
            filterStrategy = FilteredQuery.QUERY_FIRST_FILTER_STRATEGY;
          } else if ("random_access_random".equals(value) || "randomAccessAlways".equals(value)) {
            filterStrategy = XFilteredQuery.ALWAYS_RANDOM_ACCESS_FILTER_STRATEGY;
          } else if ("leap_frog".equals(value) || "leapFrog".equals(value)) {
            filterStrategy = FilteredQuery.LEAP_FROG_QUERY_FIRST_STRATEGY;
          } else if (value.startsWith("random_access_")) {
            int threshold = Integer.parseInt(value.substring("random_access_".length()));
            filterStrategy = new XFilteredQuery.CustomRandomAccessFilterStrategy(threshold);
          } else if (value.startsWith("randomAccess")) {
            int threshold = Integer.parseInt(value.substring("randomAccess".length()));
            filterStrategy = new XFilteredQuery.CustomRandomAccessFilterStrategy(threshold);
          } else if ("leap_frog_query_first".equals(value) || "leapFrogQueryFirst".equals(value)) {
            filterStrategy = FilteredQuery.LEAP_FROG_QUERY_FIRST_STRATEGY;
          } else if ("leap_frog_filter_first".equals(value)
              || "leapFrogFilterFirst".equals(value)) {
            filterStrategy = FilteredQuery.LEAP_FROG_FILTER_FIRST_STRATEGY;
          } else {
            throw new QueryParsingException(
                parseContext.index(), "[filtered] strategy value not supported [" + value + "]");
          }
        } else if ("boost".equals(currentFieldName)) {
          boost = parser.floatValue();
        } else if ("_cache".equals(currentFieldName)) {
          cache = parser.booleanValue();
        } else if ("_cache_key".equals(currentFieldName) || "_cacheKey".equals(currentFieldName)) {
          cacheKey = new CacheKeyFilter.Key(parser.text());
        } else {
          throw new QueryParsingException(
              parseContext.index(), "[filtered] query does not support [" + currentFieldName + "]");
        }
      }
    }
    if (!queryFound) {
      throw new QueryParsingException(parseContext.index(), "[filtered] requires 'query' element");
    }
    if (query == null) {
      return null;
    }
    if (filter == null) {
      if (!filterFound) {
        // we allow for null filter, so it makes compositions on the client side to be simpler
        return query;
      } else {
        // the filter was provided, but returned null, meaning we should discard it, this means no
        // matches for this query...
        return Queries.NO_MATCH_QUERY;
      }
    }
    if (filter == Queries.MATCH_ALL_FILTER) {
      // this is an instance of match all filter, just execute the query
      return query;
    }

    // cache if required
    if (cache) {
      filter = parseContext.cacheFilter(filter, cacheKey);
    }

    // if its a match_all query, use constant_score
    if (Queries.isConstantMatchAllQuery(query)) {
      Query q = new XConstantScoreQuery(filter);
      q.setBoost(boost);
      return q;
    }

    XFilteredQuery filteredQuery = new XFilteredQuery(query, filter, filterStrategy);
    filteredQuery.setBoost(boost);
    return filteredQuery;
  }
 public void testMA2() throws Exception {
   Query q = new MatchAllDocsQuery();
   q.setBoost(1000);
   qtest(q, new int[] {0, 1, 2, 3});
 }
Esempio n. 19
0
  @Override
  public Filter parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
    XContentParser parser = parseContext.parser();

    Query query = null;
    Filter filter = null;
    float boost = 1.0f;
    String scope = null;
    String path = null;
    boolean cache = false;
    String filterName = null;

    // we need a late binding filter so we can inject a parent nested filter inner nested queries
    NestedQueryParser.LateBindingParentFilter currentParentFilterContext =
        NestedQueryParser.parentFilterContext.get();

    NestedQueryParser.LateBindingParentFilter usAsParentFilter =
        new NestedQueryParser.LateBindingParentFilter();
    NestedQueryParser.parentFilterContext.set(usAsParentFilter);

    String currentFieldName = null;
    XContentParser.Token token;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
      if (token == XContentParser.Token.FIELD_NAME) {
        currentFieldName = parser.currentName();
      } else if (token == XContentParser.Token.START_OBJECT) {
        if ("query".equals(currentFieldName)) {
          query = parseContext.parseInnerQuery();
        } else if ("filter".equals(currentFieldName)) {
          filter = parseContext.parseInnerFilter();
        }
      } else if (token.isValue()) {
        if ("path".equals(currentFieldName)) {
          path = parser.text();
        } else if ("boost".equals(currentFieldName)) {
          boost = parser.floatValue();
        } else if ("_scope".equals(currentFieldName)) {
          scope = parser.text();
        } else if ("_name".equals(currentFieldName)) {
          filterName = parser.text();
        } else if ("_cache".equals(currentFieldName)) {
          cache = parser.booleanValue();
        }
      }
    }
    if (query == null && filter == null) {
      throw new QueryParsingException(
          parseContext.index(), "[nested] requires either 'query' or 'filter' field");
    }
    if (path == null) {
      throw new QueryParsingException(parseContext.index(), "[nested] requires 'path' field");
    }

    if (filter != null) {
      query = new DeletionAwareConstantScoreQuery(filter);
    }

    query.setBoost(boost);

    MapperService.SmartNameObjectMapper mapper =
        parseContext.mapperService().smartNameObjectMapper(path);
    if (mapper == null) {
      throw new QueryParsingException(
          parseContext.index(), "[nested] failed to find nested object under path [" + path + "]");
    }
    ObjectMapper objectMapper = mapper.mapper();
    if (objectMapper == null) {
      throw new QueryParsingException(
          parseContext.index(), "[nested] failed to find nested object under path [" + path + "]");
    }
    if (!objectMapper.nested().isNested()) {
      throw new QueryParsingException(
          parseContext.index(),
          "[nested] nested object under path [" + path + "] is not of nested type");
    }

    Filter childFilter = parseContext.cacheFilter(objectMapper.nestedTypeFilter());
    usAsParentFilter.filter = childFilter;
    // wrap the child query to only work on the nested path type
    query = new FilteredQuery(query, childFilter);

    Filter parentFilter = currentParentFilterContext;
    if (parentFilter == null) {
      parentFilter = NonNestedDocsFilter.INSTANCE;
      if (mapper.hasDocMapper()) {
        // filter based on the type...
        parentFilter = mapper.docMapper().typeFilter();
      }
      parentFilter = parseContext.cacheFilter(parentFilter);
    }

    // restore the thread local one...
    NestedQueryParser.parentFilterContext.set(currentParentFilterContext);

    BlockJoinQuery joinQuery =
        new BlockJoinQuery(query, parentFilter, BlockJoinQuery.ScoreMode.None);

    if (scope != null) {
      SearchContext.current().addNestedQuery(scope, joinQuery);
    }

    Filter joinFilter = new QueryWrapperFilter(joinQuery);
    if (cache) {
      joinFilter = parseContext.cacheFilter(joinFilter);
    }
    if (filterName != null) {
      parseContext.addNamedFilter(filterName, joinFilter);
    }
    return joinFilter;
  }
 public void setBoost(float boost) {
   q.setBoost(boost);
 }
Esempio n. 21
0
  @SuppressWarnings("unused")
  List<Query> createShouldQueries(Map<Short, Collection<?>> records) throws ParseException {
    List<Query> list = new ArrayList<Query>();

    // today
    Calendar calendar = Calendar.getInstance();
    long max = calendar.getTimeInMillis();
    calendar.set(Calendar.HOUR, 0);
    long min = calendar.getTimeInMillis();
    StringBuilder builder = new StringBuilder();
    builder.append('[').append(min).append(" TO ").append(max).append(']');
    Query timeQuery = timeParser.parse(builder.toString());
    timeQuery.setBoost(10.0f);
    list.add(timeQuery);

    //    System.out.println(" boost  " + calendar.get(Calendar.DATE) +  " : "+
    // timeQuery.getBoost());

    // yestoday
    max = min;
    calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 1);
    min = calendar.getTimeInMillis();
    builder.setLength(0);
    builder.append('[').append(min).append(" TO ").append(max).append(']');
    timeQuery = timeParser.parse(builder.toString());
    timeQuery.setBoost(8.0f);
    list.add(timeQuery);

    //    System.out.println(" boost  " + calendar.get(Calendar.DATE) +  " : "+
    // timeQuery.getBoost());

    max = min;
    calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 1);
    min = calendar.getTimeInMillis();
    builder.setLength(0);
    builder.append('[').append(min).append(" TO ").append(max).append(']');
    timeQuery = timeParser.parse(builder.toString());
    timeQuery.setBoost(7.0f);
    list.add(timeQuery);

    //    System.out.println(" boost  " + calendar.get(Calendar.DATE) +  " : "+
    // timeQuery.getBoost());

    max = min;
    calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 1);
    min = calendar.getTimeInMillis();
    builder.setLength(0);
    builder.append('[').append(min).append(" TO ").append(max).append(']');
    timeQuery = timeParser.parse(builder.toString());
    timeQuery.setBoost(4.0f);
    list.add(timeQuery);

    //    System.out.println(" boost  " + calendar.get(Calendar.DATE) +  " : "+
    // timeQuery.getBoost());

    max = min;
    calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 2);
    min = calendar.getTimeInMillis();
    builder.setLength(0);
    builder.append('[').append(min).append(" TO ").append(max).append(']');
    timeQuery = timeParser.parse(builder.toString());
    timeQuery.setBoost(2.0f);
    list.add(timeQuery);

    //    System.out.println(" boost  " + calendar.get(Calendar.DATE) +  " : "+
    // timeQuery.getBoost());

    // for source time
    calendar = Calendar.getInstance();
    max = calendar.getTimeInMillis();
    calendar.set(Calendar.HOUR, 0);
    min = calendar.getTimeInMillis();
    builder.setLength(0);
    builder.append('[').append(min).append(" TO ").append(max).append(']');
    timeQuery = sourceTimeParser.parse(builder.toString());
    timeQuery.setBoost(7.0f);
    list.add(timeQuery);

    //    System.out.println(" source time boost  " + calendar.get(Calendar.DATE) +  " : "+
    // timeQuery.getBoost());

    calendar = Calendar.getInstance();
    max = calendar.getTimeInMillis();
    calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 1);
    min = calendar.getTimeInMillis();
    builder.setLength(0);
    builder.append('[').append(min).append(" TO ").append(max).append(']');
    timeQuery = sourceTimeParser.parse(builder.toString());
    timeQuery.setBoost(5.0f);
    list.add(timeQuery);

    //    System.out.println(" source time boost  " + calendar.get(Calendar.DATE)  +  " : "+
    // timeQuery.getBoost() );

    calendar = Calendar.getInstance();
    max = calendar.getTimeInMillis();
    calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 1);
    min = calendar.getTimeInMillis();
    builder.setLength(0);
    builder.append('[').append(min).append(" TO ").append(max).append(']');
    timeQuery = sourceTimeParser.parse(builder.toString());
    timeQuery.setBoost(2.0f);
    list.add(timeQuery);

    //    System.out.println(" source time boost  " + calendar.get(Calendar.DATE) +  " : "+
    // timeQuery.getBoost());

    //    TermRangeQuery tquery = new TermRangeQuery(
    //        "time", String.valueOf(min), String.valueOf(max), true, true);
    //    list.add(tquery);

    return list;
  }
  @Override
  public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
    ensureNotDeleteByQuery(NAME, parseContext);
    XContentParser parser = parseContext.parser();

    boolean queryFound = false;
    float boost = 1.0f;
    String childType = null;
    ScoreType scoreType = ScoreType.NONE;
    int minChildren = 0;
    int maxChildren = 0;
    int shortCircuitParentDocSet = 8192;
    String queryName = null;
    Tuple<String, SubSearchContext> innerHits = null;

    String currentFieldName = null;
    XContentParser.Token token;
    XContentStructure.InnerQuery iq = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
      if (token == XContentParser.Token.FIELD_NAME) {
        currentFieldName = parser.currentName();
      } else if (token == XContentParser.Token.START_OBJECT) {
        // Usually, the query would be parsed here, but the child
        // type may not have been extracted yet, so use the
        // XContentStructure.<type> facade to parse if available,
        // or delay parsing if not.
        if ("query".equals(currentFieldName)) {
          iq =
              new XContentStructure.InnerQuery(
                  parseContext, childType == null ? null : new String[] {childType});
          queryFound = true;
        } else if ("inner_hits".equals(currentFieldName)) {
          innerHits = innerHitsQueryParserHelper.parse(parseContext);
        } else {
          throw new QueryParsingException(
              parseContext.index(),
              "[has_child] query does not support [" + currentFieldName + "]");
        }
      } else if (token.isValue()) {
        if ("type".equals(currentFieldName)
            || "child_type".equals(currentFieldName)
            || "childType".equals(currentFieldName)) {
          childType = parser.text();
        } else if ("score_type".equals(currentFieldName) || "scoreType".equals(currentFieldName)) {
          scoreType = ScoreType.fromString(parser.text());
        } else if ("score_mode".equals(currentFieldName) || "scoreMode".equals(currentFieldName)) {
          scoreType = ScoreType.fromString(parser.text());
        } else if ("boost".equals(currentFieldName)) {
          boost = parser.floatValue();
        } else if ("min_children".equals(currentFieldName)
            || "minChildren".equals(currentFieldName)) {
          minChildren = parser.intValue(true);
        } else if ("max_children".equals(currentFieldName)
            || "maxChildren".equals(currentFieldName)) {
          maxChildren = parser.intValue(true);
        } else if ("short_circuit_cutoff".equals(currentFieldName)) {
          shortCircuitParentDocSet = parser.intValue();
        } else if ("_name".equals(currentFieldName)) {
          queryName = parser.text();
        } else {
          throw new QueryParsingException(
              parseContext.index(),
              "[has_child] query does not support [" + currentFieldName + "]");
        }
      }
    }
    if (!queryFound) {
      throw new QueryParsingException(parseContext.index(), "[has_child] requires 'query' field");
    }
    if (childType == null) {
      throw new QueryParsingException(parseContext.index(), "[has_child] requires 'type' field");
    }

    Query innerQuery = iq.asQuery(childType);

    if (innerQuery == null) {
      return null;
    }
    innerQuery.setBoost(boost);

    DocumentMapper childDocMapper = parseContext.mapperService().documentMapper(childType);
    if (childDocMapper == null) {
      throw new QueryParsingException(
          parseContext.index(), "[has_child] No mapping for for type [" + childType + "]");
    }
    if (!childDocMapper.parentFieldMapper().active()) {
      throw new QueryParsingException(
          parseContext.index(),
          "[has_child]  Type [" + childType + "] does not have parent mapping");
    }

    if (innerHits != null) {
      InnerHitsContext.ParentChildInnerHits parentChildInnerHits =
          new InnerHitsContext.ParentChildInnerHits(
              innerHits.v2(), innerQuery, null, childDocMapper);
      String name = innerHits.v1() != null ? innerHits.v1() : childType;
      parseContext.addInnerHits(name, parentChildInnerHits);
    }

    ParentFieldMapper parentFieldMapper = childDocMapper.parentFieldMapper();
    if (!parentFieldMapper.active()) {
      throw new QueryParsingException(
          parseContext.index(), "[has_child] _parent field not configured");
    }

    String parentType = parentFieldMapper.type();
    DocumentMapper parentDocMapper = parseContext.mapperService().documentMapper(parentType);
    if (parentDocMapper == null) {
      throw new QueryParsingException(
          parseContext.index(),
          "[has_child]  Type ["
              + childType
              + "] points to a non existent parent type ["
              + parentType
              + "]");
    }

    if (maxChildren > 0 && maxChildren < minChildren) {
      throw new QueryParsingException(
          parseContext.index(), "[has_child] 'max_children' is less than 'min_children'");
    }

    BitDocIdSetFilter nonNestedDocsFilter = null;
    if (parentDocMapper.hasNestedObjects()) {
      nonNestedDocsFilter = parseContext.bitsetFilter(NonNestedDocsFilter.INSTANCE);
    }

    // wrap the query with type query
    innerQuery =
        new FilteredQuery(
            innerQuery,
            parseContext.cacheFilter(
                childDocMapper.typeFilter(), null, parseContext.autoFilterCachePolicy()));

    Query query;
    Filter parentFilter =
        parseContext.cacheFilter(
            parentDocMapper.typeFilter(), null, parseContext.autoFilterCachePolicy());
    ParentChildIndexFieldData parentChildIndexFieldData =
        parseContext.getForField(parentFieldMapper);
    if (minChildren > 1 || maxChildren > 0 || scoreType != ScoreType.NONE) {
      query =
          new ChildrenQuery(
              parentChildIndexFieldData,
              parentType,
              childType,
              parentFilter,
              innerQuery,
              scoreType,
              minChildren,
              maxChildren,
              shortCircuitParentDocSet,
              nonNestedDocsFilter);
    } else {
      query =
          new ChildrenConstantScoreQuery(
              parentChildIndexFieldData,
              innerQuery,
              parentType,
              childType,
              parentFilter,
              shortCircuitParentDocSet,
              nonNestedDocsFilter);
    }
    if (queryName != null) {
      parseContext.addNamedFilter(queryName, new CustomQueryWrappingFilter(query));
    }
    query.setBoost(boost);
    return query;
  }
  private void _includeIfUnique(
      BooleanQuery booleanQuery,
      boolean like,
      QueryParser queryParser,
      Query query,
      BooleanClause.Occur occur) {

    if (query instanceof TermQuery) {
      Set<Term> terms = new HashSet<Term>();

      TermQuery termQuery = (TermQuery) query;

      termQuery.extractTerms(terms);

      float boost = termQuery.getBoost();

      for (Term term : terms) {
        String termValue = term.text();

        if (like) {
          termValue = termValue.toLowerCase(queryParser.getLocale());

          term = term.createTerm(StringPool.STAR.concat(termValue).concat(StringPool.STAR));

          query = new WildcardQuery(term);
        } else {
          query = new TermQuery(term);
        }

        query.setBoost(boost);

        boolean included = false;

        for (BooleanClause booleanClause : booleanQuery.getClauses()) {
          if (query.equals(booleanClause.getQuery())) {
            included = true;
          }
        }

        if (!included) {
          booleanQuery.add(query, occur);
        }
      }
    } else if (query instanceof BooleanQuery) {
      BooleanQuery curBooleanQuery = (BooleanQuery) query;

      BooleanQuery containerBooleanQuery = new BooleanQuery();

      for (BooleanClause booleanClause : curBooleanQuery.getClauses()) {
        _includeIfUnique(
            containerBooleanQuery,
            like,
            queryParser,
            booleanClause.getQuery(),
            booleanClause.getOccur());
      }

      if (containerBooleanQuery.getClauses().length > 0) {
        booleanQuery.add(containerBooleanQuery, occur);
      }
    } else {
      boolean included = false;

      for (BooleanClause booleanClause : booleanQuery.getClauses()) {
        if (query.equals(booleanClause.getQuery())) {
          included = true;
        }
      }

      if (!included) {
        booleanQuery.add(query, occur);
      }
    }
  }
  @Override
  public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
    XContentParser parser = parseContext.parser();

    Query query = Queries.newMatchAllQuery();
    Filter filter = null;
    boolean filterFound = false;
    float boost = 1.0f;
    FilterCachingPolicy cache = parseContext.autoFilterCachePolicy();
    HashedBytesRef cacheKey = null;
    String queryName = null;

    String currentFieldName = null;
    XContentParser.Token token;
    FilteredQuery.FilterStrategy filterStrategy = CUSTOM_FILTER_STRATEGY;

    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
      if (token == XContentParser.Token.FIELD_NAME) {
        currentFieldName = parser.currentName();
      } else if (token == XContentParser.Token.START_OBJECT) {
        if ("query".equals(currentFieldName)) {
          query = parseContext.parseInnerQuery();
        } else if ("filter".equals(currentFieldName)) {
          filterFound = true;
          filter = parseContext.parseInnerFilter();
        } else {
          throw new QueryParsingException(
              parseContext.index(), "[filtered] query does not support [" + currentFieldName + "]");
        }
      } else if (token.isValue()) {
        if ("strategy".equals(currentFieldName)) {
          String value = parser.text();
          if ("query_first".equals(value) || "queryFirst".equals(value)) {
            filterStrategy = FilteredQuery.QUERY_FIRST_FILTER_STRATEGY;
          } else if ("random_access_always".equals(value) || "randomAccessAlways".equals(value)) {
            filterStrategy = ALWAYS_RANDOM_ACCESS_FILTER_STRATEGY;
          } else if ("leap_frog".equals(value) || "leapFrog".equals(value)) {
            filterStrategy = FilteredQuery.LEAP_FROG_QUERY_FIRST_STRATEGY;
          } else if (value.startsWith("random_access_")) {
            int threshold = Integer.parseInt(value.substring("random_access_".length()));
            filterStrategy = new CustomRandomAccessFilterStrategy(threshold);
          } else if (value.startsWith("randomAccess")) {
            int threshold = Integer.parseInt(value.substring("randomAccess".length()));
            filterStrategy = new CustomRandomAccessFilterStrategy(threshold);
          } else if ("leap_frog_query_first".equals(value) || "leapFrogQueryFirst".equals(value)) {
            filterStrategy = FilteredQuery.LEAP_FROG_QUERY_FIRST_STRATEGY;
          } else if ("leap_frog_filter_first".equals(value)
              || "leapFrogFilterFirst".equals(value)) {
            filterStrategy = FilteredQuery.LEAP_FROG_FILTER_FIRST_STRATEGY;
          } else {
            throw new QueryParsingException(
                parseContext.index(), "[filtered] strategy value not supported [" + value + "]");
          }
        } else if ("_name".equals(currentFieldName)) {
          queryName = parser.text();
        } else if ("boost".equals(currentFieldName)) {
          boost = parser.floatValue();
        } else if ("_cache".equals(currentFieldName)) {
          cache = parseContext.parseFilterCachePolicy();
        } else if ("_cache_key".equals(currentFieldName) || "_cacheKey".equals(currentFieldName)) {
          cacheKey = new HashedBytesRef(parser.text());
        } else {
          throw new QueryParsingException(
              parseContext.index(), "[filtered] query does not support [" + currentFieldName + "]");
        }
      }
    }

    // parsed internally, but returned null during parsing...
    if (query == null) {
      return null;
    }

    if (filter == null) {
      if (!filterFound) {
        // we allow for null filter, so it makes compositions on the client side to be simpler
        return query;
      } else {
        // even if the filter is not found, and its null, we should simply ignore it, and go
        // by the query
        return query;
      }
    }
    if (filter == Queries.MATCH_ALL_FILTER) {
      // this is an instance of match all filter, just execute the query
      return query;
    }

    // cache if required
    if (cache != null) {
      filter = parseContext.cacheFilter(filter, cacheKey, cache);
    }

    // if its a match_all query, use constant_score
    if (Queries.isConstantMatchAllQuery(query)) {
      Query q = new ConstantScoreQuery(filter);
      q.setBoost(boost);
      return q;
    }

    FilteredQuery filteredQuery = new FilteredQuery(query, filter, filterStrategy);
    filteredQuery.setBoost(boost);
    if (queryName != null) {
      parseContext.addNamedQuery(queryName, filteredQuery);
    }
    return filteredQuery;
  }
 public void testCSQ3() throws Exception {
   Query q = new ConstantScoreQuery(new ItemizedFilter(new int[] {0, 2}));
   q.setBoost(1000);
   qtest(q, new int[] {0, 2});
 }
Esempio n. 26
0
 protected Query tq(float boost, String field, String text) {
   Query query = new TermQuery(new Term(field, text));
   query.setBoost(boost);
   return query;
 }
  @Override
  public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
    XContentParser parser = parseContext.parser();

    boolean queryFound = false;
    float boost = 1.0f;
    String parentType = null;
    boolean score = false;
    String queryName = null;

    String currentFieldName = null;
    XContentParser.Token token;
    XContentStructure.InnerQuery iq = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
      if (token == XContentParser.Token.FIELD_NAME) {
        currentFieldName = parser.currentName();
      } else if (token == XContentParser.Token.START_OBJECT) {
        // Usually, the query would be parsed here, but the child
        // type may not have been extracted yet, so use the
        // XContentStructure.<type> facade to parse if available,
        // or delay parsing if not.
        if ("query".equals(currentFieldName)) {
          iq =
              new XContentStructure.InnerQuery(
                  parseContext, parentType == null ? null : new String[] {parentType});
          queryFound = true;
        } else {
          throw new QueryParsingException(
              parseContext.index(),
              "[has_parent] query does not support [" + currentFieldName + "]");
        }
      } else if (token.isValue()) {
        if ("type".equals(currentFieldName)
            || "parent_type".equals(currentFieldName)
            || "parentType".equals(currentFieldName)) {
          parentType = parser.text();
        } else if ("_scope".equals(currentFieldName)) {
          throw new QueryParsingException(
              parseContext.index(),
              "the [_scope] support in [has_parent] query has been removed, use a filter as a facet_filter in the relevant global facet");
        } else if ("score_type".equals(currentFieldName) || "scoreType".equals(currentFieldName)) {
          String scoreTypeValue = parser.text();
          if ("score".equals(scoreTypeValue)) {
            score = true;
          } else if ("none".equals(scoreTypeValue)) {
            score = false;
          }
        } else if ("score_mode".equals(currentFieldName) || "scoreMode".equals(currentFieldName)) {
          String scoreModeValue = parser.text();
          if ("score".equals(scoreModeValue)) {
            score = true;
          } else if ("none".equals(scoreModeValue)) {
            score = false;
          }
        } else if ("boost".equals(currentFieldName)) {
          boost = parser.floatValue();
        } else if ("_name".equals(currentFieldName)) {
          queryName = parser.text();
        } else {
          throw new QueryParsingException(
              parseContext.index(),
              "[has_parent] query does not support [" + currentFieldName + "]");
        }
      }
    }
    if (!queryFound) {
      throw new QueryParsingException(
          parseContext.index(), "[has_parent] query requires 'query' field");
    }
    if (parentType == null) {
      throw new QueryParsingException(
          parseContext.index(), "[has_parent] query requires 'parent_type' field");
    }

    Query innerQuery = iq.asQuery(parentType);

    if (innerQuery == null) {
      return null;
    }

    DocumentMapper parentDocMapper = parseContext.mapperService().documentMapper(parentType);
    if (parentDocMapper == null) {
      throw new QueryParsingException(
          parseContext.index(),
          "[has_parent] query configured 'parent_type' [" + parentType + "] is not a valid type");
    }

    innerQuery.setBoost(boost);
    // wrap the query with type query
    innerQuery =
        new XFilteredQuery(
            innerQuery, parseContext.cacheFilter(parentDocMapper.typeFilter(), null));

    ParentChildIndexFieldData parentChildIndexFieldData = null;
    Set<String> parentTypes = new HashSet<>(5);
    parentTypes.add(parentType);
    for (DocumentMapper documentMapper : parseContext.mapperService()) {
      ParentFieldMapper parentFieldMapper = documentMapper.parentFieldMapper();
      if (parentFieldMapper.active()) {
        parentChildIndexFieldData = parseContext.fieldData().getForField(parentFieldMapper);
        DocumentMapper parentTypeDocumentMapper =
            parseContext.mapperService().documentMapper(parentFieldMapper.type());
        if (parentTypeDocumentMapper == null) {
          // Only add this, if this parentFieldMapper (also a parent)  isn't a child of another
          // parent.
          parentTypes.add(parentFieldMapper.type());
        }
      }
    }
    if (parentChildIndexFieldData == null) {
      throw new QueryParsingException(
          parseContext.index(), "[has_parent] no _parent field configured");
    }

    Filter parentFilter;
    if (parentTypes.size() == 1) {
      DocumentMapper documentMapper =
          parseContext.mapperService().documentMapper(parentTypes.iterator().next());
      parentFilter = parseContext.cacheFilter(documentMapper.typeFilter(), null);
    } else {
      XBooleanFilter parentsFilter = new XBooleanFilter();
      for (String parentTypeStr : parentTypes) {
        DocumentMapper documentMapper = parseContext.mapperService().documentMapper(parentTypeStr);
        Filter filter = parseContext.cacheFilter(documentMapper.typeFilter(), null);
        parentsFilter.add(filter, BooleanClause.Occur.SHOULD);
      }
      parentFilter = parentsFilter;
    }
    Filter childrenFilter = parseContext.cacheFilter(new NotFilter(parentFilter), null);

    boolean deleteByQuery = "delete_by_query".equals(SearchContext.current().source());
    Query query;
    if (!deleteByQuery && score) {
      query = new ParentQuery(parentChildIndexFieldData, innerQuery, parentType, childrenFilter);
    } else {
      query =
          new ParentConstantScoreQuery(
              parentChildIndexFieldData, innerQuery, parentType, childrenFilter);
      if (deleteByQuery) {
        query = new XConstantScoreQuery(new DeleteByQueryWrappingFilter(query));
      }
    }
    query.setBoost(boost);
    if (queryName != null) {
      parseContext.addNamedFilter(queryName, new CustomQueryWrappingFilter(query));
    }
    return query;
  }
  @Override
  public Query parse(JsonQueryParseContext parseContext) throws IOException, QueryParsingException {
    JsonParser jp = parseContext.jp();

    JsonToken token = jp.nextToken();
    assert token == JsonToken.FIELD_NAME;
    String fieldName = jp.getCurrentName();

    String from = null;
    String to = null;
    boolean includeLower = true;
    boolean includeUpper = true;
    float boost = 1.0f;

    String currentFieldName = null;
    while ((token = jp.nextToken()) != JsonToken.END_OBJECT) {
      if (token == JsonToken.FIELD_NAME) {
        currentFieldName = jp.getCurrentName();
      } else {
        if ("from".equals(currentFieldName)) {
          if (jp.getCurrentToken() == JsonToken.VALUE_NULL) {
            from = null;
          } else {
            from = jp.getText();
          }
        } else if ("to".equals(currentFieldName)) {
          if (jp.getCurrentToken() == JsonToken.VALUE_NULL) {
            to = null;
          } else {
            to = jp.getText();
          }
        } else if ("include_lower".equals(currentFieldName)) {
          if (token == JsonToken.VALUE_NUMBER_INT) {
            includeLower = jp.getIntValue() != 0;
          } else if (token == JsonToken.VALUE_STRING) {
            includeLower = Booleans.parseBoolean(jp.getText(), includeLower);
          } else {
            includeLower = token == JsonToken.VALUE_TRUE;
          }
        } else if ("include_upper".equals(currentFieldName)) {
          if (token == JsonToken.VALUE_NUMBER_INT) {
            includeUpper = jp.getIntValue() != 0;
          } else if (token == JsonToken.VALUE_STRING) {
            includeUpper = Booleans.parseBoolean(jp.getText(), includeUpper);
          } else {
            includeUpper = token == JsonToken.VALUE_TRUE;
          }
        } else if ("boost".equals(currentFieldName)) {
          if (token == JsonToken.VALUE_STRING) {
            boost = Float.parseFloat(jp.getText());
          } else {
            boost = jp.getFloatValue();
          }
        } else if ("gt".equals(currentFieldName)) {
          if (jp.getCurrentToken() == JsonToken.VALUE_NULL) {
            from = null;
          } else {
            from = jp.getText();
          }
          includeLower = false;
        } else if ("gte".equals(currentFieldName)) {
          if (jp.getCurrentToken() == JsonToken.VALUE_NULL) {
            from = null;
          } else {
            from = jp.getText();
          }
          includeLower = true;
        } else if ("lt".equals(currentFieldName)) {
          if (jp.getCurrentToken() == JsonToken.VALUE_NULL) {
            to = null;
          } else {
            to = jp.getText();
          }
          includeUpper = false;
        } else if ("lte".equals(currentFieldName)) {
          if (jp.getCurrentToken() == JsonToken.VALUE_NULL) {
            to = null;
          } else {
            to = jp.getText();
          }
          includeUpper = true;
        }
      }
    }

    // move to the next end object, to close the field name
    token = jp.nextToken();
    assert token == JsonToken.END_OBJECT;

    Query query = null;
    MapperService.SmartNameFieldMappers smartNameFieldMappers =
        parseContext.smartFieldMappers(fieldName);
    if (smartNameFieldMappers != null) {
      if (smartNameFieldMappers.hasMapper()) {
        query = smartNameFieldMappers.mapper().rangeQuery(from, to, includeLower, includeUpper);
      }
    }
    if (query == null) {
      query = new TermRangeQuery(fieldName, from, to, includeLower, includeUpper);
    }
    query.setBoost(boost);
    return wrapSmartNameQuery(query, smartNameFieldMappers, parseContext.indexCache());
  }