mirror of https://github.com/apache/lucene.git
code cleanup. tests pass
git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150937 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
48798905d1
commit
5320408426
|
@ -1,32 +1,20 @@
|
|||
package org.apache.lucene.beans;
|
||||
|
||||
import org.apache.lucene.document.Document;
|
||||
import org.apache.lucene.document.DateField;
|
||||
import org.apache.lucene.beans.IndividualHit;
|
||||
import java.util.Date;
|
||||
|
||||
//import org.apache.log4j.Logger;
|
||||
|
||||
public class CompareDocumentsByField implements java.util.Comparator
|
||||
{
|
||||
// static private Logger logger = Logger.getLogger(CompareDocumentsByDate.class.getName());
|
||||
public CompareDocumentsByField()
|
||||
{
|
||||
}
|
||||
|
||||
public int compare(Object hit1, Object hit2)
|
||||
{
|
||||
String myDate1 = ((IndividualHit)hit1).getField();
|
||||
String myDate2 = ((IndividualHit)hit2).getField();
|
||||
if ((myDate1 == null) || (myDate2 == null)){
|
||||
//logger.error("A date was null, the score is "+((IndividualHit) hit1).getScore());
|
||||
//return -1;
|
||||
}
|
||||
return -1*(myDate1.compareTo(myDate2)); //sort in descending order
|
||||
}
|
||||
|
||||
public boolean equals(Object o1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
public class CompareDocumentsByField implements java.util.Comparator {
|
||||
public int compare(Object hit1, Object hit2) {
|
||||
String myDate1 = ((IndividualHit) hit1).getField();
|
||||
String myDate2 = ((IndividualHit) hit2).getField();
|
||||
if ((myDate1 == null) || (myDate2 == null)) {
|
||||
//logger.error("A date was null, the score is "+((IndividualHit) hit1).getScore());
|
||||
//return -1;
|
||||
}
|
||||
return -1 * (myDate1.compareTo(myDate2)); //sort in descending order
|
||||
}
|
||||
|
||||
public boolean equals(Object o1) {
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -10,16 +10,12 @@ import org.apache.lucene.search.Searcher;
|
|||
import org.apache.lucene.search.IndexSearcher;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.apache.lucene.search.Hits;
|
||||
import org.apache.lucene.search.HitCollector;
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.analysis.Analyzer;
|
||||
import org.apache.lucene.analysis.StopAnalyzer;
|
||||
import org.apache.lucene.analysis.standard.StandardAnalyzer;
|
||||
import org.apache.lucene.queryParser.QueryParser;
|
||||
import org.apache.lucene.queryParser.ParseException;
|
||||
|
||||
import java.util.Vector;
|
||||
|
||||
import org.apache.lucene.beans.HitsIterator;
|
||||
|
||||
import java.io.IOException;
|
||||
|
@ -45,11 +41,7 @@ public class SearchBean extends Object {
|
|||
// static Logger logger = Logger.getLogger(SearchBean.class.getName());
|
||||
// static Logger searchLogger = Logger.getLogger("searchLog");
|
||||
|
||||
private SearchBean(){
|
||||
}
|
||||
|
||||
/** Creates new SearchBean
|
||||
* @param path to index
|
||||
*/
|
||||
public SearchBean(Directory directory) {
|
||||
this.directory = directory;
|
||||
|
@ -161,20 +153,16 @@ public class SearchBean extends Object {
|
|||
return queryString+"|"+queryType+"|"+querySortField;
|
||||
}
|
||||
|
||||
/** setter for queryString
|
||||
*/
|
||||
public void setQueryString
|
||||
(String queryString) {
|
||||
public void setQueryString(String queryString) {
|
||||
this.queryString = queryString;
|
||||
}
|
||||
|
||||
/** getter for queryString
|
||||
*/
|
||||
public String getQueryString(){
|
||||
return queryString;
|
||||
}
|
||||
|
||||
/** getter for Lucene Query
|
||||
/** Parses the query
|
||||
* @todo allow for user defined analyzer
|
||||
*/
|
||||
private Query getQuery(String queryString, String searchField) throws ParseException {
|
||||
//String defaultSearchField = "text";
|
||||
|
@ -184,87 +172,51 @@ public class SearchBean extends Object {
|
|||
return query;
|
||||
}
|
||||
|
||||
/** Getter for property defaulSearchField.
|
||||
* @return Value of property defaulSearchField.
|
||||
*/
|
||||
public String getDefaultSearchField() {
|
||||
return defaultSearchField;
|
||||
}
|
||||
|
||||
/** Setter for property defaulSearchField.
|
||||
* @param defaulSearchField New value of property defaulSearchField.
|
||||
*/
|
||||
public void setDefaultSearchField(java.lang.String defaultSearchField) {
|
||||
public void setDefaultSearchField(String defaultSearchField) {
|
||||
this.defaultSearchField = defaultSearchField;
|
||||
}
|
||||
|
||||
/** Getter for property searchTime.
|
||||
* @return Value of property searchTime.
|
||||
*/
|
||||
public long getSearchTime() {
|
||||
return searchTime;
|
||||
}
|
||||
|
||||
/** Setter for property searchTime.
|
||||
* @param searchTime New value of property searchTime.
|
||||
*/
|
||||
public void setSearchTime(long searchTime) {
|
||||
this.searchTime = searchTime;
|
||||
}
|
||||
|
||||
/** Getter for property querySortField.
|
||||
* @return Value of property querySortField.
|
||||
*/
|
||||
public java.lang.String getQuerySortField() {
|
||||
return querySortField;
|
||||
}
|
||||
|
||||
/** Setter for property querySortField.
|
||||
* @param querySortField New value of property querySortField.
|
||||
*/
|
||||
public void setQuerySortField(String querySortField) {
|
||||
this.querySortField = querySortField;
|
||||
}
|
||||
|
||||
/** Getter for property hitsIterator.
|
||||
* @return Value of property hitsIterator.
|
||||
*/
|
||||
public HitsIterator getHitsIterator() {
|
||||
return hitsIterator;
|
||||
}
|
||||
|
||||
/** Setter for property hitsIterator.
|
||||
* @param hitsIterator New value of property hitsIterator.
|
||||
*/
|
||||
public void setHitsIterator(HitsIterator hitsIterator) {
|
||||
this.hitsIterator = hitsIterator;
|
||||
}
|
||||
|
||||
/** Getter for property queryType.
|
||||
* @return Value of property queryType.
|
||||
*/
|
||||
public java.lang.String getQueryType() {
|
||||
public String getQueryType() {
|
||||
return queryType;
|
||||
}
|
||||
|
||||
/** Setter for property queryType.
|
||||
* @param queryType New value of property queryType.
|
||||
*/
|
||||
public void setQueryType(java.lang.String queryType) {
|
||||
public void setQueryType(String queryType) {
|
||||
this.queryType = queryType;
|
||||
}
|
||||
|
||||
/** Getter for property directory.
|
||||
* @return Value of property directory.
|
||||
*/
|
||||
public org.apache.lucene.store.Directory getDirectory() {
|
||||
public Directory getDirectory() {
|
||||
return directory;
|
||||
}
|
||||
|
||||
/** Setter for property directory.
|
||||
* @param directory New value of property directory.
|
||||
*/
|
||||
public void setDirectory(org.apache.lucene.store.Directory directory) {
|
||||
public void setDirectory(Directory directory) {
|
||||
this.directory = directory;
|
||||
}
|
||||
|
||||
|
|
|
@ -20,17 +20,18 @@ import java.io.IOException;
|
|||
* @author carlson
|
||||
*/
|
||||
public class SortedField {
|
||||
|
||||
|
||||
private String fieldName;
|
||||
|
||||
|
||||
private String[] fieldValues;
|
||||
|
||||
|
||||
private static Hashtable fieldList = new Hashtable(); //keeps track of all fields
|
||||
|
||||
|
||||
/** Creates a new instance of SortedField */
|
||||
public SortedField() {
|
||||
private SortedField(String fieldName) {
|
||||
this.fieldName = fieldName;
|
||||
}
|
||||
|
||||
|
||||
/** add a field so that is can be used to sort
|
||||
* @param fieldName the name of the field to add
|
||||
* @param indexPath path to Lucene index directory
|
||||
|
@ -39,7 +40,7 @@ public class SortedField {
|
|||
IndexReader ir = IndexReader.open(indexPath);
|
||||
addField(fieldName, ir);
|
||||
}
|
||||
|
||||
|
||||
/** add a field so that is can be used to sort
|
||||
* @param fieldName the name of the field to add
|
||||
* @param indexFile File pointing to Lucene index directory
|
||||
|
@ -48,8 +49,8 @@ public class SortedField {
|
|||
IndexReader ir = IndexReader.open(indexFile);
|
||||
addField(fieldName, ir);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/** add a field so that is can be used to sort
|
||||
* @param fieldName the name of the field to add
|
||||
* @param directory Lucene Directory
|
||||
|
@ -58,15 +59,15 @@ public class SortedField {
|
|||
IndexReader ir = IndexReader.open(directory);
|
||||
addField(fieldName, ir);
|
||||
}
|
||||
|
||||
|
||||
private static void addField(String fieldName, IndexReader ir) throws IOException{
|
||||
SortedField sortedField = new SortedField();
|
||||
SortedField sortedField = new SortedField(fieldName);
|
||||
sortedField.addSortedField(fieldName,ir);
|
||||
//long start = System.currentTimeMillis();
|
||||
fieldList.put(fieldName, sortedField);
|
||||
//logger.info("adding data from field "+fieldName+" took "+(System.currentTimeMillis()-start));
|
||||
}
|
||||
|
||||
|
||||
/** adds the data from the index into a string array
|
||||
*/
|
||||
private void addSortedField(String fieldName, IndexReader ir) throws IOException{
|
||||
|
@ -81,7 +82,7 @@ public class SortedField {
|
|||
}
|
||||
ir.close();
|
||||
}
|
||||
|
||||
|
||||
/** returns the value of the field
|
||||
* @param globalID Lucene's global document ID
|
||||
* @return value of field
|
||||
|
@ -89,7 +90,7 @@ public class SortedField {
|
|||
public String getFieldValue(int globalID) {
|
||||
return fieldValues[globalID];
|
||||
}
|
||||
|
||||
|
||||
/** provides way to retrieve a SortedField once you add it
|
||||
* @param fieldName name of field to lookup
|
||||
* @return SortedField field to use when sorting
|
||||
|
@ -97,7 +98,7 @@ public class SortedField {
|
|||
public static SortedField getSortedField(String fieldName){
|
||||
return (SortedField) fieldList.get(fieldName);
|
||||
}
|
||||
|
||||
|
||||
/** Getter for property fieldName.
|
||||
* @return Value of property fieldName.
|
||||
*/
|
||||
|
|
|
@ -54,8 +54,6 @@ package org.apache.lucene;
|
|||
* <http://www.apache.org/>.
|
||||
*/
|
||||
|
||||
import org.apache.lucene.search.IndexSearcher;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.index.IndexWriter;
|
||||
import org.apache.lucene.store.RAMDirectory;
|
||||
import org.apache.lucene.store.Directory;
|
||||
|
@ -64,7 +62,6 @@ import org.apache.lucene.document.Document;
|
|||
import org.apache.lucene.document.Field;
|
||||
import org.apache.lucene.index.IndexReader;
|
||||
import org.apache.lucene.queryParser.ParseException;
|
||||
import org.apache.lucene.queryParser.QueryParser;
|
||||
|
||||
import org.apache.lucene.beans.SearchBean;
|
||||
import org.apache.lucene.beans.HitsIterator;
|
||||
|
@ -74,53 +71,43 @@ import junit.framework.TestCase;
|
|||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class TestSearchBean extends TestCase{
|
||||
public TestSearchBean(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
public void testSearchBean() throws IOException, ParseException {
|
||||
Directory indexStore = createIndex();
|
||||
SortedField.addField("text",indexStore);
|
||||
//IndexSearcher searcher = new IndexSearcher(indexStore);
|
||||
|
||||
|
||||
SearchBean sb = new SearchBean(indexStore);
|
||||
HitsIterator hi = sb.search("metal");
|
||||
|
||||
|
||||
assertEquals(1, hi.getTotalHits());
|
||||
|
||||
|
||||
assertEquals(1, hi.getPageCount());
|
||||
|
||||
|
||||
assertEquals("metal",hi.next().get("text"));
|
||||
}
|
||||
|
||||
|
||||
public void testUnoptimizedSearchBean() throws IOException, ParseException {
|
||||
Directory indexStore = createIndex();
|
||||
IndexReader reader = IndexReader.open(indexStore);
|
||||
reader.delete(0);
|
||||
//
|
||||
reader.close();
|
||||
|
||||
|
||||
SortedField.addField("text",indexStore);
|
||||
//IndexSearcher searcher = new IndexSearcher(indexStore);
|
||||
|
||||
|
||||
SearchBean sb = new SearchBean(indexStore);
|
||||
HitsIterator hi = sb.search("metal");
|
||||
|
||||
|
||||
assertEquals(0, hi.getTotalHits());
|
||||
|
||||
|
||||
assertEquals(0, hi.getPageCount());
|
||||
|
||||
|
||||
//assertEquals("metal",hi.next().get("text"));
|
||||
}
|
||||
|
||||
|
||||
public Directory createIndex() throws IOException{
|
||||
RAMDirectory indexStore = new RAMDirectory();
|
||||
IndexWriter writer = new IndexWriter(indexStore, new StandardAnalyzer(), true);
|
||||
|
@ -132,7 +119,7 @@ public class TestSearchBean extends TestCase{
|
|||
writer.addDocument(doc2);
|
||||
writer.optimize();
|
||||
writer.close();
|
||||
return (Directory) indexStore;
|
||||
return indexStore;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue