Java Code Examples for org.elasticsearch.action.index.IndexResponse#isCreated()
The following examples show how to use
org.elasticsearch.action.index.IndexResponse#isCreated() .
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: ElasticSearch.java From hsweb-learning with Apache License 2.0 | 6 votes |
private static void CreateIndex(Client client){ String json = "{" + "\"user\":\"daiyutage\"," + "\"postDate\":\"2013-01-30\"," + "\"message\":\"trying out Elasticsearch\"" + "}"; IndexResponse response = client.prepareIndex("twitter", "tweet","2") .setSource(json) .get(); // Index name String _index = response.getIndex(); System.out.println("index:"+_index); // Type name String _type = response.getType(); System.out.println("_type:"+_type); // Document ID (generated or not) String _id = response.getId(); // Version (if it's the first time you index this document, you will get: 1) long _version = response.getVersion(); System.out.println("_version:"+_version); // isCreated() is true if the document is a new one, false if it has been updated boolean created = response.isCreated(); }
Example 2
Source File: RunResultDao.java From usergrid with Apache License 2.0 | 6 votes |
/** * Saves given RunResult in elastic search, uses the id field as index id * * @param runResult RunResult object to save * @return Whether the operation succeeded * @throws Exception */ public boolean save( RunResult runResult ) throws IOException { IndexResponse response = elasticSearchClient.getClient() .prepareIndex( DAO_INDEX_KEY, DAO_TYPE_KEY, runResult.getId() ) .setRefresh( true ) .setSource( jsonBuilder() .startObject() .field( "runId", runResult.getRunId() ) .field( "runCount", runResult.getRunCount() ) .field( "runTime", runResult.getRunTime() ) .field( "ignoreCount", runResult.getIgnoreCount() ) .field( "failureCount", runResult.getFailureCount() ) .field( "createTime", System.nanoTime() ) .field( "failures", runResult.getFailures() ) .endObject() ) .execute() .actionGet(); return response.isCreated(); }
Example 3
Source File: ElasticsearchDataModel.java From elasticsearch-taste with Apache License 2.0 | 6 votes |
private void createUserID(final long userID) { final GetResponse getResponse = client .prepareGet(userIndex, userType, Long.toString(userID)) .setRefresh(true).execute().actionGet(); if (!getResponse.isExists()) { final Map<String, Object> source = new HashMap<>(); source.put("system_id", Long.toString(userID)); source.put(userIdField, userID); source.put(timestampField, new Date()); final IndexResponse response = client .prepareIndex(userIndex, userType, Long.toString(userID)) .setSource(source).setRefresh(true).execute().actionGet(); if (!response.isCreated()) { throw new TasteException("Failed to create " + source); } } }
Example 4
Source File: ElasticsearchDataModel.java From elasticsearch-taste with Apache License 2.0 | 6 votes |
private void createItemID(final long itemID) { final GetResponse getResponse = client .prepareGet(itemIndex, itemType, Long.toString(itemID)) .setRefresh(true).execute().actionGet(); if (!getResponse.isExists()) { final Map<String, Object> source = new HashMap<>(); source.put("system_id", Long.toString(itemID)); source.put(itemIdField, itemID); source.put(timestampField, new Date()); final IndexResponse response = client .prepareIndex(itemIndex, itemType, Long.toString(itemID)) .setSource(source).setRefresh(true).execute().actionGet(); if (!response.isCreated()) { throw new TasteException("Failed to create " + source); } } }
Example 5
Source File: TestTransportClient.java From jframe with Apache License 2.0 | 6 votes |
@Test public void testIndex() throws IOException { IndexResponse response = client.prepareIndex("twitter", "tweet", "1").setSource(XContentFactory.jsonBuilder().startObject() .field("user", "kimchy").field("postDate", new Date()).field("message", "trying out Elasticsearch").endObject()).get(); // Index name String _index = response.getIndex(); // Type name String _type = response.getType(); // Document ID (generated or not) String _id = response.getId(); // Version (if it's the first time you index this document, you will // get: 1) long _version = response.getVersion(); // isCreated() is true if the document is a new one, false if it has // been updated boolean created = response.isCreated(); System.out.println(response.toString()); }
Example 6
Source File: ProviderParamsDao.java From usergrid with Apache License 2.0 | 6 votes |
/** * Saves provider params to elastic search and uses owning username as id. * * @param pp Provider parameters to be saved * @return Whether the operation was successful * @throws Exception */ public boolean save( final ProviderParams pp ) throws IOException { IndexResponse response = elasticSearchClient.getClient() .prepareIndex( DAO_INDEX_KEY, DAO_TYPE_KEY, pp.getUsername() ) .setRefresh( true ) .setSource( jsonBuilder() .startObject() .field( "username", pp.getUsername() ) .field( "instanceType", pp.getInstanceType() ) .field( "accessKey", pp.getAccessKey() ) .field( "secretKey", pp.getSecretKey() ) .field( "imageId", pp.getImageId() ) .field( "keyName", pp.getKeyName() ) .field( "keys", pp.getKeys().toString() ) ) .execute() .actionGet(); return response.isCreated(); }
Example 7
Source File: UserDao.java From usergrid with Apache License 2.0 | 6 votes |
/** * Stores a new user, username is used as ID, since it should be unique * * @param user User to save * @return whether the operation succeeded * @throws Exception */ public boolean save( User user ) throws IOException { IndexResponse response = elasticSearchClient.getClient() .prepareIndex( DAO_INDEX_KEY, DAO_TYPE_KEY, user.getUsername() ) .setRefresh( true ) .setSource( jsonBuilder() .startObject() .field( "password", user.getPassword() ) .endObject() ) .execute() .actionGet(); return response.isCreated(); }
Example 8
Source File: ModuleDao.java From usergrid with Apache License 2.0 | 6 votes |
/** * Saves module to elastic search * * @param module Module to save * @return Whether or not the operation was successful * @throws Exception */ public boolean save( Module module ) throws IOException { IndexResponse response = elasticSearchClient.getClient() .prepareIndex( DAO_INDEX_KEY, DAO_TYPE_KEY, module.getId() ) .setRefresh( true ) .setSource( jsonBuilder() .startObject() .field( "groupId", module.getGroupId() ) .field( "artifactId", module.getArtifactId() ) .field( "version", module.getVersion() ) .field( "vcsRepoUrl", module.getVcsRepoUrl() ) .field( "testPackageBase", module.getTestPackageBase() ) .endObject() ) .execute() .actionGet(); return response.isCreated(); }
Example 9
Source File: NoteDao.java From usergrid with Apache License 2.0 | 6 votes |
public boolean save( Note note ) throws IOException { IndexResponse response = elasticSearchClient.getClient() .prepareIndex( DAO_INDEX_KEY, DAO_TYPE_KEY, note.getId() ) .setRefresh( true ) .setSource( jsonBuilder() .startObject() .field( "commitId", note.getCommitId() ) .field( "runNumber", note.getRunNumber() ) .field( "text", note.getText() ) .endObject() ) .execute() .actionGet(); return response.isCreated(); }
Example 10
Source File: CommitDao.java From usergrid with Apache License 2.0 | 6 votes |
/** * Saves commit to elastic search and uses commit id as index id. * * @param commit Commit to save * @return Whether the operation was successful * @throws Exception */ public boolean save( Commit commit ) throws IOException { IndexResponse response = elasticSearchClient.getClient() .prepareIndex( "modules", "commit", commit.getId() ) .setRefresh( true ) .setSource( jsonBuilder() .startObject() .field( "moduleId", commit.getModuleId() ) .field( "md5", commit.getMd5() ) .field( "createTime", commit.getCreateTime() ) .endObject() ) .execute() .actionGet(); return response.isCreated(); }
Example 11
Source File: RunDao.java From usergrid with Apache License 2.0 | 5 votes |
/** * @param run Run to save in elastic search * @return Whether the operation succeeded * @throws Exception */ public boolean save( Run run ) throws IOException { IndexResponse response = elasticSearchClient.getClient() .prepareIndex( DAO_INDEX_KEY, DAO_TYPE_KEY, run.getId() ) .setRefresh( true ) .setSource( jsonBuilder() .startObject() .field( "id", run.getId() ) .field( "commitId", run.getCommitId() ) .field( "runner", run.getRunner() ) .field( "runNumber", run.getRunNumber() ) .field( "testName", run.getTestName() ) .field( "chopType", run.getChopType() ) .field( "iterations", run.getIterations() ) .field( "totalTestsRun", run.getTotalTestsRun() ) .field( "threads", run.getThreads() ) .field( "delay", run.getDelay() ) .field( "time", run.getTime() ) .field( "actualTime", run.getActualTime() ) .field( "minTime", run.getMinTime() ) .field( "maxTime", run.getMaxTime() ) .field( "meanTime", run.getAvgTime() ) .field( "failures", run.getFailures() ) .field( "ignores", run.getIgnores() ) .field( "saturate", run.getSaturate() ) .field( "startTime", run.getStartTime() ) .field( "stopTime", run.getStopTime() ) .endObject() ) .execute() .actionGet(); return response.isCreated(); }
Example 12
Source File: RunnerDao.java From usergrid with Apache License 2.0 | 5 votes |
/** * * @param runner * @param user * @param commitId * @param moduleId * @return * @throws IOException */ public boolean save( Runner runner, String user, String commitId, String moduleId ) throws IOException { String groupId = new RunnerGroup( user, commitId, moduleId ).getId(); IndexResponse response = elasticSearchClient.getClient() .prepareIndex( DAO_INDEX_KEY, DAO_TYPE_KEY, runner.getUrl() ) .setRefresh( true ) .setSource( jsonBuilder() .startObject() .field( "groupId", groupId ) .field( "ipv4Address", runner.getIpv4Address() ) .field( "hostname", runner.getHostname() ) .field( "serverPort", runner.getServerPort() ) .field( "url", runner.getUrl() ) .field( "tempDir", runner.getTempDir() ) .field( "user", user ) .field( "commitId", commitId ) .field( "moduleId", moduleId ) .endObject() ) .execute() .actionGet(); return response.isCreated(); }
Example 13
Source File: ESClientTest.java From emotional_analysis with Apache License 2.0 | 5 votes |
@Test public void test1() throws Exception { XContentBuilder source = createJson4(); // 存json入索引中 IndexResponse response = client.prepareIndex("twitter", "tweet", "1").setSource(source).get(); // // 结果获取 String index = response.getIndex(); String type = response.getType(); String id = response.getId(); long version = response.getVersion(); boolean created = response.isCreated(); System.out.println(index + " : " + type + ": " + id + ": " + version + ": " + created); }
Example 14
Source File: ESConnector.java From Siamese with GNU General Public License v3.0 | 5 votes |
/*** * A method for one-by-one document indexing * @param index the index name * @param type the doc type name * @param documents the array of documents * @return status of bulk insert (true = no failure, false = failures) */ public boolean sequentialInsert(String index, String type, ArrayList<Document> documents) throws Exception { boolean isCreated = false; for (Document d : documents) { try { XContentBuilder builder = jsonBuilder() .startObject() .field("id", d.getId()) .field("file", d.getFile()) .field("startline", d.getStartLine()) .field("endline", d.getEndLine()) .field("src", d.getSource()) .field("t2src", d.getT2Source()) .field("t1src", d.getT1Source()) .field("tokenizedsrc", d.getTokenizedSource()) .field("origsrc", d.getOriginalSource()) .field("license", d.getLicense()) .field("url", d.getUrl()) .endObject(); // insert document one by one IndexResponse response = client.prepareIndex(index, type).setSource(builder).get(); if (!response.isCreated()) { throw new Exception("cannot insert " + d.getId() + ", " + d.getFile() + ", src = " + builder.string()); } } catch (IOException e) { e.printStackTrace(); return false; } } return true; }
Example 15
Source File: RiverWebTest.java From elasticsearch-river-web with Apache License 2.0 | 4 votes |
public void test_basic() throws Exception { RiverWeb.exitMethod = new IntConsumer() { @Override public void accept(final int value) { if (value != 0) { fail(); } } }; final String index = "webindex"; final String type = "my_web"; final String riverWebIndex = ".river_web"; final String riverWebType = "config"; // create an index runner.createIndex(index, null); runner.ensureYellow(index); // create a mapping final String mappingSource = "{\"my_web\":{\"dynamic_templates\":[{\"url\":{\"match\":\"url\",\"mapping\":{\"type\":\"string\",\"store\":\"yes\",\"index\":\"not_analyzed\"}}},{\"method\":{\"match\":\"method\",\"mapping\":{\"type\":\"string\",\"store\":\"yes\",\"index\":\"not_analyzed\"}}},{\"charSet\":{\"match\":\"charSet\",\"mapping\":{\"type\":\"string\",\"store\":\"yes\",\"index\":\"not_analyzed\"}}},{\"mimeType\":{\"match\":\"mimeType\",\"mapping\":{\"type\":\"string\",\"store\":\"yes\",\"index\":\"not_analyzed\"}}}]}}"; runner.createMapping(index, type, mappingSource); if (!runner.indexExists(index)) { fail(); } String config = type; { final String riverWebSource = "{\"index\":\"" + index + "\",\"urls\":[\"http://www.codelibs.org/\",\"http://fess.codelibs.org/\"]" + ",\"include_urls\":[\"http://www.codelibs.org/.*\",\"http://fess.codelibs.org/.*\"]" + ",\"exclude_urls\":[\".*\\\\.txt\",\".*\\\\.png\",\".*\\\\.gif\",\".*\\\\.js\",\".*\\\\.css\"]" + ",\"max_depth\":5,\"max_access_count\":100,\"num_of_thread\":5,\"interval\":1000" + ",\"target\":[{\"pattern\":{\"url\":\"http://www.codelibs.org/.*\",\"mimeType\":\"text/html\"}" + ",\"properties\":{\"title\":{\"text\":\"title\"},\"body\":{\"text\":\"body\"},\"bodyAsHtml\":{\"html\":\"body\"},\"projects\":{\"text\":\"ul.nav-listlia\",\"is_array\":true}}}" + ",{\"pattern\":{\"url\":\"http://fess.codelibs.org/.*\",\"mimeType\":\"text/html\"}" + ",\"properties\":{\"title\":{\"text\":\"title\"},\"body\":{\"text\":\"body\",\"trim_spaces\":true},\"menus\":{\"text\":\"ul.nav-listlia\",\"is_array\":true}}}]}"; final IndexResponse response = runner.insert(riverWebIndex, riverWebType, config, riverWebSource); if (!response.isCreated()) { fail(); } } RiverWeb.main(new String[] { "--config-id", config, "--es-hosts", "localhost:" + runner.node().settings().get("transport.tcp.port"), "--cluster-name", clusterName, "--cleanup" }); assertTrue(runner.count(index, type).getHits().getTotalHits() + " >= 100", runner.count(index, type).getHits().getTotalHits() >= 100); runner.ensureYellow(); }
Example 16
Source File: RiverWebTest.java From elasticsearch-river-web with Apache License 2.0 | 4 votes |
public void test_overwrite() throws Exception { RiverWeb.exitMethod = new IntConsumer() { @Override public void accept(final int value) { if (value != 0) { fail(); } } }; final String index = "webindex"; final String type = "my_web"; final String riverWebIndex = ".river_web"; final String riverWebType = "config"; // create an index runner.createIndex(index, null); runner.ensureYellow(index); // create a mapping final String mappingSource = "{\"my_web\":{\"dynamic_templates\":[{\"url\":{\"match\":\"url\",\"mapping\":{\"type\":\"string\",\"store\":\"yes\",\"index\":\"not_analyzed\"}}},{\"method\":{\"match\":\"method\",\"mapping\":{\"type\":\"string\",\"store\":\"yes\",\"index\":\"not_analyzed\"}}},{\"charSet\":{\"match\":\"charSet\",\"mapping\":{\"type\":\"string\",\"store\":\"yes\",\"index\":\"not_analyzed\"}}},{\"mimeType\":{\"match\":\"mimeType\",\"mapping\":{\"type\":\"string\",\"store\":\"yes\",\"index\":\"not_analyzed\"}}}]}}"; runner.createMapping(index, type, mappingSource); if (!runner.indexExists(index)) { fail(); } String config = type; { final String riverWebSource = "{\"index\":\"" + index + "\",\"type\":\"" + type + "\",\"urls\":[\"http://fess.codelibs.org/\"],\"include_urls\":[\"http://fess.codelibs.org/.*\"],\"max_depth\":1,\"max_access_count\":1,\"num_of_thread\":1,\"interval\":1000,\"overwrite\":true,\"target\":[{\"pattern\":{\"url\":\"http://fess.codelibs.org/.*\",\"mimeType\":\"text/html\"},\"properties\":{\"title\":{\"text\":\"title\"},\"body\":{\"text\":\"body\",\"trim_spaces\":true}}}]}"; final IndexResponse response = runner.insert(riverWebIndex, riverWebType, config, riverWebSource); if (!response.isCreated()) { fail(); } } RiverWeb.main(new String[] { "--config-id", config, "--es-hosts", "localhost:" + runner.node().settings().get("transport.tcp.port"), "--cluster-name", clusterName, "--cleanup" }); assertEquals(1, runner.count(index, type).getHits().getTotalHits()); SearchResponse response1 = runner.search(index, type, QueryBuilders.termQuery("url", "http://fess.codelibs.org/"), null, 0, 1); RiverWeb.main(new String[] { "--config-id", config, "--es-hosts", "localhost:" + runner.node().settings().get("transport.tcp.port"), "--cluster-name", clusterName, "--cleanup" }); assertEquals(1, runner.count(index, type).getHits().getTotalHits()); SearchResponse response2 = runner.search(index, type, QueryBuilders.termQuery("url", "http://fess.codelibs.org/"), null, 0, 1); assertFalse(response1.getHits().getHits()[0].getSource().get("@timestamp") .equals(response2.getHits().getHits()[0].getSource().get("@timestamp"))); runner.ensureYellow(); }
Example 17
Source File: RiverWebTest.java From elasticsearch-river-web with Apache License 2.0 | 4 votes |
public void test_incremental() throws Exception { RiverWeb.exitMethod = new IntConsumer() { @Override public void accept(final int value) { if (value != 0) { fail(); } } }; final String index = "webindex"; final String type = "my_web"; final String riverWebIndex = ".river_web"; final String riverWebType = "config"; // create an index runner.createIndex(index, null); runner.ensureYellow(index); // create a mapping final String mappingSource = "{\"my_web\":{\"dynamic_templates\":[{\"url\":{\"match\":\"url\",\"mapping\":{\"type\":\"string\",\"store\":\"yes\",\"index\":\"not_analyzed\"}}},{\"method\":{\"match\":\"method\",\"mapping\":{\"type\":\"string\",\"store\":\"yes\",\"index\":\"not_analyzed\"}}},{\"charSet\":{\"match\":\"charSet\",\"mapping\":{\"type\":\"string\",\"store\":\"yes\",\"index\":\"not_analyzed\"}}},{\"mimeType\":{\"match\":\"mimeType\",\"mapping\":{\"type\":\"string\",\"store\":\"yes\",\"index\":\"not_analyzed\"}}}]}}"; runner.createMapping(index, type, mappingSource); if (!runner.indexExists(index)) { fail(); } String config = type; { final String riverWebSource = "{\"index\":\"" + index + "\",\"type\":\"" + type + "\",\"urls\":[\"http://fess.codelibs.org/\"],\"include_urls\":[\"http://fess.codelibs.org/.*\"],\"max_depth\":1,\"max_access_count\":1,\"num_of_thread\":1,\"interval\":1000,\"incremental\":true,\"target\":[{\"pattern\":{\"url\":\"http://fess.codelibs.org/.*\",\"mimeType\":\"text/html\"},\"properties\":{\"title\":{\"text\":\"title\"},\"body\":{\"text\":\"body\",\"trim_spaces\":true}}}]}"; final IndexResponse response = runner.insert(riverWebIndex, riverWebType, config, riverWebSource); if (!response.isCreated()) { fail(); } } RiverWeb.main(new String[] { "--config-id", config, "--es-hosts", "localhost:" + runner.node().settings().get("transport.tcp.port"), "--cluster-name", clusterName, "--cleanup" }); assertEquals(1, runner.count(index, type).getHits().getTotalHits()); SearchResponse response1 = runner.search(index, type, QueryBuilders.termQuery("url", "http://fess.codelibs.org/"), null, 0, 1); RiverWeb.main(new String[] { "--config-id", config, "--es-hosts", "localhost:" + runner.node().settings().get("transport.tcp.port"), "--cluster-name", clusterName, "--cleanup" }); assertEquals(1, runner.count(index, type).getHits().getTotalHits()); SearchResponse response2 = runner.search(index, type, QueryBuilders.termQuery("url", "http://fess.codelibs.org/"), null, 0, 1); assertEquals(response1.getHits().getHits()[0].getSource().get("@timestamp"), response2.getHits().getHits()[0].getSource().get("@timestamp")); runner.ensureYellow(); }
Example 18
Source File: RiverWebTest.java From elasticsearch-river-web with Apache License 2.0 | 4 votes |
public void test_default() throws Exception { RiverWeb.exitMethod = new IntConsumer() { @Override public void accept(final int value) { if (value != 0) { fail(); } } }; final String index = "webindex"; final String type = "my_web"; final String riverWebIndex = ".river_web"; final String riverWebType = "config"; // create an index runner.createIndex(index, null); runner.ensureYellow(index); // create a mapping final String mappingSource = "{\"my_web\":{\"dynamic_templates\":[{\"url\":{\"match\":\"url\",\"mapping\":{\"type\":\"string\",\"store\":\"yes\",\"index\":\"not_analyzed\"}}},{\"method\":{\"match\":\"method\",\"mapping\":{\"type\":\"string\",\"store\":\"yes\",\"index\":\"not_analyzed\"}}},{\"charSet\":{\"match\":\"charSet\",\"mapping\":{\"type\":\"string\",\"store\":\"yes\",\"index\":\"not_analyzed\"}}},{\"mimeType\":{\"match\":\"mimeType\",\"mapping\":{\"type\":\"string\",\"store\":\"yes\",\"index\":\"not_analyzed\"}}}]}}"; runner.createMapping(index, type, mappingSource); if (!runner.indexExists(index)) { fail(); } String config = type; { final String riverWebSource = "{\"index\":\"" + index + "\",\"type\":\"" + type + "\",\"urls\":[\"http://fess.codelibs.org/\"],\"include_urls\":[\"http://fess.codelibs.org/.*\"],\"max_depth\":1,\"max_access_count\":1,\"num_of_thread\":1,\"interval\":1000,\"target\":[{\"pattern\":{\"url\":\"http://fess.codelibs.org/.*\",\"mimeType\":\"text/html\"},\"properties\":{\"title\":{\"text\":\"title\"},\"body\":{\"text\":\"body\",\"trim_spaces\":true}}}]}"; final IndexResponse response = runner.insert(riverWebIndex, riverWebType, config, riverWebSource); if (!response.isCreated()) { fail(); } } RiverWeb.main(new String[] { "--config-id", config, "--es-hosts", "localhost:" + runner.node().settings().get("transport.tcp.port"), "--cluster-name", clusterName, "--cleanup" }); assertEquals(1, runner.count(index, type).getHits().getTotalHits()); SearchResponse response1 = runner.search(index, type, QueryBuilders.termQuery("url", "http://fess.codelibs.org/"), null, 0, 1); RiverWeb.main(new String[] { "--config-id", config, "--es-hosts", "localhost:" + runner.node().settings().get("transport.tcp.port"), "--cluster-name", clusterName, "--cleanup" }); assertEquals(2, runner.count(index, type).getHits().getTotalHits()); SearchResponse response2 = runner.search(index, type, QueryBuilders.termQuery("url", "http://fess.codelibs.org/"), null, 0, 2); assertEquals(1, response1.getHits().getTotalHits()); assertEquals(2, response2.getHits().getTotalHits()); runner.ensureYellow(); }
Example 19
Source File: EsDoc.java From AsuraFramework with Apache License 2.0 | 3 votes |
/** * 新增文档 es自动生成id * @param indexName * @param indexType * @param obj * @return */ public boolean createDocument(String indexName, String indexType,Object obj) { TransportClient client = esClientFactory.getClient(); IndexResponse indexResponse = client.prepareIndex(esClientFactory.getIndexs(indexName),indexType). setSource(JSON.toJSONString(obj)).execute().actionGet(); return indexResponse.isCreated(); }