2012-07-11 100 views
0

我正在阅读关于全文搜索api(java)在谷歌应用程序引擎中的文档https://developers.google.com/appengine/docs/java/search/overview。他们有获得索引的例子:在Google App Engine中为全文搜索创建索引

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

如何创建索引?如何创建一个?

感谢

回答

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来查找现有索引,而不是使用可能会创建它们的代码,以避免尝试在我的代码中获取索引,但无意中更改了规范。

0

编写文档时隐式创建索引。一致性是索引的一个属性,即不能有两个具有不同一致性的相同名称的索引。

相关问题