protected <N extends Number> NumericRangeQuery<?> numericRange(
     Class<N> clazz,
     String field,
     @Nullable N min,
     @Nullable N max,
     boolean minInc,
     boolean maxInc) {
   if (clazz.equals(Integer.class)) {
     return NumericRangeQuery.newIntRange(field, (Integer) min, (Integer) max, minInc, maxInc);
   } else if (clazz.equals(Double.class)) {
     return NumericRangeQuery.newDoubleRange(field, (Double) min, (Double) max, minInc, minInc);
   } else if (clazz.equals(Float.class)) {
     return NumericRangeQuery.newFloatRange(field, (Float) min, (Float) max, minInc, minInc);
   } else if (clazz.equals(Long.class)) {
     return NumericRangeQuery.newLongRange(field, (Long) min, (Long) max, minInc, minInc);
   } else if (clazz.equals(Byte.class) || clazz.equals(Short.class)) {
     return NumericRangeQuery.newIntRange(
         field,
         min != null ? min.intValue() : null,
         max != null ? max.intValue() : null,
         minInc,
         maxInc);
   } else {
     throw new IllegalArgumentException("Unsupported numeric type " + clazz.getName());
   }
 }
 @Override
 public Query fuzzyQuery(String value, double minSim, int prefixLength, int maxExpansions) {
   short iValue = Short.parseShort(value);
   short iSim = (short) (minSim * dFuzzyFactor);
   return NumericRangeQuery.newIntRange(
       names.indexName(), precisionStep, iValue - iSim, iValue + iSim, true, true);
 }
Beispiel #3
0
 public static Query createMultiIntQuery(String field, int[] ids) {
   BooleanQuery query = new BooleanQuery();
   for (int id : ids) {
     query.add(NumericRangeQuery.newIntRange(field, id, id, true, true), Occur.SHOULD);
   }
   return query;
 }
  @Override
  public void addNumericRangeTerm(
      BooleanQuery booleanQuery, String field, Integer startValue, Integer endValue) {

    NumericRangeQuery<?> numericRangeQuery =
        NumericRangeQuery.newIntRange(field, startValue, endValue, true, true);

    booleanQuery.add(numericRangeQuery, BooleanClause.Occur.SHOULD);
  }
Beispiel #5
0
  /**
   * Returns a number of random songs.
   *
   * @param criteria Search criteria.
   * @return List of random songs.
   */
  public List<MediaFile> getRandomSongs(RandomSearchCriteria criteria) {
    List<MediaFile> result = new ArrayList<MediaFile>();

    IndexReader reader = null;
    try {
      reader = createIndexReader(SONG);
      Searcher searcher = new IndexSearcher(reader);

      BooleanQuery query = new BooleanQuery();
      query.add(
          new TermQuery(new Term(FIELD_MEDIA_TYPE, MediaFile.MediaType.MUSIC.name().toLowerCase())),
          BooleanClause.Occur.MUST);
      if (criteria.getGenre() != null) {
        String genre = normalizeGenre(criteria.getGenre());
        query.add(new TermQuery(new Term(FIELD_GENRE, genre)), BooleanClause.Occur.MUST);
      }
      if (criteria.getFromYear() != null || criteria.getToYear() != null) {
        NumericRangeQuery<Integer> rangeQuery =
            NumericRangeQuery.newIntRange(
                FIELD_YEAR, criteria.getFromYear(), criteria.getToYear(), true, true);
        query.add(rangeQuery, BooleanClause.Occur.MUST);
      }

      List<SpanTermQuery> musicFolderQueries = new ArrayList<SpanTermQuery>();
      for (MusicFolder musicFolder : criteria.getMusicFolders()) {
        musicFolderQueries.add(
            new SpanTermQuery(new Term(FIELD_FOLDER, musicFolder.getPath().getPath())));
      }
      query.add(
          new SpanOrQuery(musicFolderQueries.toArray(new SpanQuery[musicFolderQueries.size()])),
          BooleanClause.Occur.MUST);

      TopDocs topDocs = searcher.search(query, null, Integer.MAX_VALUE);
      List<ScoreDoc> scoreDocs = Lists.newArrayList(topDocs.scoreDocs);
      Random random = new Random(System.currentTimeMillis());

      while (!scoreDocs.isEmpty() && result.size() < criteria.getCount()) {
        int index = random.nextInt(scoreDocs.size());
        Document doc = searcher.doc(scoreDocs.remove(index).doc);
        int id = Integer.valueOf(doc.get(FIELD_ID));
        try {
          addIfNotNull(mediaFileService.getMediaFile(id), result);
        } catch (Exception x) {
          LOG.warn("Failed to get media file " + id);
        }
      }

    } catch (Throwable x) {
      LOG.error("Failed to search or random songs.", x);
    } finally {
      FileUtil.closeQuietly(reader);
    }
    return result;
  }
 @Override
 public Query rangeQuery(
     Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper) {
   return NumericRangeQuery.newIntRange(
       name(),
       numericPrecisionStep(),
       lowerTerm == null ? null : (int) parseValue(lowerTerm),
       upperTerm == null ? null : (int) parseValue(upperTerm),
       includeLower,
       includeUpper);
 }
 @Override
 public Query fuzzyQuery(
     Object value,
     Fuzziness fuzziness,
     int prefixLength,
     int maxExpansions,
     boolean transpositions) {
   byte iValue = parseValue(value);
   byte iSim = fuzziness.asByte();
   return NumericRangeQuery.newIntRange(
       name(), numericPrecisionStep(), iValue - iSim, iValue + iSim, true, true);
 }
 @Override
 public Query fuzzyQuery(String value, String minSim, int prefixLength, int maxExpansions) {
   short iValue = Short.parseShort(value);
   short iSim;
   try {
     iSim = Short.parseShort(minSim);
   } catch (NumberFormatException e) {
     iSim = (short) Float.parseFloat(minSim);
   }
   return NumericRangeQuery.newIntRange(
       names.indexName(), precisionStep, iValue - iSim, iValue + iSim, true, true);
 }
 @Override
 public Query fuzzyQuery(
     String value,
     Fuzziness fuzziness,
     int prefixLength,
     int maxExpansions,
     boolean transpositions) {
   short iValue = Short.parseShort(value);
   short iSim = fuzziness.asShort();
   return NumericRangeQuery.newIntRange(
       names().indexName(), numericPrecisionStep(), iValue - iSim, iValue + iSim, true, true);
 }
 @Override
 public Query fuzzyQuery(
     String value,
     Fuzziness fuzziness,
     int prefixLength,
     int maxExpansions,
     boolean transpositions) {
   int iValue = Integer.parseInt(value);
   int iSim = fuzziness.asInt();
   return NumericRangeQuery.newIntRange(
       names.indexName(), precisionStep, iValue - iSim, iValue + iSim, true, true);
 }
 @Override
 public Query fuzzyQuery(
     String value, String minSim, int prefixLength, int maxExpansions, boolean transpositions) {
   byte iValue = Byte.parseByte(value);
   byte iSim;
   try {
     iSim = Byte.parseByte(minSim);
   } catch (NumberFormatException e) {
     iSim = (byte) Float.parseFloat(minSim);
   }
   return NumericRangeQuery.newIntRange(
       names.indexName(), precisionStep, iValue - iSim, iValue + iSim, true, true);
 }
 @Override
 public Query rangeQuery(
     String lowerTerm,
     String upperTerm,
     boolean includeLower,
     boolean includeUpper,
     @Nullable QueryParseContext context) {
   return NumericRangeQuery.newIntRange(
       names.indexName(),
       precisionStep,
       lowerTerm == null ? null : Integer.parseInt(lowerTerm),
       upperTerm == null ? null : Integer.parseInt(upperTerm),
       includeLower,
       includeUpper);
 }
 @Override
 public Query rangeQuery(
     Object lowerTerm,
     Object upperTerm,
     boolean includeLower,
     boolean includeUpper,
     @Nullable QueryParseContext context) {
   return NumericRangeQuery.newIntRange(
       names.indexName(),
       precisionStep,
       lowerTerm == null ? null : parseValue(lowerTerm),
       upperTerm == null ? null : parseValue(upperTerm),
       includeLower,
       includeUpper);
 }
Beispiel #14
0
  @Override
  public Query getRangeQuery(
      QParser parser,
      SchemaField field,
      String min,
      String max,
      boolean minInclusive,
      boolean maxInclusive) {
    int ps = precisionStep;
    Query query = null;
    switch (type) {
      case INTEGER:
        query =
            NumericRangeQuery.newIntRange(
                field.getName(),
                ps,
                min == null ? null : Integer.parseInt(min),
                max == null ? null : Integer.parseInt(max),
                minInclusive,
                maxInclusive);
        break;
      case FLOAT:
        query =
            NumericRangeQuery.newFloatRange(
                field.getName(),
                ps,
                min == null ? null : Float.parseFloat(min),
                max == null ? null : Float.parseFloat(max),
                minInclusive,
                maxInclusive);
        break;
      case LONG:
        query =
            NumericRangeQuery.newLongRange(
                field.getName(),
                ps,
                min == null ? null : Long.parseLong(min),
                max == null ? null : Long.parseLong(max),
                minInclusive,
                maxInclusive);
        break;
      case DOUBLE:
        query =
            NumericRangeQuery.newDoubleRange(
                field.getName(),
                ps,
                min == null ? null : Double.parseDouble(min),
                max == null ? null : Double.parseDouble(max),
                minInclusive,
                maxInclusive);
        break;
      case DATE:
        query =
            NumericRangeQuery.newLongRange(
                field.getName(),
                ps,
                min == null ? null : dateField.parseMath(null, min).getTime(),
                max == null ? null : dateField.parseMath(null, max).getTime(),
                minInclusive,
                maxInclusive);
        break;
      default:
        throw new SolrException(
            SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field");
    }

    return query;
  }
 @Override
 public Query termQuery(Object value, @Nullable QueryParseContext context) {
   int iValue = parseValue(value);
   return NumericRangeQuery.newIntRange(
       names.indexName(), precisionStep, iValue, iValue, true, true);
 }
 @Override
 public Query fieldQuery(String value, @Nullable QueryParseContext context) {
   int iValue = Integer.parseInt(value);
   return NumericRangeQuery.newIntRange(
       names.indexName(), precisionStep, iValue, iValue, true, true);
 }
Beispiel #17
0
  public static <T> List<T> searchDocument(
      Class<T> clazz,
      Page page,
      String[] fields,
      List<String> searchkeyword,
      List<Integer[]> priceList)
      throws Exception {
    if (fields == null || fields.length < 1) {
      return null;
    }
    // 解析分词
    // 获取第一个组合数量

    // 获取索引目录文件
    Directory directory = getDirectory(clazz);
    if (directory == null) {
      return null;
    }
    // 获取读取的索引
    IndexReader indexReader = DirectoryReader.open(directory);
    // 获取索引的查询器
    IndexSearcher indexSearcher = new IndexSearcher(indexReader);

    // 查询指定字段的转换器
    // QueryParser parser = new QueryParser("keywords", analyzer);
    //	String[] str = new String[] { "keywords" };
    QueryParser parser = new MultiFieldQueryParser(fields, analyzer);
    BooleanQuery andQuery = new BooleanQuery();
    // 构建多条and查询
    for (String _sk : searchkeyword) {
      Query query = parser.parse(_sk);
      andQuery.add(query, Occur.MUST);
    }

    // 构建between查询
    if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(priceList)) {
      for (Integer[] d : priceList) {
        Integer l = d[0];
        Integer h = d[1];
        boolean f = false;
        if (h > 0) {
          f = true;
        }
        NumericRangeQuery<Integer> numericQuery =
            NumericRangeQuery.newIntRange("price", l, h, true, f);
        andQuery.add(numericQuery, Occur.MUST);
      }
    }

    // 需要查询的关键字

    // bq.add(query, Occur.SHOULD);
    TopDocs topDocs = null;
    int totalCount = indexSearcher.count(andQuery);
    if (totalCount == 0) {
      return null;
    }
    if (page == null) {
      topDocs = indexSearcher.search(andQuery, totalCount);
    } else {
      // 查询出的结果文档
      int _size = 20;
      if (page != null && page.getPageSize() > 0) {
        _size = page.getPageSize();
      }

      // 总条数
      page.setTotalCount(totalCount);

      int _max = page.getPageIndex() * (page.getPageIndex() - 1);
      if (_max - totalCount >= 0) {
        return null;
      }
      // 先获取上一页的最后一个元素
      ScoreDoc lastscoreDoc = getLastScoreDoc(page.getPageIndex(), _size, andQuery, indexSearcher);
      topDocs = indexSearcher.searchAfter(lastscoreDoc, andQuery, _size);
    }

    // 查询出的结果文档
    ScoreDoc[] hits = topDocs.scoreDocs;

    if (hits == null || hits.length < 1) {
      return null;
    }

    List<T> list = new ArrayList<T>(hits.length);
    for (int i = 0; i < hits.length; i++) {
      Document hitDoc = indexSearcher.doc(hits[i].doc);
      T t = clazz.newInstance();
      for (String fieldName : ClassUtils.getLuceneFields(clazz)) {
        String fieldValue = hitDoc.get(fieldName);
        ClassUtils.setPropertieValue(fieldName, t, fieldValue);
      }
      list.add(t);
    }
    indexReader.close();
    directory.close();

    return list;
  }
  public void testWriteReadMerge() throws IOException {
    // get another codec, other than the default: so we are merging segments across different codecs
    final Codec otherCodec;
    if ("SimpleText".equals(Codec.getDefault().getName())) {
      otherCodec = new Lucene46Codec();
    } else {
      otherCodec = new SimpleTextCodec();
    }
    Directory dir = newDirectory();
    IndexWriterConfig iwConf =
        newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random()));
    iwConf.setMaxBufferedDocs(RandomInts.randomIntBetween(random(), 2, 30));
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwConf.clone());

    final int docCount = atLeast(200);
    final byte[][][] data = new byte[docCount][][];
    for (int i = 0; i < docCount; ++i) {
      final int fieldCount =
          rarely()
              ? RandomInts.randomIntBetween(random(), 1, 500)
              : RandomInts.randomIntBetween(random(), 1, 5);
      data[i] = new byte[fieldCount][];
      for (int j = 0; j < fieldCount; ++j) {
        final int length = rarely() ? random().nextInt(1000) : random().nextInt(10);
        final int max = rarely() ? 256 : 2;
        data[i][j] = randomByteArray(length, max);
      }
    }

    final FieldType type = new FieldType(StringField.TYPE_STORED);
    type.setIndexed(false);
    type.freeze();
    IntField id = new IntField("id", 0, Store.YES);
    for (int i = 0; i < data.length; ++i) {
      Document doc = new Document();
      doc.add(id);
      id.setIntValue(i);
      for (int j = 0; j < data[i].length; ++j) {
        Field f = new Field("bytes" + j, data[i][j], type);
        doc.add(f);
      }
      iw.w.addDocument(doc);
      if (random().nextBoolean() && (i % (data.length / 10) == 0)) {
        iw.w.close();
        // test merging against a non-compressing codec
        if (iwConf.getCodec() == otherCodec) {
          iwConf.setCodec(Codec.getDefault());
        } else {
          iwConf.setCodec(otherCodec);
        }
        iw = new RandomIndexWriter(random(), dir, iwConf.clone());
      }
    }

    for (int i = 0; i < 10; ++i) {
      final int min = random().nextInt(data.length);
      final int max = min + random().nextInt(20);
      iw.deleteDocuments(NumericRangeQuery.newIntRange("id", min, max, true, false));
    }

    iw.forceMerge(2); // force merges with deletions

    iw.commit();

    final DirectoryReader ir = DirectoryReader.open(dir);
    assertTrue(ir.numDocs() > 0);
    int numDocs = 0;
    for (int i = 0; i < ir.maxDoc(); ++i) {
      final Document doc = ir.document(i);
      if (doc == null) {
        continue;
      }
      ++numDocs;
      final int docId = doc.getField("id").numericValue().intValue();
      assertEquals(data[docId].length + 1, doc.getFields().size());
      for (int j = 0; j < data[docId].length; ++j) {
        final byte[] arr = data[docId][j];
        final BytesRef arr2Ref = doc.getBinaryValue("bytes" + j);
        final byte[] arr2 =
            Arrays.copyOfRange(arr2Ref.bytes, arr2Ref.offset, arr2Ref.offset + arr2Ref.length);
        assertArrayEquals(arr, arr2);
      }
    }
    assertTrue(ir.numDocs() <= numDocs);
    ir.close();

    iw.deleteAll();
    iw.commit();
    iw.forceMerge(1);

    iw.close();
    dir.close();
  }