Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
ShapeFieldCacheDistanceValueSource.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 using System;
19 using Lucene.Net.Index;
20 using Lucene.Net.Search.Function;
21 using Spatial4n.Core.Context;
22 using Spatial4n.Core.Distance;
23 using Spatial4n.Core.Shapes;
24 
25 namespace Lucene.Net.Spatial.Util
26 {
27  /// <summary>
28  /// An implementation of the Lucene ValueSource model to support spatial relevance ranking.
29  /// </summary>
31  {
32  private readonly ShapeFieldCacheProvider<Point> provider;
33  private readonly SpatialContext ctx;
34  private readonly Point from;
35 
36  public ShapeFieldCacheDistanceValueSource(SpatialContext ctx, ShapeFieldCacheProvider<Point> provider, Point from)
37  {
38  this.ctx = ctx;
39  this.from = from;
40  this.provider = provider;
41  }
42 
44  {
45  private readonly ShapeFieldCacheDistanceValueSource enclosingInstance;
46  private readonly ShapeFieldCache<Point> cache;
47  private readonly Point from;
48  private readonly DistanceCalculator calculator;
49  private readonly double nullValue;
50 
52  {
53  cache = enclosingInstance.provider.GetCache(reader);
54  this.enclosingInstance = enclosingInstance;
55 
56  from = enclosingInstance.from;
57  calculator = enclosingInstance.ctx.GetDistCalc();
58  nullValue = (enclosingInstance.ctx.IsGeo() ? 180 : double.MaxValue);
59  }
60 
61  public override float FloatVal(int doc)
62  {
63  return (float)DoubleVal(doc);
64  }
65 
66  public override double DoubleVal(int doc)
67  {
68  var vals = cache.GetShapes(doc);
69  if (vals != null)
70  {
71  double v = calculator.Distance(from, vals[0]);
72  for (int i = 1; i < vals.Count; i++)
73  {
74  v = Math.Min(v, calculator.Distance(from, vals[i]));
75  }
76  return v;
77  }
78  return nullValue;
79  }
80 
81  public override string ToString(int doc)
82  {
83  return enclosingInstance.Description() + "=" + FloatVal(doc);
84  }
85  }
86 
87  public override DocValues GetValues(IndexReader reader)
88  {
89  return new CachedDistanceDocValues(reader, this);
90  }
91 
92  public override string Description()
93  {
94  return GetType().Name + "(" + provider + ", " + from + ")";
95  }
96 
97  public override bool Equals(object o)
98  {
99  if (this == o) return true;
100 
101  var that = o as ShapeFieldCacheDistanceValueSource;
102 
103  if (that == null) return false;
104  if (!ctx.Equals(that.ctx)) return false;
105  if (!from.Equals(that.from)) return false;
106  if (!provider.Equals(that.provider)) return false;
107 
108  return true;
109  }
110 
111  public override int GetHashCode()
112  {
113  return from.GetHashCode();
114  }
115  }
116 }