您的位置:首页 > 大数据 > 人工智能

lucene报错Lock obtain timed out:

2015-07-01 11:57 459 查看
一、索引数值类型的数据。

       在早期的lucene,数值类型的值,如“1900”是作为一个文本来对待的。他就是一个字符串,没有大小,没有范围。在实际使用当中,我们经常需要使用数字作为索引。例如,图书的价格,邮件的收发时间等等。

       在lucene2.9以后提供了这种numeric index的功能。

      可以使用一个 NumericField的Field子类来实现。使用setXXValue的方法可以为Field设置numeric的值。

      doc.add(new NumericField("price").setDoubleValue(19.99));

     这样这个price 的field就可以用于 搜索,sort 了。也可以像textual文本那样精确匹配。

     通用 NumericField可以接受多个同名的field。  

     例如:doc.add(new NumericField("price").setDoubleValue(19.99));

               doc.add(new NumericField("price").setINTValue(9.99));

在获取搜索结果的时候,NumericRangeQuery 和 NumericRangeFilter这个两个类会以 “or” 的形式来对待所有一个同名的field有多个value的情况。但是sort在这种情况下没有定义。

    我们在index时间和日期的时候,就可以使用NumericField了。基本方法是将其转化为数值。

    doc.add(new NumericField("timestamp") .setLongValue(new Date().getTime()));

 

  Calendar cal = Calendar.getInstance();

    cal.setTime(date);

    doc.add(new NumericField("dayOfMonth") .setIntValue(cal.get(Calendar.DAY_OF_MONTH)));

二、域截断

    在我们实例化一个IndexWriter的时候,会传入一个参数   IndexWriter.MaxFieldLength.UNLIMITED

 new IndexWriter(dir,new StandardAnalyzer(Version.LUCENE_36),IndexWriter.MaxFieldLength.UNLIMITED);

     这个参数就是用户域截断的。当我们在索引一个Field的value的时候,我们事先不知道这个value有多长,也许很长,

或者这个value是非textual的 很长的 binary content。这就会造成问题。我们可以使用这个参数控制index的数量。

   也可使用indexWriter的setMaxFieldLength(maxFieldLength)

writer.setMaxFieldLength(maxFieldLength)

这个maxFieldLength的意思是index的最多多少个term:

The maximum number of terms that will be indexed for a single field in a document

 

如果刚开始的indexWriter的没有截断,在后面设置了设置截断长度,不影响前面的index。

 ! 慎用这个feature,因为可能造成索引不全,影响体验。

三、近似实时搜索

      当我们频繁通过一个writer来修改一个index。现在我们想要search实时的内容时候就可以调用getReader方法。来获得一个只读的IndexReader。它会将这个writer的commit或者uncommit的change添加到index中,并返回一个read-only indexReader。

      IndexReader reader = writer.getReader();

记住使用玩这个reader后记住关闭它。

这是lucene对它的解释。

"near real-time" searching, in that changes made during an IndexWriter session can be quickly made available for searching without closing the writer nor calling commit().Note that this is functionally equivalent to calling {#flush} and then using IndexReader.open(org.apache.lucene.store.Directory)
to open a new reader. But the turarnound time of this method should be faster since it avoids the potentially costly commit().You must close the IndexReader returned by this method once you are done using it.

四、优化index

优化原因:频繁更新indexWriter,会生成很多的segment(每个segment会有多个文件)。lucene在search的时候会search每一个segment然后合并结果,造成速度下降。而且太多的segment会消耗 更多的文件描述符。所以需要优化。

优化的目的就是将散落的多个segmet合并为N个,提高search的速度。

indexWriter提供了4个方法用于优化。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java lucene 索引