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