本文整理汇总了Java中com.google.appengine.api.search.IndexSpec类的典型用法代码示例。如果您正苦于以下问题:Java IndexSpec类的具体用法?Java IndexSpec怎么用?Java IndexSpec使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
IndexSpec类属于com.google.appengine.api.search包,在下文中一共展示了IndexSpec类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doGet
import com.google.appengine.api.search.IndexSpec; //导入依赖的package包/类
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
PrintWriter out = resp.getWriter();
Document document = Document.newBuilder()
.setId("AZ125")
.addField(Field.newBuilder().setName("myField").setText("myValue")).build();
try {
Utils.indexADocument(INDEX, document);
} catch (InterruptedException e) {
out.println("Interrupted");
return;
}
out.println("Indexed a new document.");
// [START get_document]
IndexSpec indexSpec = IndexSpec.newBuilder().setName(INDEX).build();
Index index = SearchServiceFactory.getSearchService().getIndex(indexSpec);
// Fetch a single document by its doc_id
Document doc = index.get("AZ125");
// Fetch a range of documents by their doc_ids
GetResponse<Document> docs = index.getRange(
GetRequest.newBuilder().setStartId("AZ125").setLimit(100).build());
// [END get_document]
out.println("myField: " + docs.getResults().get(0).getOnlyField("myField").getText());
}
示例2: indexADocument
import com.google.appengine.api.search.IndexSpec; //导入依赖的package包/类
/**
* Put a given document into an index with the given indexName.
* @param indexName The name of the index.
* @param document A document to add.
* @throws InterruptedException When Thread.sleep is interrupted.
*/
// [START putting_document_with_retry]
public static void indexADocument(String indexName, Document document)
throws InterruptedException {
IndexSpec indexSpec = IndexSpec.newBuilder().setName(indexName).build();
Index index = SearchServiceFactory.getSearchService().getIndex(indexSpec);
final int maxRetry = 3;
int attempts = 0;
int delay = 2;
while (true) {
try {
index.put(document);
} catch (PutException e) {
if (StatusCode.TRANSIENT_ERROR.equals(e.getOperationResult().getCode())
&& ++attempts < maxRetry) { // retrying
Thread.sleep(delay * 1000);
delay *= 2; // easy exponential backoff
continue;
} else {
throw e; // otherwise throw
}
}
break;
}
}
示例3: indexADocument_successfullyInvoked
import com.google.appengine.api.search.IndexSpec; //导入依赖的package包/类
@Test
public void indexADocument_successfullyInvoked() throws Exception {
String id = "test";
Document doc = Document.newBuilder()
.setId(id)
.addField(Field.newBuilder().setName("f").setText("v"))
.build();
Utils.indexADocument(INDEX, doc);
// get the document by id
IndexSpec indexSpec = IndexSpec.newBuilder().setName(INDEX).build();
Index index = SearchServiceFactory.getSearchService().getIndex(indexSpec);
Document fetched = index.get(id);
assertThat(fetched.getOnlyField("f").getText())
.named("A value of the fetched document")
.isEqualTo("v");
}
示例4: indexADocument
import com.google.appengine.api.search.IndexSpec; //导入依赖的package包/类
/**
* Put a given document into an index with the given indexName.
*
* @param indexName The name of the index.
* @param document A document to add.
* @throws InterruptedException When Thread.sleep is interrupted.
*/
// [START putting_document_with_retry]
public static void indexADocument(String indexName, Document document)
throws InterruptedException {
IndexSpec indexSpec = IndexSpec.newBuilder().setName(indexName).build();
Index index = SearchServiceFactory.getSearchService().getIndex(indexSpec);
final int maxRetry = 3;
int attempts = 0;
int delay = 2;
while (true) {
try {
index.put(document);
} catch (PutException e) {
if (StatusCode.TRANSIENT_ERROR.equals(e.getOperationResult().getCode())
&& ++attempts < maxRetry) { // retrying
Thread.sleep(delay * 1000);
delay *= 2; // easy exponential backoff
continue;
} else {
throw e; // otherwise throw
}
}
break;
}
}
示例5: indexADocument_successfullyInvoked
import com.google.appengine.api.search.IndexSpec; //导入依赖的package包/类
@Test
public void indexADocument_successfullyInvoked() throws Exception {
String id = "test";
Document doc =
Document.newBuilder()
.setId(id)
.addField(Field.newBuilder().setName("f").setText("v"))
.build();
Utils.indexADocument(INDEX, doc);
// get the document by id
IndexSpec indexSpec = IndexSpec.newBuilder().setName(INDEX).build();
Index index = SearchServiceFactory.getSearchService().getIndex(indexSpec);
Document fetched = index.get(id);
assertThat(fetched.getOnlyField("f").getText())
.named("A value of the fetched document")
.isEqualTo("v");
}
示例6: createIndex
import com.google.appengine.api.search.IndexSpec; //导入依赖的package包/类
private Index createIndex(String indexName, String docId) {
Index index = searchService.getIndex(IndexSpec.newBuilder()
.setName(indexName)
.build());
Field field = Field.newBuilder().setName("subject").setText("put(Document.Builder)").build();
Document.Builder docBuilder = Document.newBuilder()
.setId(docId + "1")
.addField(field);
index.put(docBuilder);
field = Field.newBuilder().setName("subject").setText("put(Document)").build();
Document document = Document.newBuilder()
.setId(docId + "2")
.addField(field).build();
index.put(document);
return index;
}
示例7: getIndex
import com.google.appengine.api.search.IndexSpec; //导入依赖的package包/类
private Index getIndex(ParticipantId participant) {
return searchService.getIndex(
IndexSpec.newBuilder().setName(getIndexName(participant))
// We could consider making this global, though the documentation
// says not more than 1 update per second, which is a bit borderline.
.setConsistency(Consistency.PER_DOCUMENT).build());
}
示例8: doSearch
import com.google.appengine.api.search.IndexSpec; //导入依赖的package包/类
private Results<ScoredDocument> doSearch() {
String indexName = SEARCH_INDEX;
// [START search_with_options]
try {
// Build the SortOptions with 2 sort keys
SortOptions sortOptions = SortOptions.newBuilder()
.addSortExpression(SortExpression.newBuilder()
.setExpression("price")
.setDirection(SortExpression.SortDirection.DESCENDING)
.setDefaultValueNumeric(0))
.addSortExpression(SortExpression.newBuilder()
.setExpression("brand")
.setDirection(SortExpression.SortDirection.DESCENDING)
.setDefaultValue(""))
.setLimit(1000)
.build();
// Build the QueryOptions
QueryOptions options = QueryOptions.newBuilder()
.setLimit(25)
.setFieldsToReturn("model", "price", "description")
.setSortOptions(sortOptions)
.build();
// A query string
String queryString = "product: coffee roaster AND price < 500";
// Build the Query and run the search
Query query = Query.newBuilder().setOptions(options).build(queryString);
IndexSpec indexSpec = IndexSpec.newBuilder().setName(indexName).build();
Index index = SearchServiceFactory.getSearchService().getIndex(indexSpec);
Results<ScoredDocument> result = index.search(query);
return result;
} catch (SearchException e) {
// handle exception...
}
// [END search_with_options]
return null;
}
示例9: doGet
import com.google.appengine.api.search.IndexSpec; //导入依赖的package包/类
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
PrintWriter out = resp.getWriter();
Document document =
Document.newBuilder()
.setId("AZ125")
.addField(Field.newBuilder().setName("myField").setText("myValue"))
.build();
try {
Utils.indexADocument(INDEX, document);
} catch (InterruptedException e) {
out.println("Interrupted");
return;
}
out.println("Indexed a new document.");
// [START get_document]
IndexSpec indexSpec = IndexSpec.newBuilder().setName(INDEX).build();
Index index = SearchServiceFactory.getSearchService().getIndex(indexSpec);
// Fetch a single document by its doc_id
Document doc = index.get("AZ125");
// Fetch a range of documents by their doc_ids
GetResponse<Document> docs =
index.getRange(GetRequest.newBuilder().setStartId("AZ125").setLimit(100).build());
// [END get_document]
out.println("myField: " + docs.getResults().get(0).getOnlyField("myField").getText());
}
示例10: getIndex
import com.google.appengine.api.search.IndexSpec; //导入依赖的package包/类
/**
* Obtains the Index.
*
* @param clazz
* the class
* @return the {@link Index} specified with the {@link DocumentIndex} annotation.
*/
public Index getIndex(Class<?> clazz) {
String indexName;
try {
indexName = ObjectParser.getIndexName(clazz);
} catch (AnnotationNotFoundException e) {
throw new AnnotationNotFoundException(e.getMessage());
}
IndexSpec indexSpec = IndexSpec.newBuilder().setName(indexName).build();
Index index = SearchServiceFactory.getSearchService().getIndex(indexSpec);
return index;
}
示例11: getIndex
import com.google.appengine.api.search.IndexSpec; //导入依赖的package包/类
private static Index getIndex(String indexName) {
Map<String, Index> indicesTable = getIndicesTable();
Index index = indicesTable.get(indexName);
if (index == null) {
IndexSpec indexSpec = IndexSpec.newBuilder().setName(indexName).build();
index = SearchServiceFactory.getSearchService().getIndex(indexSpec);
indicesTable.put(indexName, index);
}
return index;
}
示例12: initIndexes
import com.google.appengine.api.search.IndexSpec; //导入依赖的package包/类
public void initIndexes() {
SearchService search = SearchServiceFactory.getSearchService();
IndexSpec.Builder spec = IndexSpec.newBuilder();
for (int i = 0; i < 25; i++) {
String name = String.format("index%s", i);
spec.setName(name);
addDocuments(search.getIndex(spec), name, i == 0 ? 25 : 1);
}
search = SearchServiceFactory.getSearchService("ns");
spec.setName("<b>");
addDocuments(search.getIndex(spec), "other", 1);
}
示例13: testCreateDocument
import com.google.appengine.api.search.IndexSpec; //导入依赖的package包/类
@Test
public void testCreateDocument() throws Exception {
String indexName = "test-doc";
Index index = searchService.getIndex(IndexSpec.newBuilder().setName(indexName));
delDocs(index);
Builder docBuilder = Document.newBuilder().setId("tck").setLocale(Locale.FRENCH).setRank(8);
docBuilder.addField(Field.newBuilder().setName("field1").setText("text field"));
docBuilder.addField(Field.newBuilder().setName("field1").setNumber(987));
docBuilder.addField(Field.newBuilder().setName("field2").setNumber(123));
docBuilder.addField(Field.newBuilder().setName("field3").setDate(new Date()));
index.put(docBuilder.build());
sync();
Results<ScoredDocument> result = searchDocs(index, "", 0);
assertEquals(1, result.getNumberReturned());
ScoredDocument retDoc = result.iterator().next();
assertEquals("tck", retDoc.getId());
assertEquals(Locale.FRENCH, retDoc.getLocale());
assertEquals(8, retDoc.getRank());
assertEquals(2, retDoc.getFieldCount("field1"));
assertEquals(1, retDoc.getFieldCount("field3"));
assertEquals(3, retDoc.getFieldNames().size());
Iterator<Field> fields = retDoc.getFields().iterator();
int count = 0;
for ( ; fields.hasNext() ; ++count ) {
fields.next();
}
assertEquals(4, count);
fields = retDoc.getFields("field1").iterator();
count = 0;
for ( ; fields.hasNext() ; ++count ) {
fields.next();
}
assertEquals(2, count);
Field field = retDoc.getOnlyField("field2");
assertEquals(FieldType.NUMBER, field.getType());
}
示例14: testNamespaceWithBug
import com.google.appengine.api.search.IndexSpec; //导入依赖的package包/类
@Test
public void testNamespaceWithBug() throws InterruptedException, ParseException {
String ns = "ns-indextest";
String indexName = "ns-index";
int docCount = 5;
NamespaceManager.set(ns);
SearchService searchService2 = SearchServiceFactory.getSearchService();
Index index = searchService2.getIndex(IndexSpec.newBuilder()
.setName(indexName)
.build());
delDocs(index);
addDocs(index, docCount);
GetIndexesRequest request = GetIndexesRequest.newBuilder()
.setIndexNamePrefix(indexName)
.setOffset(0)
.setNamespace(ns)
.setLimit(10)
.build();
assertEquals(ns, request.getNamespace());
GetResponse<Index> response = searchService2.getIndexes(request);
List<Index> listIndexes = response.getResults();
for (Index oneIndex : listIndexes) {
assertEquals(ns, listIndexes.get(0).getNamespace());
assertEquals(indexName, listIndexes.get(0).getName());
verifyDocCount(oneIndex, docCount);
}
assertEquals(ns, searchService2.getNamespace());
NamespaceManager.set("");
}
示例15: testNamespace
import com.google.appengine.api.search.IndexSpec; //导入依赖的package包/类
public void testNamespace() throws InterruptedException, ParseException {
String ns = "ns-indextest";
String indexName = "ns-index";
int docCount = 5;
NamespaceManager.set(ns);
Index index = searchService.getIndex(IndexSpec.newBuilder()
.setName(indexName)
.build());
delDocs(index);
addDocs(index, docCount);
GetIndexesRequest request = GetIndexesRequest.newBuilder()
.setIndexNamePrefix(indexName)
.setOffset(0)
.setNamespace(ns)
.setLimit(10)
.build();
assertEquals(ns, request.getNamespace());
GetResponse<Index> response = searchService.getIndexes(request);
List<Index> listIndexes = response.getResults();
for (Index oneIndex : listIndexes) {
assertEquals(ns, listIndexes.get(0).getNamespace());
assertEquals(indexName, listIndexes.get(0).getName());
verifyDocCount(oneIndex, docCount);
}
assertEquals(ns, searchService.getNamespace());
NamespaceManager.set("");
}