Lucene5学习之NumericRangeQuery使用

    说到NumericRangeQuery查询,你们肯定觉得很简单,不就是数字范围查询吗?用户提供一个上限值和一个下限值,底层API里直接>min,<max,真的是这样吗?其实在Lucene里只能对字符串String建立索引,那么数字怎么转成String,你肯定又会想当然的认为toString()一下就OK啦?OK,假如真的是这样的,那字符串"3" > "26"问题怎么解决?OK,可以通过在数字前面加前导零解决,“03”<"26"是没错,可是前导零加几位没法确定,加多了浪费硬盘空间,加少了支持索引的数字位数受限。即使你解决了位数受限问题,但Lucene里的范围查询本质还是通过BooleanQuery进行条件连接起来的,term条件太多还是会出现too many boolean Clause异常的。其实Lucene内部是把数字(int,long,float,double)转成十六进制的数字来处理的。具体怎么转成的请参看NumericUtils这个工具类的源码,

 

/**
   * Converts a <code>float</code> value to a sortable signed <code>int</code>.
   * The value is converted by getting their IEEE 754 floating-point "float format"
   * bit layout and then some bits are swapped, to be able to compare the result as int.
   * By this the precision is not reduced, but the value can easily used as an int.
   * @see #sortableIntToFloat
   */
  public static int floatToSortableInt(float val) {
    int f = Float.floatToRawIntBits(val);
    if (f<0) f ^= 0x7ff
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Lucene是一个开源的全文搜索引擎库,用于实现文本索引和搜索功能。下面是一个Lucene使用示例: 1. 创建索引: ```java import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; // 创建索引 public class Indexer { public static void main(String[] args) { // 索引存储路径 String indexPath = "path_to_index_directory"; try { // 创建分词器 Analyzer analyzer = new StandardAnalyzer(); // 创建索引配置 IndexWriterConfig config = new IndexWriterConfig(analyzer); // 创建索引目录 Directory directory = FSDirectory.open(Paths.get(indexPath)); // 创建索引写入器 IndexWriter indexWriter = new IndexWriter(directory, config); // 创建文档 Document document = new Document(); document.add(new Field("content", "Hello world!", TextField.TYPE_STORED)); // 将文档添加到索引中 indexWriter.addDocument(document); // 提交索引 indexWriter.commit(); // 关闭索引写入器 indexWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 2. 搜索索引: ```java import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexReader; import org.apache.lucene.queryparser.classic.QueryParser; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TopDocs; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; // 搜索索引 public class Searcher { public static void main(String[] args) { // 索引目录路径 String indexPath = "path_to_index_directory"; try { // 创建分词器 Analyzer analyzer = new StandardAnalyzer(); // 创建索引目录 Directory directory = FSDirectory.open(Paths.get(indexPath)); // 创建索引读取器 IndexReader indexReader = DirectoryReader.open(directory); // 创建索引搜索器 IndexSearcher indexSearcher = new IndexSearcher(indexReader); // 创建查询解析器 QueryParser parser = new QueryParser("content", analyzer); // 创建查询 Query query = parser.parse("Hello"); // 执行查询,获取前n个结果 TopDocs topDocs = indexSearcher.search(query, 10); ScoreDoc[] scoreDocs = topDocs.scoreDocs; // 遍历结果 for (ScoreDoc scoreDoc : scoreDocs) { int docId = scoreDoc.doc; Document document = indexSearcher.doc(docId); System.out.println("Content: " + document.get("content")); } // 关闭索引读取器 indexReader.close(); } catch (IOException | ParseException e) { e.printStackTrace(); } } } ``` 以上示例演示了如何使用Lucene创建索引并进行搜索。在创建索引时,需要定义分词器、索引配置、文档字段等。在搜索索引时,需要创建查询解析器、执行查询并获取结果。你可以根据自己的需求进行更多的定制和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值