Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java,Lucene:在Java中为IndexWriter设置锁超时。_Java_Lucene - Fatal编程技术网

Java,Lucene:在Java中为IndexWriter设置锁超时。

Java,Lucene:在Java中为IndexWriter设置锁超时。,java,lucene,Java,Lucene,我正在致力于将Lucene与基于Spring MVC的应用程序集成。目前我们已经可以使用它了,但是很少出现无法获得锁的错误。之后,我必须手动删除锁定文件,然后它才能正常工作 如何在Java中设置锁定索引的超时?我没有Lucene的任何XML配置。我通过POM.xml在maven中添加了项目库,并实例化了所需的类 代码: public void saveIndexes(String text, String tagFileName, String filePath, long groupId, b

我正在致力于将Lucene与基于Spring MVC的应用程序集成。目前我们已经可以使用它了,但是很少出现
无法获得锁的错误。之后,我必须手动删除锁定文件,然后它才能正常工作

如何在Java中设置锁定索引的超时?我没有Lucene的任何XML配置。我通过POM.xml在maven中添加了项目库,并实例化了所需的类

代码:

public void saveIndexes(String text, String tagFileName, String filePath, long groupId, boolean type, int objectId) {
        try {
            // path is the indexing directory. 
            File testDir;
            Path suggestionsPath;
            Directory suggestionsDir;

            Path phraseSuggestPath;
            Directory phraseSuggestDir;

            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 {

                FieldType fieldType = new FieldType(TextField.TYPE_STORED);
                fieldType.setTokenized(false);
                doc.add(new Field("contents", text+"\n"+tagFileName, fieldType));
            }
            indexWriter.addDocument(doc);
            indexWriter.commit();
            indexWriter.flush();
            indexWriter.close();
            directory.close();

            StandardAnalyzer analyzer = new StandardAnalyzer();
            AnalyzingInfixSuggester wordSuggester = new AnalyzingInfixSuggester(suggestionsDir, analyzer);

            ArrayList<String> words = new ArrayList<>();
            if (text != null) {
                text = html2text(text);
                Pattern pt = Pattern.compile("[^\\w\\s]");
                Matcher match = pt.matcher(text);
                while (match.find()) {
                    String s = match.group();
                    text = text.replaceAll("\\" + s, "");
                }

                if (text.contains(" ")) {
                    Collections.addAll(words, text.split(" "));

                } else {
                    words.add(text);
                }
                SuggestionIterator suggestionIterator = new SuggestionIterator(words.iterator());
                wordSuggester.build(suggestionIterator);
                wordSuggester.close();
                suggestionsDir.close();
            }

            AnalyzingInfixSuggester phraseSuggester = new AnalyzingInfixSuggester(phraseSuggestDir, analyzer);
            if (text != null) {
                text = html2text(text);
                ArrayList<String> phrases = new ArrayList<>();
                phrases.add(text);
                SuggestionIterator suggestionIterator = new SuggestionIterator(phrases.iterator());
                phraseSuggester.build(suggestionIterator);
                phraseSuggester.close();
                phraseSuggestDir.close();
            }

        } catch (Exception ignored) {
        }
    }
public void saveindex(字符串文本、字符串标记文件名、字符串文件路径、长groupId、布尔类型、int objectId){
试一试{
//路径是索引目录。
文件testDir;
路径建议路径;
目录建议目录;
路径短语suggestpath;
目录短语建议目录;
Directory Directory=org.apache.lucene.store.FSDirectory.open(路径);
IndexWriterConfig config=newindexwriterconfig(newsimpleanalyzer());
IndexWriter IndexWriter=新的IndexWriter(目录,配置);
org.apache.lucene.document.document doc=new org.apache.lucene.document.document();
if(filePath!=null){
File File=新文件(filePath);//当前目录
添加(新的TextField(“path”,file.getPath(),Field.Store.YES));
}
doc.add(新的StringField(“id”,String.valueOf(objectId),Field.Store.YES));
//doc.add(新的TextField(“id”,String.valueOf(objectId),Field.Store.YES));
if(text==null){
if(filePath!=null){
FileInputStream is=新的FileInputStream(filePath);
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is));
StringBuilder stringBuffer=新的StringBuilder();
弦线;
而((line=reader.readLine())!=null){
stringBuffer.append(行).append(“\n”);
}
stringBuffer.append(“\n”).append(tagFileName);
reader.close();
添加(新的TextField(“contents”,stringBuffer.toString(),Field.Store.YES));
}
}否则{
FieldType FieldType=新的FieldType(TextField.TYPE\u存储);
fieldType.setTokenized(false);
添加文档(新字段(“内容”,文本+“\n”+标记文件名,字段类型));
}
indexWriter.addDocument(文档);
indexWriter.commit();
indexWriter.flush();
indexWriter.close();
close()目录;
StandardAnalyzer=新的StandardAnalyzer();
AnalyzingInfixSuggester wordSuggester=新的AnalyzingInfixSuggester(suggestionsDir,analyzer);
ArrayList words=新的ArrayList();
如果(文本!=null){
text=html2text(text);
Pattern pt=Pattern.compile(“[^\\w\\s]”);
匹配器匹配=匹配器(文本);
while(match.find()){
字符串s=match.group();
text=text.replaceAll(“\\”+s,”);
}
if(text.contains(“”){
Collections.addAll(words,text.split(“”));
}否则{
添加(文本);
}
SuggestionIterator SuggestionIterator=新的SuggestionIterator(words.iterator());
build(suggestionIterator);
wordSuggester.close();
suggestionsDir.close();
}
AnalyzingInfixSuggester phraseSuggester=新的AnalyzingInfixSuggester(phraseSuggestDir,analyzer);
如果(文本!=null){
text=html2text(text);
ArrayList短语=新建ArrayList();
短语。添加(文本);
SuggestionIterator SuggestionIterator=新的SuggestionIterator(短语.iterator());
构建(suggestionIterator);
短语suggester.close();
phraseSuggestDir.close();
}
}捕获(忽略异常){
}
}
多谢各位

我引用了两件事

打开IndexWriter会为正在使用的目录创建一个锁文件。 尝试在同一目录上打开另一个IndexWriter将导致 锁定失败异常

注意:IndexWriter实例是完全线程安全的,这意味着 多个线程可以同时调用其任何方法。如果你的 应用程序需要外部同步,您不应该 在IndexWriter实例上同步,因为这可能导致死锁; 改用您自己的(非Lucene)对象

因此,如果IndexWriter已在其他地方打开且未关闭,则无法再次打开它。在您的例子中,当两个用户在同一个代码块中时,会出现一些不吉利的时间

你可以通过两种方式解决这个问题

1.指定关键部分:将具有编写器打开、使用和关闭操作的代码部分标记为关键部分,并在该关键部分上应用Java同步。使用一些应用程序单例bean在上进行同步。所以,当另一个用户点击该块时,他将等待第一个用户完成并释放锁

2.单个Writer实例:在应用程序中开发一种机制,在应用程序的生命周期内只打开和关闭Writer一次,并在服务代码中传递该单个实例,这样Writer方法就可以被任意多个用户调用,因为Lucene使Writer实例具有线程安全性。 我想,这可以通过一个单例springbean并将该bean注入到您的服务中来实现

第二种方法的缺点是-针对单个全局索引目录的多服务器部署,如果存在oth