简体   繁体   English

Java,Lucene:不区分大小写的搜索不适用于WildCard查询

[英]Java, Lucene : Case insensitive search not working with WildCard query

I am working on a integrating Lucene search and indexing in our Spring-MVC based application and currently I am using Wild-card query search, but it's not working in a case-insensitive manner. 我正在基于Spring-MVC的应用程序中集成Lucene搜索和索引,目前正在使用通配符查询搜索,但是它不区分大小写。 Any ideas? 有任何想法吗?

When I type Ideas, I get the results, but not for ideas. 键入“想法”时,我得到的是结果,但不是想法。

Code : 代码:

@Override
    public synchronized void saveIndexes(String text, String tagFileName, String filePath, long groupId, boolean type, int objectId) {
        try {
        Directory directory = org.apache.lucene.store.FSDirectory.open(path);
            IndexWriterConfig config = new IndexWriterConfig(new SimpleAnalyzer());
            IndexWriter indexWriter = new IndexWriter(directory, config);
 org.apache.lucene.document.Document doc = new org.apache.lucene.document.Document();
            if (filePath != null) {
                File file = new File(filePath); // current directory
                doc.add(new TextField("path", file.getPath(), Field.Store.YES));
            }
            doc.add(new StringField("id", String.valueOf(objectId), Field.Store.YES));
            //  doc.add(new TextField("id",String.valueOf(objectId),Field.Store.YES));
            if (text == null) {
                if (filePath != null) {
                    FileInputStream is = new FileInputStream(filePath);
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                    StringBuilder stringBuffer = new StringBuilder();
                    String line;
                    while ((line = reader.readLine()) != null) {
                        stringBuffer.append(line).append("\n");
                    }
                    stringBuffer.append("\n").append(tagFileName);
                    if(groupNotes!=null){
                        stringBuffer.append(String.valueOf(groupNotes.getNoteNumber()));
                    }
                    reader.close();
                    doc.add(new TextField("contents", stringBuffer.toString(), Field.Store.YES));
                }
            } 

}

// Search code //搜索代码

 @Override
    public List<Integer> searchLucene(String text, long groupId, boolean type) {
 Directory directory = FSDirectory.open(path);
                        IndexReader indexReader = DirectoryReader.open(directory);
                        IndexSearcher indexSearcher = new IndexSearcher(indexReader);
                        Query query = new WildcardQuery(new Term("contents","*"+text + "*"));
                        TopDocs topDocs = indexSearcher.search(query, 50);
                        for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
                            org.apache.lucene.document.Document document = indexSearcher.doc(scoreDoc.doc);
                            objectIds.add(Integer.valueOf(document.get("id")));
                            System.out.println("Text is "+document.get("contents"));
                        }
                        indexSearcher.getIndexReader().close();
                        directory.close();
}

Thank you. 谢谢。

I would try to store text in lowercase into the index. 我会尝试将小写的文本存储到索引中。 And when the user searches the text, make the search query text in lowercase, before searching it in the index. 并且,当用户搜索文本时,请先使用小写字母搜索查询文本,然后再在索引中进行搜索。

https://stackoverflow.com/a/30926385/4587961 https://stackoverflow.com/a/30926385/4587961

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM