1 17 package org.alfresco.repo.search.impl.lucene.query; 18 19 import java.io.IOException ; 20 import java.util.HashMap ; 21 22 import org.alfresco.repo.search.impl.lucene.query.LeafScorer.Counter; 23 import org.alfresco.service.cmr.dictionary.DictionaryService; 24 import org.apache.lucene.document.Document; 25 import org.apache.lucene.document.Field; 26 import org.apache.lucene.index.IndexReader; 27 import org.apache.lucene.index.Term; 28 import org.apache.lucene.index.TermPositions; 29 import org.apache.lucene.search.Explanation; 30 import org.apache.lucene.search.Scorer; 31 import org.apache.lucene.search.Similarity; 32 import org.apache.lucene.search.Weight; 33 34 public class PathScorer extends Scorer 35 { 36 Scorer scorer; 37 38 PathScorer(Similarity similarity, Scorer scorer) 39 { 40 super(similarity); 41 this.scorer = scorer; 42 } 43 44 45 public static PathScorer createPathScorer(Similarity similarity, PathQuery pathQuery, IndexReader reader, Weight weight, DictionaryService dictionarySertvice, boolean repeat) throws IOException 46 { 47 Scorer selfScorer = null; 48 HashMap <String , Counter> selfIds = null; 49 50 StructuredFieldPosition last = null; 51 if(pathQuery.getQNameStructuredFieldPositions().size() > 0) 52 { 53 last = pathQuery.getQNameStructuredFieldPositions().get(pathQuery.getQNameStructuredFieldPositions().size() - 1); 54 } 55 if ((last != null) && last.linkSelf()) 56 { 57 PathQuery selfQuery = new PathQuery(dictionarySertvice); 58 selfQuery.setQuery(pathQuery.getPathStructuredFieldPositions(), pathQuery.getQNameStructuredFieldPositions()); 59 selfQuery.removeDescendantAndSelf(); 60 if (!selfQuery.isEmpty()) 61 { 62 selfIds = new HashMap <String , Counter>(); 63 selfScorer = PathScorer.createPathScorer(similarity, selfQuery, reader, weight, dictionarySertvice, repeat); 64 selfIds.clear(); 65 while (selfScorer.next()) 66 { 67 int doc = selfScorer.doc(); 68 Document document = reader.document(doc); 69 Field id = document.getField("ID"); 70 Counter counter = selfIds.get(id.stringValue()); 71 if (counter == null) 72 { 73 counter = new Counter(); 74 selfIds.put(id.stringValue(), counter); 75 } 76 counter.count++; 77 } 78 } 79 } 80 81 82 if ((pathQuery.getPathStructuredFieldPositions().size() + pathQuery.getQNameStructuredFieldPositions().size()) == 0) return null; 86 87 88 for (StructuredFieldPosition sfp : pathQuery.getPathStructuredFieldPositions()) 89 { 90 if (sfp.getTermText() != null) 91 { 92 TermPositions p = reader.termPositions(new Term(pathQuery.getPathField(), sfp.getTermText())); 93 if (p == null) 94 return null; 95 CachingTermPositions ctp = new CachingTermPositions(p); 96 sfp.setCachingTermPositions(ctp); 97 } 98 } 99 100 for (StructuredFieldPosition sfp : pathQuery.getQNameStructuredFieldPositions()) 101 { 102 if (sfp.getTermText() != null) 103 { 104 TermPositions p = reader.termPositions(new Term(pathQuery.getQnameField(), sfp.getTermText())); 105 if (p == null) 106 return null; 107 CachingTermPositions ctp = new CachingTermPositions(p); 108 sfp.setCachingTermPositions(ctp); 109 } 110 } 111 112 TermPositions rootContainerPositions = null; 113 if (pathQuery.getPathRootTerm() != null) 114 { 115 rootContainerPositions = reader.termPositions(pathQuery.getPathRootTerm()); 116 } 117 118 TermPositions rootLeafPositions = null; 119 if (pathQuery.getQNameRootTerm() != null) 120 { 121 rootLeafPositions = reader.termPositions(pathQuery.getQNameRootTerm()); 122 } 123 124 125 TermPositions tp = reader.termPositions(); 126 127 ContainerScorer cs = null; 128 129 TermPositions level0 = null; 130 131 TermPositions nodePositions = reader.termPositions(new Term("ISNODE", "T")); 132 133 if (pathQuery.getPathStructuredFieldPositions().size() > 0) 137 { 138 TermPositions containerPositions = reader.termPositions(new Term("ISCONTAINER", "T")); 139 cs = new ContainerScorer(weight, rootContainerPositions, (StructuredFieldPosition[]) pathQuery.getPathStructuredFieldPositions().toArray(new StructuredFieldPosition[] {}), 140 containerPositions, similarity, reader.norms(pathQuery.getPathField())); 141 } 142 else 143 { 144 level0 = reader.termPositions(new Term("ISROOT", "T")); 145 } 146 147 LeafScorer ls = new LeafScorer(weight, rootLeafPositions, level0, cs, (StructuredFieldPosition[]) pathQuery.getQNameStructuredFieldPositions().toArray(new StructuredFieldPosition[] {}), nodePositions, 148 selfIds, reader, similarity, reader.norms(pathQuery.getQnameField()), dictionarySertvice, repeat, tp); 149 150 return new PathScorer(similarity, ls); 151 } 152 153 @Override 154 public boolean next() throws IOException 155 { 156 return scorer.next(); 157 } 158 159 @Override 160 public int doc() 161 { 162 return scorer.doc(); 163 } 164 165 @Override 166 public float score() throws IOException 167 { 168 return scorer.score(); 169 } 170 171 @Override 172 public boolean skipTo(int position) throws IOException 173 { 174 return scorer.skipTo(position); 175 } 176 177 @Override 178 public Explanation explain(int position) throws IOException 179 { 180 return scorer.explain(position); 181 } 182 183 } 184 | Popular Tags |