I think you need TextField rather than StringField. See also http://wiki.apache.org/lucene-java/LuceneFAQ#Why_am_I_getting_no_hits_.2BAC8_incorrect_hits.3F
-- Ian. On Tue, Dec 18, 2012 at 2:14 PM, Ramon Casha <rca...@gmail.com> wrote: > I have just downloaded and set up Lucene 4.0.0 to implement a search > facility for a web app I'm developing. > > Creating the index seems to be successful - the files created contain > the text that I'm indexing. However, search is returning no results. > The code I'm using is fairly similar to the examples given. > > Here is the search code: > > private static final File index = new File("/tmp/naturopedia-index"); > private static final Version VERSION = Version.LUCENE_40; > > public String search() throws IOException, ParseException { > Analyzer analyzer = new StandardAnalyzer(VERSION); > Directory directory = FSDirectory.open(index); > > DirectoryReader ireader = DirectoryReader.open(directory); > IndexSearcher isearcher = new IndexSearcher(ireader); > > QueryParser parser = new QueryParser(VERSION, "labels", analyzer); > > Query q = parser.parse(getQuery()); > ScoreDoc[] hits = isearcher.search(q, 1000).scoreDocs; > > for (int i = 0; i < hits.length; i++) { > Document hitDoc = isearcher.doc(hits[i].doc); > System.out.println(hitDoc.getField("id")); > } > ireader.close(); > directory.close(); > return "search"; > } > ---------------------- > Every time I try this, the search returns zero results. I tried with > different fields (text and labels), both of which are indexed, and I > tried different words. Any help would be appreciated. > > Here is the code for producing the index: > > public String crawl() throws IOException { > Analyzer analyzer = new StandardAnalyzer(VERSION); > Directory directory = FSDirectory.open(index); > > IndexWriterConfig config = new IndexWriterConfig(VERSION, analyzer); > config.setOpenMode(IndexWriterConfig.OpenMode.CREATE); > IndexWriter iwriter = new IndexWriter(directory, config); > > DB db = new DB(); // JPA interface class. > > for ( Taxonomy t : db.taxonomy.all() ) { > LOG.log(Level.INFO, "Scanning {0}", t); > > Document doc = new Document(); > doc.add(new LongField("id", t.getId(), Store.YES)); > LOG.log(Level.INFO, " id={0}", t.getId()); > StringBuilder text = new StringBuilder(); > for(Text l : t.getTexts()) { > text.append(l.getWikiText()) > .append((' ')); > } > if(text.length() > 0) { > doc.add(new StringField("text", text.toString(), > Field.Store.YES)); > LOG.log(Level.INFO, " text={0}", text); > } > > StringBuilder labels = new StringBuilder(); > toIndex(labels, t.getLabels()); > for(Image l : t.getImages()) { > toIndex(labels, l.getLabels()); > } > doc.add(new StringField("labels", labels.toString(), > Field.Store.NO)); > LOG.log(Level.INFO, " labels={0}", labels); > StringBuilder sb = new StringBuilder(); > for ( Tag tag : t.getTags() ) { > toIndex(sb, tag.getLabels()); > } > if(!sb.toString().isEmpty()) { > doc.add(new StringField("tags", sb.toString(), > Field.Store.NO)); > LOG.log(Level.INFO, " tags={0}", sb); > } > iwriter.addDocument(doc); > } > > db.close(); > iwriter.close(); > > return "search"; > } > > private void toIndex(StringBuilder sb, LabelGroup lg) { > for(Label l : lg.getLabels()) { > sb.append(l.getText()); > sb.append(" "); > } > } > > > -- > Ramon Casha > > --------------------------------------------------------------------- > To unsubscribe, e-mail: java-user-unsubscr...@lucene.apache.org > For additional commands, e-mail: java-user-h...@lucene.apache.org > --------------------------------------------------------------------- To unsubscribe, e-mail: java-user-unsubscr...@lucene.apache.org For additional commands, e-mail: java-user-h...@lucene.apache.org