【问题标题】:Specifying index path with Hibernate Search and Spring使用 Hibernate Search 和 Spring 指定索引路径
【发布时间】:2014-05-22 01:33:23
【问题描述】:

我在为索引设置正确路径时遇到问题。如果它在我的 spring 应用程序中会很棒,因为我猜即使在我将应用程序部署到 Cloudbees 之后它也可以工作。

这是我试图索引的对象:

@Entity
@Table(name="educations")
@Indexed(index="educations")
public class Education {

我在 servlet-context.xml 中有以下内容:

<resources mapping="/resources/**" location="/resources/"/>

我这样指定lucene索引路径:

Properties props = new Properties();
props.put("hibernate.search.default.indexBase", "resources/lucene/indexes");
entityManagerFactory.setJpaProperties(props);

这没有给我任何错误,但我也找不到文件夹,我不明白。我试着搜索它。

我也试过了:

props.put("hibernate.search.default.indexBase", "classpath:/lucene/indexes");

props.put("hibernate.search.default.indexBase", "/resources/lucene/indexes");

但还是找不到文件夹。然而,经过一段时间的努力,我尝试将它放在我的主目录中。 (这可能会在稍后部署到云时给我带来问题):

props.put("hibernate.search.default.indexBase", "/lucene/indexes");

我得到以下信息

Cannot write into index directory: /lucene/indexes for index educations

所以我认为这是一个权限错误。我在终端(OSX)中尝试以下操作:

sudo chmod -R u+rwX /lucene/indexes/

sudo chmod -R 755 /lucene/indexes/

但仍然是同样的错误。有人可以对此有所了解吗?

谢谢!

编辑: 经过更多调查,我确定这是权限问题。如果我指定 Spring 应用程序根目录的完整路径,它就可以工作。如果不给出完整路径,我仍然不知道如何指定它。

【问题讨论】:

    标签: spring hibernate spring-mvc lucene hibernate-search


    【解决方案1】:

    我也找不到文件夹的位置,所以我想出了以下解决方案:

    首先我通过调用System.getProperty("user.dir") 获得working directory 的位置。这是独立于操作系统的,因此它适用于 linux 和 windows。工作目录是加载应用程序的目录。接下来,我只需将我想要的相对路径作为我的 lucene 索引的位置附加到工作目录文件夹路径。然后我用它作为hibernate.search.default.indexBase 的值。现在我总能知道在哪里寻找 lucene 索引。

    代码如下:

    String luceneFilePath = System.getProperty("user.dir") + "/resources/lucene/indexes";
    
    Properties props = new Properties();
    props.put("hibernate.search.default.indexBase", luceneFilePath);
    entityManagerFactory.setJpaProperties(props);
    

    【讨论】:

      【解决方案2】:

      相对路径是相对于启动 Java 进程的目录的。如果您有一些启动脚本或类似的内容,请查看此脚本的目录。绝对路径可以正常工作,但您当然需要写入权限。

      如果您想为您的案例提供更通用的解决方案,例如,您可以在启动应用程序时将正确的目录设置为系统属性,并在创建 属性 时从那里读取它。或者您尝试以另一种方式在运行时确定应用程序的完整路径。

      【讨论】:

      • 我问自己一个类似的问题;我不知道相对路径在哪个目录中产生。在我的 application.properties 中,我将“/data/index/default”指定为休眠默认索引库。但是我在我的应用程序目录中的任何地方都找不到这样的文件夹 - 尽管它是在 spring boot 启动应用程序时创建的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-20
      • 2015-06-05
      • 1970-01-01
      相关资源
      最近更新 更多