Jave,Lucene:用数字作为字符串进行搜索不起作用

We *_*org 1 java lucene

我正在致力于将 Lucene 集成到我们基于 Spring-MVC 的项目中,目前除了使用数字搜索之外,它运行良好。

每当我尝试搜索类似123Ab123任何其中包含数字的内容时,我都不会得到任何搜索结果。

不过,只要我删除这些数字,它就可以正常工作。

有什么建议么?谢谢。

代码 :

 public List<Integer> searchLucene(String text, long groupId, boolean type) {
        List<Integer> objectIds = new ArrayList<>();
        if (text != null) {
            //String specialChars = "+ - && || ! ( ) { } [ ] ^ \" ~ * ? : \\ /";
            text = text.replace("+", "\\+");
            text = text.replace("-", "\\-");
            text = text.replace("&&", "\\&&");
            text = text.replace("||", "\\||");
            text = text.replace("!", "\\!");
            text = text.replace("(", "\\(");
            text = text.replace(")", "\\)");
            text = text.replace("{", "\\}");
            text = text.replace("{", "\\}");
            text = text.replace("[", "\\[");
            text = text.replace("^", "\\^");
            //  text = text.replace("\"","\\\"");
            text = text.replace("~", "\\~");
            text = text.replace("*", "\\*");
            text = text.replace("?", "\\?");
            text = text.replace(":", "\\:");
            //text = text.replace("\\","\\\\");
            text = text.replace("/", "\\/");
            try {
                Path path;
             //Set system path code
                    Directory directory = FSDirectory.open(path);
                    IndexReader indexReader = DirectoryReader.open(directory);
                    IndexSearcher indexSearcher = new IndexSearcher(indexReader);
                    QueryParser queryParser = new QueryParser("contents", new SimpleAnalyzer());
                    Query query;
                    query = queryParser.parse(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("");
                        System.out.println("id " + document.get("id"));
                        System.out.println("content " + document.get("contents"));
                    }
                    indexSearcher.getIndexReader().close();
                    directory.close();
                return objectIds;
            } catch (Exception ignored) {
           }
        }
        return null;
    }
Run Code Online (Sandbox Code Playgroud)

索引代码:

@Override
    public void saveIndexes(String text, String tagFileName, String filePath, long groupId, boolean type, int objectId) {
        try {
            //indexing directory
            File testDir;
            Path path1;
            Directory index_dir;
            if (type) {
            // System path code
            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);
                    reader.close();
                    doc.add(new TextField("contents", stringBuffer.toString(), Field.Store.YES));
                }
            } else {
                text = text + "\n" + tagFileName;
                doc.add(new TextField("contents", text, Field.Store.YES));
            }
            indexWriter.addDocument(doc);
            indexWriter.commit();
            indexWriter.flush();
            indexWriter.close();
            directory.close();


        } catch (Exception ignored) {
        }
    }
Run Code Online (Sandbox Code Playgroud)

我尝试过使用和不使用通配符 ie *。谢谢。

Sab*_*han 5

问题出在您的索引代码中。

你的字段contents是 aTextField 并且你正在使用 aSimpleAnalyzer所以如果你看到SimpleAnalyzer文档,它会说,

使用 LowerCaseFilter 过滤 LetterTokenizer 的分析器

因此,这意味着对于您的字段,如果将其设置为标记化数字,则会将其删除。

现在看一下TextField代码,这里的 aTextField总是被标记化,无论它是TYPE_STOREDor TYPE_NOT_STORED

因此,如果您希望索引字母和数字,则需要使用 aStringField而不是TextField

StringField文档,

已索引但未标记化的字段:整个字符串值作为单个标记进行索引。例如,这可能用于“国家”字段或“id”字段,或者您打算用于排序或通过字段缓存访问的任何字段。

AStringField永远不会被标记化,无论它是 TYPE_STOREDTYPE_NOT_STORED

因此,索引后,数字将从字段中删除contents,并在没有数字的情况下进行索引,因此您在搜索时找不到这些模式。

QueryParser首先使用如下查询来验证您的索引条款,而不是进行复杂的搜索,

Query wildcardQuery = new WildcardQuery(new Term("contents", searchString));
TopDocs hits = searcher.search(wildcardQuery, 20);
Run Code Online (Sandbox Code Playgroud)

另外,要了解调试是否集中在索引器端或搜索器端,请使用Luke Tool查看是否根据您的需要创建术语。如果存在术语,您可以关注搜索者代码。