lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Milind <mili...@gmail.com>
Subject Re: Why does this search fail?
Date Tue, 26 Aug 2014 16:58:59 GMT
I just wrote this small test case.  Do you mean add some more field values
and search for it?  I added a whole bunch of strings with the same
pattern.  C000x.DevNm00y, changing the x and y values to different numbers.

I changed the code to add some similar and different patterns and this is
what I get

    Directory theDirectory = new RAMDirectory();
    Version theVersion = Version.LUCENE_47;
    Analyzer theAnalyzer = new StandardAnalyzer(theVersion);
    IndexWriterConfig theConfig =
       new IndexWriterConfig(theVersion, theAnalyzer);
    IndexWriter theWriter =
       new IndexWriter(theDirectory, theConfig);

    String theFieldName = "Name";
        String[] theFieldValues = new String[]
           {"C0001.DevNm001", "C0001.DevNm002",
            "John-Appleseed", "JohnAppleseed"};
        Document theDocument = new Document();
        for (int i = 0; i < theFieldValues.length; i++) {
            theDocument.add(new TextField(theFieldName,
                                          theFieldValues[i],
                                          Field.Store.YES));
        }
        theWriter.addDocument(theDocument);
        theWriter.close();

        String[] theQueryStr = new String[]
            {"C0001.DevNm00*", "John-Applesee*", "JohnApplesee*"};
        QueryParser theQueryParser =
            new QueryParser(theVersion, theFieldName, theAnalyzer);
        IndexReader theIndexReader = DirectoryReader.open(theDirectory);
        IndexSearcher theSearcher = new IndexSearcher(theIndexReader);
        for (int i = 0; i < theQueryStr.length; i++) {
            Query theQuery =
                theQueryParser.parse(theFieldName + ":" + theQueryStr[i]);
            System.out.println(theQuery.getClass() + ", " + theQuery);
            TopScoreDocCollector theCollector =
               TopScoreDocCollector.create(10, true);
            theSearcher.search(theQuery, theCollector);
            ScoreDoc[] theHits = theCollector.topDocs().scoreDocs;
            System.out.println("Hits found: " + theHits.length);
        }


Output:

class org.apache.lucene.search.PrefixQuery, Name:c0001.devnm00*
Hits found: 0
class org.apache.lucene.search.PrefixQuery, Name:john-applesee*
Hits found: 0
class org.apache.lucene.search.PrefixQuery, Name:johnapplesee*
Hits found: 1



On Tue, Aug 26, 2014 at 12:30 PM, Ralf Heyde <ralf.heyde@gmx.de> wrote:

> Can you Post the Result of the queryparser for the other queries too?
>
> Gesendet von meinem BlackBerry 10-Smartphone.
>   Originalnachricht
> Von: Milind
> Gesendet: Dienstag, 26. August 2014 18:24
> An: java-user@lucene.apache.org
> Antwort an: java-user@lucene.apache.org
> Betreff: Why does this search fail?
>
> I have a field with the value C0001.DevNm001. If I search for
>
> C0001.DevNm001 --> Get Hit
> DevNm00* --> Get Hit
> C0001.DevNm00* --> Get No Hit
>
> The field gets tokenized on the period since it's surrounded by a letter
> and and a number. The query gets evaluated as a prefix query. I'd have
> thought that this should have found the document. Any clues on why this
> doesn't work?
>
> The full code is below.
>
> Directory theDirectory = new RAMDirectory();
> Version theVersion = Version.LUCENE_47;
> Analyzer theAnalyzer = new StandardAnalyzer(theVersion);
> IndexWriterConfig theConfig =
> new IndexWriterConfig(theVersion, theAnalyzer);
> IndexWriter theWriter = new IndexWriter(theDirectory, theConfig);
>
> String theFieldName = "Name";
> String theFieldValue = "C0001.DevNm001";
> Document theDocument = new Document();
> theDocument.add(new TextField(theFieldName, theFieldValue,
> Field.Store.YES));
> theWriter.addDocument(theDocument);
> theWriter.close();
>
> String theQueryStr = theFieldName + ":C0001.DevNm00*";
> Query theQuery =
> new QueryParser(theVersion, theFieldName,
> theAnalyzer).parse(theQueryStr);
> System.out.println(theQuery.getClass() + ", " + theQuery);
> IndexReader theIndexReader = DirectoryReader.open(theDirectory);
> IndexSearcher theSearcher = new IndexSearcher(theIndexReader);
> TopScoreDocCollector collector = TopScoreDocCollector.create(10,
> true);
> theSearcher.search(theQuery, collector);
> ScoreDoc[] theHits = collector.topDocs().scoreDocs;
> System.out.println("Hits found: " + theHits.length);
>
> Output:
>
> class org.apache.lucene.search.PrefixQuery, Name:c0001.devnm00*
> Hits found: 0
>
>
> --
> Regards
> Milind
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>
>


-- 
Regards
Milind

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message