lucene-general mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simon Willnauer <simon.willna...@googlemail.com>
Subject Re: no result
Date Thu, 13 Oct 2011 12:53:12 GMT
hey, see my comments inline...

On Thu, Oct 13, 2011 at 1:03 PM, xdzgor1 <pk@alpha-solutions.dk> wrote:
> Hi, can someone please help me with the following simple example. Why does my
> search not result in a hit?
>
> Analyzer indexAnalyzer = new StandardAnalyzer(Version.LUCENE_33);
> IndexWriterConfig indexWriterConfig = new
> IndexWriterConfig(Version.LUCENE_33, indexAnalyzer);
> Directory ramDirectory = new RAMDirectory();
> IndexWriter indexWriter = new IndexWriter(ramDirectory, indexWriterConfig);
> Document document = new Document();
> document.add(new Field("f1", "a b", Field.Store.YES,
> Field.Index.NOT_ANALYZED));

here you index a document as not analyzed... you can find this by
searching for "a b", if you analyze it you actually end up with two
terms here "a" and "b", makes sense?

> indexWriter.addDocument(document);
> indexWriter.optimize();
> indexWriter.close();
>
> IndexReader indexReader = IndexReader.open(ramDirectory);
> IndexSearcher indexSearcher = new IndexSearcher(indexReader);
> Analyzer queryAnalyzer = new StandardAnalyzer(Version.LUCENE_33);
> QueryParser queryParser = new QueryParser(Version.LUCENE_33, "id",
> queryAnalyzer);
> Query query = queryParser.parse("f1:a b");

here you build a query f1:a OR id:b (id is your default search field &
OR is the default operator)
you got to make sure that you QParser makes one token out of "a b", to
do so you can pass KeywordAnalyzer to the parser and build the query
like this  queryParser.parse("f1:\"a b\""); or use
Field.Index.ANALYZED during indexing and search with
queryParser.parse("f1:(a AND b)");

simon

> TopDocs hits = indexSearcher.search(query, 100);
> int count = hits.totalHits;
>
> Thanks,
> Peter
>
>
> --
> View this message in context: http://lucene.472066.n3.nabble.com/no-result-tp3418300p3418300.html
> Sent from the Lucene - General mailing list archive at Nabble.com.
>

Mime
View raw message