Lucene.Net  3.0.3
Lucene.Net is a .NET port of the Java Lucene Indexing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties
BBoxSimilarityValueSource.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 Lucene.Net.Index;
19 using Lucene.Net.Search;
20 using Lucene.Net.Search.Function;
21 using Lucene.Net.Spatial.Util;
22 using Spatial4n.Core.Shapes;
23 using Spatial4n.Core.Shapes.Impl;
24 
25 namespace Lucene.Net.Spatial.BBox
26 {
28  {
29  private readonly BBoxStrategy strategy;
30  private readonly BBoxSimilarity similarity;
31 
33  {
34  this.strategy = strategy;
35  this.similarity = similarity;
36  }
37 
38  private class BBoxSimilarityValueSourceDocValues : DocValues
39  {
40  private readonly BBoxSimilarityValueSource _enclosingInstance;
41  private readonly Rectangle rect;
42  private readonly double[] minX;
43  private readonly double[] minY;
44  private readonly double[] maxX;
45  private readonly double[] maxY;
46 
47  private readonly IBits validMinX, validMaxX;
48 
49  public BBoxSimilarityValueSourceDocValues(IndexReader reader, BBoxSimilarityValueSource enclosingInstance)
50  {
51  _enclosingInstance = enclosingInstance;
52  rect = _enclosingInstance.strategy.GetSpatialContext().MakeRectangle(0, 0, 0, 0); //reused
53 
54  minX = FieldCache_Fields.DEFAULT.GetDoubles(reader, enclosingInstance.strategy.field_minX/*, true*/);
55  minY = FieldCache_Fields.DEFAULT.GetDoubles(reader, enclosingInstance.strategy.field_minY/*, true*/);
56  maxX = FieldCache_Fields.DEFAULT.GetDoubles(reader, enclosingInstance.strategy.field_maxX/*, true*/);
57  maxY = FieldCache_Fields.DEFAULT.GetDoubles(reader, enclosingInstance.strategy.field_maxY/*, true*/);
58 
59  validMinX = FieldCache_Fields.DEFAULT.GetDocsWithField(reader, enclosingInstance.strategy.field_minX);
60  validMaxX = FieldCache_Fields.DEFAULT.GetDocsWithField(reader, enclosingInstance.strategy.field_maxX);
61  }
62 
63  public override float FloatVal(int doc)
64  {
65  // make sure it has minX and area
66  if (validMinX.Get(doc) && validMaxX.Get(doc))
67  {
68  rect.Reset(
69  minX[doc], maxX[doc],
70  minY[doc], maxY[doc]);
71  return (float) _enclosingInstance.similarity.Score(rect, null);
72  }
73  else
74  {
75  return (float) _enclosingInstance.similarity.Score(null, null);
76  }
77  }
78 
79  public override Explanation Explain(int doc)
80  {
81  // make sure it has minX and area
82  if (validMinX.Get(doc) && validMaxX.Get(doc))
83  {
84  rect.Reset(
85  minX[doc], maxX[doc],
86  minY[doc], maxY[doc]);
87  var exp = new Explanation();
88  _enclosingInstance.similarity.Score(rect, exp);
89  return exp;
90  }
91  return new Explanation(0, "No BBox");
92  }
93 
94  public override string ToString(int doc)
95  {
96  return _enclosingInstance.Description() + "=" + FloatVal(doc);
97  }
98  }
99 
100  public override DocValues GetValues(IndexReader reader)
101  {
102  return new BBoxSimilarityValueSourceDocValues(reader, this);
103  }
104 
105  public override string Description()
106  {
107  return "BBoxSimilarityValueSource(" + similarity + ")";
108  }
109 
110  public override bool Equals(object o)
111  {
112  var other = o as BBoxSimilarityValueSource;
113  if (other == null) return false;
114  return similarity.Equals(other.similarity);
115  }
116 
117  public override int GetHashCode()
118  {
119  return typeof(BBoxSimilarityValueSource).GetHashCode() + similarity.GetHashCode();
120  }
121  }
122 }