|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| Searcher.java | - | 100% | 100% | 100% |
|
||||||||||||||
| 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 | * $Id: Searcher.java 710170 2008-11-03 21:35:52Z natalia $ | |
| 18 | */ | |
| 19 | ||
| 20 | package org.apache.xindice.core.query.ftsearch; | |
| 21 | ||
| 22 | import org.apache.lucene.analysis.Analyzer; | |
| 23 | import org.apache.lucene.queryParser.ParseException; | |
| 24 | import org.apache.lucene.queryParser.QueryParser; | |
| 25 | import org.apache.lucene.search.IndexSearcher; | |
| 26 | import org.apache.lucene.search.Query; | |
| 27 | import org.apache.lucene.search.TopDocs; | |
| 28 | import org.apache.xindice.core.data.NodeSet; | |
| 29 | import org.w3c.dom.NodeList; | |
| 30 | ||
| 31 | import java.io.IOException; | |
| 32 | ||
| 33 | /** | |
| 34 | * Searcher executes full text queries against list of nodes and returns | |
| 35 | * nodes that match the query in the order of relevance (score) - most relevant | |
| 36 | * results will be return first.<br> | |
| 37 | * <br> | |
| 38 | * Query syntax is the same as syntax of Lucene query, except it does not use | |
| 39 | * field names. | |
| 40 | * | |
| 41 | * @version $Revision: 710170 $, $Date: 2008-11-03 21:35:52 +0000 (Mon, 03 Nov 2008) $ | |
| 42 | */ | |
| 43 | public class Searcher { | |
| 44 | private NodeList nodes; | |
| 45 | private Analyzer analyzer; | |
| 46 | ||
| 47 | /** | |
| 48 | * Builds new Searcher based on list of nodes and analyzer. | |
| 49 | * | |
| 50 | * @param nodes List of nodes to search | |
| 51 | * @param analyzer Analyzer that will be used to tokenize text of the nodes. | |
| 52 | * Choice of analyzer affects query results. | |
| 53 | * @see org.apache.lucene.analysis.Analyzer | |
| 54 | */ | |
| 55 | 54 | public Searcher(NodeList nodes, Analyzer analyzer) { |
| 56 | 54 | this.nodes = nodes; |
| 57 | 54 | this.analyzer = analyzer; |
| 58 | } | |
| 59 | ||
| 60 | /** | |
| 61 | * Executes query against list of nodes and returns matches in the order | |
| 62 | * of relevance (score). | |
| 63 | * | |
| 64 | * @param query Full text query | |
| 65 | * @return NodeSet that contains matching nodes | |
| 66 | * @throws ParseException Query failed to be parsed | |
| 67 | */ | |
| 68 | 54 | public NodeSet search(String query) throws ParseException { |
| 69 | 54 | Query compQuery = new QueryParser("", analyzer).parse(query); |
| 70 | 54 | NodeReader reader = new NodeReader(nodes, analyzer); |
| 71 | 54 | IndexSearcher searcher = new IndexSearcher(reader); |
| 72 | ||
| 73 | 54 | TopDocs docs = null; |
| 74 | 54 | try { |
| 75 | 54 | docs = searcher.search(compQuery, reader.numDocs()); |
| 76 | } catch (IOException e) { | |
| 77 | // this searcher does not use file IO, exception won't happen | |
| 78 | } | |
| 79 | ||
| 80 | 54 | return new ResultSet(docs); |
| 81 | } | |
| 82 | ||
| 83 | private class ResultSet implements NodeSet { | |
| 84 | private TopDocs docs; | |
| 85 | private int count; | |
| 86 | ||
| 87 | 54 | private ResultSet(TopDocs docs) { |
| 88 | 54 | this.docs = docs; |
| 89 | } | |
| 90 | ||
| 91 | 72 | public boolean hasMoreNodes() { |
| 92 | 72 | return count < docs.scoreDocs.length; |
| 93 | } | |
| 94 | ||
| 95 | 18 | public Object getNextNode() { |
| 96 | 18 | return nodes.item(docs.scoreDocs[count++].doc); |
| 97 | } | |
| 98 | } | |
| 99 | } |
|
||||||||||