重新加载Lucene建议索引

重新加载Lucene建议索引,lucene,Lucene,如何存储和重新加载Lucene suggester索引 以下是构建建议者索引的方法: def buildAutoCompleteIndex(path:Path, data:List[Map[String,Any]]) :BlendedInfixSuggester = { val directory = FSDirectory.open(path) val autoComplete = new BlendedInfixSuggester(directory, new Stand

如何存储和重新加载Lucene suggester索引

以下是构建建议者索引的方法:

def buildAutoCompleteIndex(path:Path, data:List[Map[String,Any]])
  :BlendedInfixSuggester = {
    val directory = FSDirectory.open(path)
    val autoComplete = new BlendedInfixSuggester(directory, new StandardAnalyzer())
    autoComplete.build(new EntityIteratorStub())

    data.map { d =>
      autoComplete.add(d("text").asInstanceOf[BytesRef],
       d("contexts").asInstanceOf[Set[BytesRef]],
       d("weight").asInstanceOf[Long],
       d("payload").asInstanceOf[BytesRef])
    }
    autoComplete.refresh

    autoComplete
  }
但是,如果我尝试检查索引是否存在于服务器重启上,我会得到一个suggester未生成异常

def checkIfIndexExists(path:Path):BlendedInfixSuggester = {
  val directory = FSDirectory.open(path)
  val autoComplete = new BlendedInfixSuggester(directory, new StandardAnalyzer())

  try {
    // exception occurs here ->
    if (autoComplete.lookup("a", 1, true, false).length > 0) autoComplete
    else null
  } catch {
    case NonFatal(e) => {
      println("Index does not exist, recreating at " + path)
      null
    }
  }
}
编辑==========================

在Lucene AnalyzingFixSuggester中找到此项:


这是否意味着无法存储重新加载的建议索引?

使用提交解决了这个问题

def checkIfIndexExists(path:Path):BlendedInfixSuggester = {
  val directory = FSDirectory.open(path)

  val autoComplete = new BlendedInfixSuggester(directory, new StandardAnalyzer())

  try {
    if (autoComplete.getCount > 0) autoComplete
    else null
  } catch {
    case NonFatal(e) => null
  }
}


def buildAutoCompleteIndex(path:Path, data:List[Map[String,Any]])
:BlendedInfixSuggester = {
  val directory = FSDirectory.open(path)

  val autoComplete = new BlendedInfixSuggester(directory, new StandardAnalyzer())

  // Just build a stub iterator to get started with
  autoComplete.build(new EntityIteratorStub())

  data.map { d =>
    autoComplete.add(d("text").asInstanceOf[BytesRef],
     d("contexts").asInstanceOf[Set[BytesRef]],
     d("weight").asInstanceOf[Long],
     d("payload").asInstanceOf[BytesRef])
  }
  autoComplete.refresh
  autoComplete.commit

  autoComplete
}

试试提交方法。是的,提交成功了,谢谢!
def checkIfIndexExists(path:Path):BlendedInfixSuggester = {
  val directory = FSDirectory.open(path)

  val autoComplete = new BlendedInfixSuggester(directory, new StandardAnalyzer())

  try {
    if (autoComplete.getCount > 0) autoComplete
    else null
  } catch {
    case NonFatal(e) => null
  }
}


def buildAutoCompleteIndex(path:Path, data:List[Map[String,Any]])
:BlendedInfixSuggester = {
  val directory = FSDirectory.open(path)

  val autoComplete = new BlendedInfixSuggester(directory, new StandardAnalyzer())

  // Just build a stub iterator to get started with
  autoComplete.build(new EntityIteratorStub())

  data.map { d =>
    autoComplete.add(d("text").asInstanceOf[BytesRef],
     d("contexts").asInstanceOf[Set[BytesRef]],
     d("weight").asInstanceOf[Long],
     d("payload").asInstanceOf[BytesRef])
  }
  autoComplete.refresh
  autoComplete.commit

  autoComplete
}