Java Code Examples for com.google.appengine.api.search.Index#put()
The following examples show how to use
com.google.appengine.api.search.Index#put() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GallerySearchIndex.java From appinventor-extensions with Apache License 2.0 | 6 votes |
/** * index gallery app into search index * @param app galleryapp */ public void indexApp (GalleryApp app) { // take the title, description, and the user name and index it // need to build up a string with all meta data String indexWords = app.getTitle()+" "+app.getDescription() + " " + app.getDeveloperName(); // now create the doc Document doc = Document.newBuilder() .setId(String.valueOf(app.getGalleryAppId())) .addField(Field.newBuilder().setName("content").setText(indexWords)) .build(); Index index = getIndex(); try { index.put(doc); } catch (PutException e) { if (StatusCode.TRANSIENT_ERROR.equals(e.getOperationResult().getCode())) { // retry putting the document } } }
Example 2
Source File: Utils.java From java-docs-samples with Apache License 2.0 | 6 votes |
/** * 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; } }
Example 3
Source File: IndexTest.java From appengine-tck with Apache License 2.0 | 6 votes |
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; }
Example 4
Source File: SearchServiceTest.java From appengine-tck with Apache License 2.0 | 6 votes |
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; }
Example 5
Source File: SearchServlet.java From appengine-java-vm-runtime with Apache License 2.0 | 5 votes |
public void addDocument(Index index, String docid) { Document.Builder builder = Document.newBuilder(); builder.setId(docid); builder.setRank(docid.hashCode()); Field.Builder field = Field.newBuilder(); field.setName("title"); field.setText(String.format("Title: title%%<%s>", docid)); builder.addField(field); field = Field.newBuilder(); field.setName("body"); field.setHTML(String.format("<h3>body of %s, some string</h3>", docid)); builder.addField(field); field = Field.newBuilder(); field.setName("atom"); field.setAtom(String.format("atom%% <%s>", docid)); builder.addField(field); field = Field.newBuilder(); field.setName("number"); field.setNumber(docid.hashCode() % 4096); builder.addField(field); field = Field.newBuilder(); field.setName("date"); field.setDate(new Date(2011 - 1900, 11 - 1, (docid.hashCode() % 30) + 1)); builder.addField(field); index.put(builder.build()); }
Example 6
Source File: SearchTestBase.java From appengine-tck with Apache License 2.0 | 5 votes |
protected void addDocs(Index index, int docCount) throws ParseException, InterruptedException { if (searchDocs(index, "", 0).getNumberFound() == 0) { List<Document> documents = new ArrayList<>(); Calendar cal = Calendar.getInstance(); DateFormat dfDate = new SimpleDateFormat("yyyy,M,d"); for (int i = 0; i < docCount; i++) { Builder docBuilder = Document.newBuilder(); // two text field with different locale docBuilder.addField(Field.newBuilder().setName("textfield").setText("text with num " + i)); Field field = Field.newBuilder().setName("textfield").setText("C'est la vie " + i).setLocale(Locale.FRENCH).build(); docBuilder.addField(field); docBuilder.addField(Field.newBuilder().setName("numfield").setNumber(i)); String dateVal = "" + cal.get(Calendar.YEAR) + ","; dateVal += cal.get(Calendar.MONTH) + ","; int day = cal.get(Calendar.DATE) + i; dateVal += day; docBuilder.addField(Field.newBuilder().setName("datefield").setDate(dfDate.parse(dateVal))); docBuilder.addField(Field.newBuilder().setName("htmlfield").setHTML("<B>html</B> " + i)); docBuilder.addField(Field.newBuilder().setName("atomfield").setAtom("atom" + i + ".com")); GeoPoint geoPoint = new GeoPoint((double) i, (double) (100 + i)); docBuilder.addField(Field.newBuilder().setName("geofield").setGeoPoint(geoPoint)); // two field in same name and with different field type docBuilder.addField(Field.newBuilder().setName("mixfield").setText("text and number mix field")); docBuilder.addField(Field.newBuilder().setName("mixfield").setNumber(987)); docBuilder.setId("selfid" + i); // only doc(id="selfid0") has "cn" locale, others have "en" locale if (i == 0) { docBuilder.setLocale(new Locale("cn")); } else { docBuilder.setLocale(new Locale("en")); } documents.add(docBuilder.build()); } index.put(documents); sync(); } }
Example 7
Source File: DocumentTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@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()); }
Example 8
Source File: IndexTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testPutDeleteDocs() throws InterruptedException { String indexName = "put-index"; String docId = "testPutDocs"; List<String> docIdList = new ArrayList<>(); 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); docIdList.add(docId + "1"); field = Field.newBuilder().setName("subject").setText("put(Document)").build(); Document document = Document.newBuilder() .setId(docId + "2") .addField(field).build(); index.put(document); docIdList.add(docId + "1"); GetIndexesRequest request = GetIndexesRequest.newBuilder() .setIndexNamePrefix(indexName) .build(); GetResponse<Index> response = searchService.getIndexes(request); List<Index> listIndexes = response.getResults(); for (Index oneIndex : listIndexes) { Field retField = oneIndex.get(docId + "1").getOnlyField("subject"); assertEquals("put(Document.Builder)", retField.getText()); retField = oneIndex.get(docId + "2").getOnlyField("subject"); assertEquals("put(Document)", retField.getText()); oneIndex.delete(docIdList.get(0)); sync(); assertNull(oneIndex.get(docIdList.get(0))); } }
Example 9
Source File: IndexTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testPutDeleteAsyncDocsByString() throws InterruptedException { String indexName = "put-index"; String docId = "testPutDocs"; List<String> docIdList = new ArrayList<>(); 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); docIdList.add(docId + "1"); field = Field.newBuilder().setName("subject").setText("put(Document)").build(); Document document = Document.newBuilder() .setId(docId + "2") .addField(field).build(); index.put(document); docIdList.add(docId + "1"); GetIndexesRequest request = GetIndexesRequest.newBuilder() .setIndexNamePrefix(indexName) .build(); GetResponse<Index> response = searchService.getIndexes(request); List<Index> listIndexes = response.getResults(); for (Index oneIndex : listIndexes) { Field retField = oneIndex.get(docId + "1").getOnlyField("subject"); assertEquals("put(Document.Builder)", retField.getText()); retField = oneIndex.get(docId + "2").getOnlyField("subject"); assertEquals("put(Document)", retField.getText()); oneIndex.deleteAsync(docIdList.get(0)); sync(); assertNull(oneIndex.get(docIdList.get(0))); } }
Example 10
Source File: FieldTest.java From appengine-tck with Apache License 2.0 | 4 votes |
@Test public void testDocFields() throws Exception { String indexName = "test-doc-fields"; Index index = searchService.getIndex(IndexSpec.newBuilder().setName(indexName)); delDocs(index); Builder docBuilder = Document.newBuilder(); Field field = Field.newBuilder().setName("textfield").setText("text field").build(); docBuilder.addField(field); field = Field.newBuilder().setName("numberfield").setNumber(123).build(); docBuilder.addField(field); Date now = new Date(); field = Field.newBuilder().setName("datefield").setDate(now).build(); docBuilder.addField(field); field = Field.newBuilder().setName("htmlfield").setHTML("<html>html field</html>").build(); docBuilder.addField(field); User currentUser = new User("[email protected]", "appenginetest.com"); field = Field.newBuilder().setName("atomfield").setAtom(currentUser.getAuthDomain()).build(); docBuilder.addField(field); GeoPoint geoPoint = new GeoPoint((double) -10, 10.000001); field = Field.newBuilder().setName("geofield").setGeoPoint(geoPoint).build(); docBuilder.addField(field); index.put(docBuilder); sync(); Results<ScoredDocument> result = searchDocs(index, "", 0); assertEquals(1, result.getNumberReturned()); ScoredDocument doc = result.iterator().next(); Field retField = doc.getOnlyField("textfield"); assertEquals(FieldType.TEXT, retField.getType()); assertEquals("textfield", retField.getName()); assertEquals("text field", retField.getText()); retField = doc.getOnlyField("numberfield"); assertEquals(FieldType.NUMBER, retField.getType()); assertEquals(new Double("123"), retField.getNumber()); retField = doc.getOnlyField("datefield"); assertEquals(FieldType.DATE, retField.getType()); assertEquals(now, retField.getDate()); retField = doc.getOnlyField("htmlfield"); assertEquals(FieldType.HTML, retField.getType()); assertEquals("<html>html field</html>", retField.getHTML()); retField = doc.getOnlyField("atomfield"); assertEquals(FieldType.ATOM, retField.getType()); assertEquals(currentUser.getAuthDomain(), retField.getAtom()); retField = doc.getOnlyField("geofield"); assertEquals(FieldType.GEO_POINT, retField.getType()); assertEquals(-10, retField.getGeoPoint().getLatitude(), 0); assertEquals(10.000001, retField.getGeoPoint().getLongitude(), 0.000000); }