Created
September 3, 2015 07:35
-
-
Save sinmetal/5ac6caf8e3a1313d768e to your computer and use it in GitHub Desktop.
App Engine Search API のStaticsを見るサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.sinmetal.controller; | |
import java.io.*; | |
import java.util.*; | |
import org.slim3.controller.*; | |
import com.google.appengine.api.search.*; | |
public class SearchStorageMonitorController extends SimpleController { | |
SearchService searchService = SearchServiceFactory.getSearchService(); | |
GetResponse<Index> indexResponse = searchService | |
.getIndexes(GetIndexesRequest.newBuilder().setSchemaFetched(true) | |
.build()); | |
Index index; | |
@Override | |
protected Navigation run() throws Exception { | |
index = indexResponse.getResults().get(0); | |
System.out.println(index.toString()); | |
if (isGet()) { | |
return doGet(); | |
} else if (isPost()) { | |
return doPost(); | |
} | |
return null; | |
} | |
Navigation doGet() throws IOException { | |
response.setContentType("text/plain"); | |
try { | |
long storageLimit = index.getStorageLimit(); | |
response.getWriter().write( | |
String.format("Storage Limit = %s\n", storageLimit)); | |
} catch (Throwable e) { | |
response.getWriter().write( | |
String.format("getStorageLimit is error:%s\n", | |
e.getMessage())); | |
} | |
try { | |
long storageUsage = index.getStorageUsage(); | |
response.getWriter().write( | |
String.format("Storage Usage = %s\n", storageUsage)); | |
} catch (Throwable e) { | |
response.getWriter().write( | |
String.format("getStorageUsage is error:%s\n", | |
e.getMessage())); | |
} | |
return null; | |
} | |
Navigation doPost() throws IOException { | |
String id = String.valueOf(new Date().getTime()); | |
Document document = Document | |
.newBuilder() | |
.setId(id) | |
.addField( | |
Field.newBuilder().setName("subject") | |
.setText("my first email")) | |
.addField( | |
Field.newBuilder().setName("body") | |
.setHTML("<html>some content here</html>")) | |
.build(); | |
index.put(document); | |
response.setStatus(201); | |
response.setContentType("text/plain"); | |
response.getWriter().write(String.format("add document id = %s", id)); | |
response.flushBuffer(); | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment