0

我正在https://developers.google.com/appengine/docs/java/search/overview阅读有关谷歌应用引擎中全文搜索 api (java) 的文档。他们有获取索引的示例:

public Index getIndex() {
      IndexSpec indexSpec = IndexSpec.newBuilder()
          .setName("myindex")
          .setConsistency(Consistency.PER_DOCUMENT)
          .build();
      return SearchServiceFactory.getSearchService().getIndex(indexSpec);
}

创建索引怎么样?如何创建一个?

谢谢

4

2 回答 2

1

你刚刚做到了。你刚刚创建了一个。

public class IndexSpec

Represents information about an index. This class is used to fully specify the index you want to retrieve from the SearchService. To build an instance use the newBuilder() method and set all required parameters, plus optional values different than the defaults.

https://developers.google.com/appengine/docs/java/javadoc/com/google/appengine/api/search/IndexSpec

您可以通过查看 SearchService 来确认这一点

SearchService is also responsible for creating new indexes. For example:
 SearchService searchService = SearchServiceFactory.getSearchService();
  index = searchService.getIndex(IndexSpec.newBuilder().setName("myindex"));

https://developers.google.com/appengine/docs/java/javadoc/com/google/appengine/api/search/SearchService

无论如何,如果它不存在,您的代码似乎会创建一个新索引。这就是文档的建议:

 // Get the index. If not yet created, create it.
  Index index = searchService.getIndex(
  IndexSpec.newBuilder()
      .setIndexName("indexName")
      .setConsistency(Consistency.PER_DOCUMENT));

https://developers.google.com/appengine/docs/java/javadoc/com/google/appengine/api/search/Index

现在,如果再次运行代码并更改一致性会发生什么?您是否拥有具有不同一致性的相同索引?索引是否被覆盖?我不知道。我将使用 SearchService 来查找现有索引,而不是使用可能创建它们的代码,以避免尝试在我的代码中获取索引但无意中更改规范。

于 2012-07-11T07:54:20.770 回答
0

写入文档时会隐式创建索引。一致性是索引的一个属性,即不能有两个同名但一致性不同的索引。

于 2012-07-12T00:31:17.433 回答