提问者:小点点

lucene.net3.0.3索引空间太慢


我最近将我的搜索代码从lucene.net2.9.4升级到3.0.3。我注意到空间包发生了变化,并相应地更新了我的代码。我注意到升级的一个缺点是索引时间慢得多。通过消除过程,我已经能够将速度缩小到索引拉特/长坐标的新空间代码:

        public void AddLocation (double lat, double lng)
    {
        try
        {
            string latLongKey = lat.ToString() + "," + lng.ToString();
            AbstractField[] shapeFields = null;
            Shape shape = null;
            if (HasSpatialShapes(latLongKey))
            {
                shape = SpatialShapes[latLongKey];
            }
            else
            {
                if (this.Strategy is BBoxStrategy)
                {
                    shape = Context.MakeRectangle(DistanceUtils.NormLonDEG(lng), DistanceUtils.NormLonDEG(lng), DistanceUtils.NormLatDEG(lat), DistanceUtils.NormLatDEG(lat));
                }
                else
                {
                    shape = Context.MakePoint(DistanceUtils.NormLonDEG(lng), DistanceUtils.NormLatDEG(lat));
                }

                AddSpatialShapes(latLongKey, shape);
            }

            shapeFields = Strategy.CreateIndexableFields(shape);
            //Potentially more than one shape in this field is supported by some
            // strategies; see the javadocs of the SpatialStrategy impl to see.
            foreach (AbstractField f in shapeFields)
            {
                _document.Add(f);
            }
            //add lat long values to index too
            _document.Add(GetField("latitude", NumericUtils.DoubleToPrefixCoded(lat), Field.Index.NOT_ANALYZED, Field.Store.YES, 0f, false));
            _document.Add(GetField("longitude", NumericUtils.DoubleToPrefixCoded(lng), Field.Index.NOT_ANALYZED, Field.Store.YES, 0f, false));
        }
        catch (Exception e)
        {
            RollingFileLogger.Instance.LogException(ServiceConstants.SERVICE_INDEXER_CONST, "Document",string.Format("AddLocation({0},{1})", lat.ToString(), lng.ToString()), e, null);
            throw e;
        }
    }

使用2.9.4,我能够在大约11分钟内索引大约300,000行具有lat/lng点的数据。使用这个新的空间包,它需要超过5个小时(我在测试完成之前就终止了测试,所以我没有确切的时间安排)。这是我使用的空间上下文/策略:

   public static SpatialContext SpatialContext
   {
       get
       {
           if (null == _spatialContext)
           {
               lock (_lockObject)
               {
                   if(null==_spatialContext) _spatialContext = SpatialContext.GEO;
               }
           }
           return _spatialContext;
       }
   }

   public static SpatialStrategy SpatialStrategy
   {
       get
       {
           if (null == _spatialStrategy)
           {
               lock (_lockObject)
               {
                   if (null == _spatialStrategy)
                   {
                       int maxLength = 9;
                       GeohashPrefixTree geohashPrefixTree = new GeohashPrefixTree(SpatialContext, maxLength);
                       _spatialStrategy = new RecursivePrefixTreeStrategy(geohashPrefixTree, "geoField");                           
                   }
               }
           }
           return _spatialStrategy;
       }
   }

我的索引方法有什么问题吗?我已经缓存了由lat/lng点创建的形状,因为我不需要相同坐标的新形状。似乎是CreateIndexableFields()方法在索引期间花费了最多的时间。我尝试缓存此方法生成的字段以重用,但我无法从缓存的字段中创建TokenStream的新实例以在新Document中使用(在lucene.net3.0.3中,TokenStream的构造函数受到保护)。我在空间策略中将maxLevels int降低到4,但我没有看到索引时间的改善。任何反馈都将不胜感激。


共1个答案

匿名用户

更新:我将空间策略更改为PointVectorStrategy,现在我的索引时间减少到11分钟,大约有300,000个文档。关键是缓存添加到文档时使用的形状创建的IndexableFields。PointVectorStrategy允许这样做,因为它创建了用于索引的NumericFields。这对于RecursiveTreePrefix Strategy是不可能的,因为它创建了带有TokenStreams的字段用于索引。在3.0.3Lucene.net中,TokenStreams不可重复用于索引。感谢大家的帮助。