lucene搜索仅适用于小写字母

时间:2014-09-07 21:50:55

标签: lucene

我正在添加我的lucene文档,如下面的

final Document document = new Document();
        document.add(new Field("login", user.getLogin(), Field.Store.YES, Field.Index.NO));
        document.add(new Field("email", user.getEmail(), Field.Store.YES, Field.Index.ANALYZED));
        document.add(new Field("firstName", user.getFirstName(), Field.Store.YES, Field.Index.ANALYZED));
        document.add(new Field("lastName", user.getLastName(), Field.Store.YES, Field.Index.ANALYZED));
        userIndexWriter.addDocument(document);

因此,如果我用小写字母搜索,搜索成功,但如果我用大写字母搜索,则搜索不返回任何内容。

如果我遗失某些东西,任何人都有线索......?

analyzer = new StandardAnalyzer(Version.LUCENE_36);


                final IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_36, analyzer);

                final IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);

和我的搜索经理

final SearcherManager searcherManager = new SearcherManager(indexWriter, true, null);

我正在寻找以下

final BooleanQuery booleanQuery = new BooleanQuery();

            final Query query1 = new PrefixQuery(new Term("email", prefix));
            final Query query2 = new PrefixQuery(new Term("firstName", prefix));
            final Query query3 = new PrefixQuery(new Term("lastName", prefix));

            booleanQuery.add(query1, BooleanClause.Occur.SHOULD);
            booleanQuery.add(query2, BooleanClause.Occur.SHOULD);
            booleanQuery.add(query3, BooleanClause.Occur.SHOULD);

            final SortField sortField = new SortField("firstName", SortField.STRING, true);
            final Sort sort = new Sort(sortField);

            final TopDocs topDocs = searcherManager .search(booleanQuery, DEFAULT_TOP_N_SEARCH_USER, sort);

1 个答案:

答案 0 :(得分:1)

确保对文档和查询应用相同的分析。例如,如果您将索引分析器设置为StandardAnalzyer,那么您还需要将它应用于您的查询,如下所示:

QueryParser queryParser = new QueryParser(Version.LUCENE_CURRENT, "firstName", new StandardAnalyzer(Version.LUCENE_CURRENT));
    try {
        Query q = queryParser.parse("Ameer");
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }