Exemplo n.º 1
0
    /**
     * 组装TermRangeQuery
     *
     * @param elements
     * @return
     */
    private TermRangeQuery toTermRangeQuery(Element fieldNameEle, LinkedList<Element> elements) {

      boolean includeFirst = false;
      boolean includeLast = false;
      String firstValue = null;
      String lastValue = null;
      // 检查第一个元素是否是[或者{
      Element first = elements.getFirst();
      if ('[' == first.type) {
        includeFirst = true;
      } else if ('{' == first.type) {
        includeFirst = false;
      } else {
        throw new IllegalStateException("表达式异常");
      }
      // 检查最后一个元素是否是]或者}
      Element last = elements.getLast();
      if (']' == last.type) {
        includeLast = true;
      } else if ('}' == last.type) {
        includeLast = false;
      } else {
        throw new IllegalStateException("表达式异常, RangeQuery缺少结束括号");
      }
      if (elements.size() < 4 || elements.size() > 5) {
        throw new IllegalStateException("表达式异常, RangeQuery 错误");
      }
      // 读出中间部分
      Element e2 = elements.get(1);
      if ('\'' == e2.type) {
        firstValue = e2.toString();
        //
        Element e3 = elements.get(2);
        if (',' != e3.type) {
          throw new IllegalStateException("表达式异常, RangeQuery缺少逗号分隔");
        }
        //
        Element e4 = elements.get(3);
        if ('\'' == e4.type) {
          lastValue = e4.toString();
        } else if (e4 != last) {
          throw new IllegalStateException("表达式异常,RangeQuery格式错误");
        }
      } else if (',' == e2.type) {
        firstValue = null;
        //
        Element e3 = elements.get(2);
        if ('\'' == e3.type) {
          lastValue = e3.toString();
        } else {
          throw new IllegalStateException("表达式异常,RangeQuery格式错误");
        }

      } else {
        throw new IllegalStateException("表达式异常, RangeQuery格式错误");
      }

      return TermRangeQuery.newStringRange(
          fieldNameEle.toString(), firstValue, lastValue, includeFirst, includeLast);
    }
Exemplo n.º 2
0
 @Override
 public TermRangeQuery getQuery(Element e) throws ParserException {
   String fieldName = DOMUtils.getAttributeWithInheritance(e, "fieldName");
   String lowerTerm = e.hasAttribute("lowerTerm") ? e.getAttribute("lowerTerm") : null;
   String upperTerm = e.hasAttribute("upperTerm") ? e.getAttribute("upperTerm") : null;
   boolean includeLower = DOMUtils.getAttribute(e, "includeLower", true);
   boolean includeUpper = DOMUtils.getAttribute(e, "includeUpper", true);
   return TermRangeQuery.newStringRange(
       fieldName, lowerTerm, upperTerm, includeLower, includeUpper);
 }
 /** test sparse filters (match single documents) */
 public void testSparse() throws Exception {
   for (int i = 0; i < 10; i++) {
     int id_start = random().nextInt(ir.maxDoc() - 1);
     int id_end = id_start + 1;
     Query query =
         TermRangeQuery.newStringRange(
             "id", Integer.toString(id_start), Integer.toString(id_end), true, true);
     Filter expected = new QueryWrapperFilter(query);
     Filter actual = new CachingWrapperFilter(expected);
     assertFilterEquals(expected, actual);
   }
 }
 /** DateField specific range query */
 public Query getRangeQuery(
     QParser parser,
     SchemaField sf,
     Date part1,
     Date part2,
     boolean minInclusive,
     boolean maxInclusive) {
   return TermRangeQuery.newStringRange(
       sf.getName(),
       part1 == null ? null : toInternal(part1),
       part2 == null ? null : toInternal(part2),
       minInclusive,
       maxInclusive);
 }
  /**
   * Tests NumericRangeQuery on a multi-valued field (multiple numeric values per document). This
   * test ensures, that a classical TermRangeQuery returns exactly the same document numbers as
   * NumericRangeQuery (see SOLR-1322 for discussion) and the multiple precision terms per numeric
   * value do not interfere with multiple numeric values.
   */
  public void testMultiValuedNRQ() throws Exception {
    Directory directory = newDirectory();
    RandomIndexWriter writer =
        new RandomIndexWriter(
            random(),
            directory,
            newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random()))
                .setMaxBufferedDocs(_TestUtil.nextInt(random(), 50, 1000)));

    DecimalFormat format = new DecimalFormat("00000000000", new DecimalFormatSymbols(Locale.US));

    int num = atLeast(500);
    for (int l = 0; l < num; l++) {
      Document doc = new Document();
      for (int m = 0, c = random().nextInt(10); m <= c; m++) {
        int value = random().nextInt(Integer.MAX_VALUE);
        doc.add(newStringField("asc", format.format(value), Field.Store.NO));
        doc.add(new IntField("trie", value, Field.Store.NO));
      }
      writer.addDocument(doc);
    }
    IndexReader reader = writer.getReader();
    writer.close();

    IndexSearcher searcher = newSearcher(reader);
    num = atLeast(50);
    for (int i = 0; i < num; i++) {
      int lower = random().nextInt(Integer.MAX_VALUE);
      int upper = random().nextInt(Integer.MAX_VALUE);
      if (lower > upper) {
        int a = lower;
        lower = upper;
        upper = a;
      }
      TermRangeQuery cq =
          TermRangeQuery.newStringRange(
              "asc", format.format(lower), format.format(upper), true, true);
      NumericRangeQuery<Integer> tq =
          NumericRangeQuery.newIntRange("trie", lower, upper, true, true);
      TopDocs trTopDocs = searcher.search(cq, 1);
      TopDocs nrTopDocs = searcher.search(tq, 1);
      assertEquals(
          "Returned count for NumericRangeQuery and TermRangeQuery must be equal",
          trTopDocs.totalHits,
          nrTopDocs.totalHits);
    }
    reader.close();
    directory.close();
  }
 /** Returns a random filter over the document set */
 protected Filter randomFilter() {
   return new QueryWrapperFilter(
       TermRangeQuery.newStringRange("field", "a", "" + randomChar(), true, true));
 }