lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Allison, Timothy B." <>
Subject RE: Wild card search not working
Date Mon, 30 Nov 2015 12:52:07 GMT
If you want to find the matching terms, you have to do something like this:

            Query rewritten = spanTerm.rewrite(indexReader);
            Weight w = rewritten.createWeight(isearcher, false);
            Set<Term> terms = new HashSet<>();
            for (Term t : terms) {
                System.out.println("found term: " + t);

Be careful to set the rewriteMethod on MultiTerm queries to something that won't truncate
results (if that's your desired behavior)....or that will truncate results to a number that
you're aware of. :)

-----Original Message-----
From: Douglas Kunzma [] 
Sent: Monday, November 30, 2015 6:59 AM
Subject: Wild card search not working

Hi -

I've created a test program where I've been struggling with for  a couple of days trying to
get wild card searches working in Lucene.  I have some Solr experience but this is the first
time that I'm working with Lucene.
I've copied the entire program to github here:

luceneseach2 <>/
Please let me know what I'm doing wrong.  I'm using Lucene 5.3.1

I also paste the small program below.  It is self contained.

I've checked all of the usual stuff like using a TextField.

Thanks, Doug

*import;import;import java.nio.charset.Charset;import java.nio.file.FileSystems;import
java.nio.file.Files;import java.nio.file.Path;import java.nio.file.Paths;import java.util.Calendar;import
java.util.List;import java.util.Vector;import org.apache.commons.lang.mutable.MutableLong;import
org.apache.log4j.LogManager;import org.apache.log4j.Logger;import org.apache.lucene.analysis.Analyzer;import
org.apache.lucene.queryparser.classic.ParseException;import;import;import;import;import;import;import;import;import;import;import;public class IndexTester {    private
static Path path = FileSystems.getDefault().getPath("C:\\temp\\tester");
private static Analyzer analyzer = new StandardAnalyzer();        final
static Logger log = LogManager.getLogger(IndexTester.class);    public
static void main(String args[]) throws IOException, ParseException { Directory idx =;
index("C:\\temp\\test_index");        Term term = new Term("Doc",
"quart?"); // must be lower case.        WildcardQuery wc = new
WildcardQuery(term);        SpanQuery spanTerm = new
SpanMultiTermQueryWrapper<WildcardQuery>(wc);        IndexReader
indexReader =; System.out.println("Term freq=" + indexReader.totalTermFreq(term));
System.out.println("Term freq=" +
indexReader.getSumTotalTermFreq("Doc"));        IndexSearcher isearcher =
new IndexSearcher(indexReader);         TopDocs docs =, 1);        System.out.println("totalHits = " +
docs.totalHits);        IndexReaderContext indexReaderContext =
isearcher.getTopReaderContext();        TermContext context =, term);        TermStatistics
termStatistics = isearcher.termStatistics(term, context); System.out.println("termStatics="
+ termStatistics.totalTermFreq());
}    public static List<String> query(Query query, MutableLong totalHits,
MutableLong totalDocs)            throws IOException {        List<String>
files = new Vector<String>();        Directory idx =;        DirectoryReader indexReader =;        IndexSearcher isearcher = new
IndexSearcher(indexReader);        TopDocs topDocs =, 100);        ScoreDoc[] top =
topDocs.scoreDocs;        System.out.println(topDocs.totalHits);
totalDocs.setValue(top.length);        log.trace("top length" +
top.length);        for (int i = 0; i < top.length; i++) {            int
docID = top[i].doc;            Document doc =
isearcher.doc(docID);            Path path =
Paths.get(doc.get("Path"));            String fileName =
path.getFileName().toString();            log.trace("match fileName =" +
fileName);            files.add(fileName);        }
indexReader.close();        idx.close();        return files;    }
public static void index(String dir) throws IOException { IndexWriterConfig config = new IndexWriterConfig(analyzer);
config.setOpenMode(IndexWriterConfig.OpenMode.CREATE);        Directory idx
=;        IndexWriter indexWriter = new
IndexWriter(idx, config);        List<File> files = lgDir(dir);        for
(File f : files) {            log.trace("filename=" +
f.getName());            //boolean indexExists =
doesIndexExist();           // log.trace("indexExists" +
indexExists);            addDoc(indexWriter, f);               }
indexWriter.commit();        indexWriter.close();        idx.close();
}    /**     * Add the document to the index.     *      * @param
writer     * @param filePath     * @throws IOException     */    private
static void addDoc(IndexWriter writer, File filePath) throws IOException
{        Document doc = new Document();        //byte[] encoded =
List<String> lines =
Charset.forName("Cp1252"));        StringBuffer buf = new
StringBuffer();       java.util.Iterator<String> iter
=lines.iterator();       while (iter.hasNext()) {
buf.append(;           buf.append("\n");       }
//String content = new String(encoded, "UTF-8");        //if
(content.length() > 0) {
log.trace(filePath.getCanonicalPath());            doc.add(new
StringField("Path", filePath.getCanonicalPath(),
Field.Store.YES));            Calendar calendar =
calendar.setTimeInMillis(filePath.lastModified());            doc.add(new
LongField("Date", filePath.lastModified(), Field.Store.YES));            //
log.trace("content size" + content.length());            doc.add(new
TextField("Doc", buf.toString(), Field.Store.YES));
writer.addDocument(doc);            writer.commit();       // }    }
public static List<File> lgDir(String directory) {        File d = new
File(directory);        File[] f = d.listFiles();        List<File> myList
= new Vector<File>();        for (File f1 : f) {
myList.add(f1);        }        log.trace("size count of myList =" +
myList.size());        return myList;    }}*
View raw message