lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jack Krupansky" <j...@basetechnology.com>
Subject Re: Help needed: search is returning no results
Date Tue, 18 Dec 2012 14:35:46 GMT
Maybe you wanted "text" fields that are analyzed and tokenized, as opposed 
to string fields which are not analyzed and stored and queried exactly 
as-is.

See:
http://lucene.apache.org/core/4_0_0/core/org/apache/lucene/document/TextField.html

But, show us some of your indexed data and queries that fail.

-- Jack Krupansky

-----Original Message----- 
From: Ramon Casha
Sent: Tuesday, December 18, 2012 9:14 AM
To: java-user@lucene.apache.org
Subject: Help needed: search is returning no results

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-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org 


---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Mime
View raw message