2012-07-11 103 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

編寫文檔時隱式創建索引。一致性是索引的一個屬性,即不能有兩個具有不同一致性的相同名稱的索引。

相關問題