lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Milind <mili...@gmail.com>
Subject Can't get case insensitive keyword analyzer to work
Date Sat, 09 Aug 2014 20:39:01 GMT
I looked at a couple of examples on how to get keyword analyzer to be case
insensitive but I think I missed something since it's not working for me.

In the code below, I'm indexing text in upper case and searching in lower
case.  But I get back no hits.  Do I need to something more while
indexing?

    private static class LowerCaseKeywordAnalyzer extends Analyzer
    {
        @Override
        protected TokenStreamComponents createComponents(String
theFieldName, Reader theReader)
        {
            KeywordTokenizer theTokenizer = new KeywordTokenizer(theReader);
            TokenStreamComponents theTokenStreamComponents =
                new TokenStreamComponents(
                        theTokenizer,
                        new LowerCaseFilter(Version.LUCENE_46,
theTokenizer));
            return theTokenStreamComponents;
        }
    }

    private static void addDocment(IndexWriter theWriter,
                                      String theFieldName,
                                      String theValue,
                                      boolean storeTokenized)
        throws Exception
    {
          Document theDocument = new Document();
          FieldType theFieldType = new FieldType();
          theFieldType.setStored(true);
          theFieldType.setIndexed(true);
          theFieldType.setTokenized(storeTokenized);
          theDocument.add(new Field(theFieldName, theValue, theFieldType));
          theWriter.addDocument(theDocument);
    }


    static void testLowerCaseKeywordAnalyzer()
        throws Exception
    {
        Version theVersion = Version.LUCENE_46;
        Directory theIndex = new RAMDirectory();

        Analyzer theAnalyzer = new LowerCaseKeywordAnalyzer();

        IndexWriterConfig theConfig = new IndexWriterConfig(theVersion,
                                                            theAnalyzer);
        IndexWriter theWriter = new IndexWriter(theIndex, theConfig);
        addDocment(theWriter, "sn", "SN345-B21", false);
        addDocment(theWriter, "sn", "SN445-B21", false);
        theWriter.close();

        QueryParser theParser = new QueryParser(theVersion, "sn",
theAnalyzer);
        Query theQuery = theParser.parse("sn:sn345-b21");
        IndexReader theIndexReader = DirectoryReader.open(theIndex);
        IndexSearcher theSearcher = new IndexSearcher(theIndexReader);
        TopScoreDocCollector theCollector = TopScoreDocCollector.create(10,
true);
        theSearcher.search(theQuery, theCollector);
        ScoreDoc[] theHits = theCollector.topDocs().scoreDocs;
        System.out.println("Number of results found: " + theHits.length);
    }

-- 
Regards
Milind

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