org.elasticsearch.action.index.IndexResponse Java Examples

The following examples show how to use org.elasticsearch.action.index.IndexResponse. 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: IndexAnomalyDetectorJobActionHandler.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
private void onIndexAnomalyDetectorJobResponse(IndexResponse response, AnomalyDetectorFunction function) throws IOException {
    if (response == null || (response.getResult() != CREATED && response.getResult() != UPDATED)) {
        channel.sendResponse(new BytesRestResponse(response.status(), response.toXContent(channel.newErrorBuilder(), EMPTY_PARAMS)));
        return;
    }
    if (function != null) {
        function.execute();
    } else {
        XContentBuilder builder = channel
            .newBuilder()
            .startObject()
            .field(RestHandlerUtils._ID, response.getId())
            .field(RestHandlerUtils._VERSION, response.getVersion())
            .field(RestHandlerUtils._SEQ_NO, response.getSeqNo())
            .field(RestHandlerUtils._PRIMARY_TERM, response.getPrimaryTerm())
            .endObject();
        channel.sendResponse(new BytesRestResponse(RestStatus.OK, builder));
    }
}
 
Example #2
Source File: AbstractApiAction.java    From deprecated-security-advanced-modules with Apache License 2.0 6 votes vote down vote up
protected void saveAnUpdateConfigs(final Client client, final RestRequest request, final CType cType,
								   final SecurityDynamicConfiguration<?> configuration, OnSucessActionListener<IndexResponse> actionListener) {
	final IndexRequest ir = new IndexRequest(this.opendistroIndex);

	//final String type = "_doc";
	final String id = cType.toLCString();

	configuration.removeStatic();

	try {
		client.index(ir.id(id)
						.setRefreshPolicy(RefreshPolicy.IMMEDIATE)
						.setIfSeqNo(configuration.getSeqNo())
						.setIfPrimaryTerm(configuration.getPrimaryTerm())
						.source(id, XContentHelper.toXContent(configuration, XContentType.JSON, false)),
				new ConfigUpdatingActionListener<IndexResponse>(client, actionListener));
	} catch (IOException e) {
		throw ExceptionsHelper.convertToElastic(e);
	}
}
 
Example #3
Source File: EmbeddedServerIT.java    From arctic-sea with Apache License 2.0 6 votes vote down vote up
@Test
public void connectEmbeddedMode() throws Exception {

    settings.setNodeConnectionMode(ElasticsearchSettingsKeys.CONNECTION_MODE_EMBEDDED_SERVER);
    adminHandler.init();

    Map<String, Object> data = new HashMap<>();
    data.put("test", "test-string");
    IndexResponse idx = dataHandler.persist(data);

    Thread.sleep(2000);

    String ret = dataHandler.getClient().get(new GetRequest(idx.getIndex(), idx.getId()), RequestOptions.DEFAULT)
            .getSourceAsString();
    Assertions.assertNotNull(ret);

    adminHandler.destroy();

    try {
        FileUtils.deleteDirectory(new File("./elasticsearch"));
    } catch (IOException e) {
        logger.info(e.getMessage(), e);
    }
}
 
Example #4
Source File: ElasticsearchDataHandler.java    From arctic-sea with Apache License 2.0 6 votes vote down vote up
@Override
public IndexResponse persist(Map<String, Object> dataMap) throws ElasticsearchGenerationException, IOException {
    if (!settings.isLoggingEnabled()) {
        return null;
    }
    if (adminHandler.getElasticsearchClient() == null) {
        throw new NullPointerException("Client is not initialized. Data will not be persisted.");
    }

    dataMap.put(ServiceEventDataMapping.TIMESTAMP_FIELD.getName(), DateTime.now(DateTimeZone.UTC));
    dataMap.put(ServiceEventDataMapping.UUID_FIELD.getName(), settings.getUuid());
    logger.debug("Persisting {}", dataMap);
    IndexResponse response = adminHandler.getElasticsearchClient().index(
            new IndexRequest(settings.getIndexId()).type(settings.getTypeId()).source(dataMap),
            RequestOptions.DEFAULT);
    return response;
}
 
Example #5
Source File: EsAbstractBehavior.java    From fess with Apache License 2.0 6 votes vote down vote up
@Override
protected int delegateUpdate(final Entity entity, final UpdateOption<? extends ConditionBean> option) {
    final EsAbstractEntity esEntity = (EsAbstractEntity) entity;
    final IndexRequestBuilder builder = createUpdateRequest(esEntity);

    final IndexResponse response = builder.execute().actionGet(indexTimeout);
    final long seqNo = response.getSeqNo();
    if (seqNo != SequenceNumbers.UNASSIGNED_SEQ_NO) {
        esEntity.asDocMeta().seqNo(seqNo);
    }
    final long primaryTerm = response.getPrimaryTerm();
    if (primaryTerm != SequenceNumbers.UNASSIGNED_PRIMARY_TERM) {
        esEntity.asDocMeta().primaryTerm(primaryTerm);
    }

    return 1;
}
 
Example #6
Source File: ProviderParamsDao.java    From usergrid with Apache License 2.0 6 votes vote down vote up
/**
 * 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 vote down vote up
/**
 * 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: EsEventPersistence.java    From logsniffer with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public String persist(final Event event) {
	String evStr = null;
	try {
		evStr = jsonMapper.writeValueAsString(event);
		final IndexRequest indexRequest = Requests
				.indexRequest(indexNamingStrategy.buildActiveName(event.getSnifferId()))
				.type(getType(event.getSnifferId())).source(evStr);
		final String eventId = clientTpl.executeWithClient(new ClientCallback<IndexResponse>() {
			@Override
			public IndexResponse execute(final Client client) {
				return client.index(indexRequest).actionGet();
			}
		}).getId();
		logger.debug("Persisted event with id: {}", eventId);
		return eventId;
	} catch (final Exception e) {
		throw new DataAccessException("Failed to persiste event: " + evStr, e);
	}
}
 
Example #9
Source File: NoteDao.java    From usergrid with Apache License 2.0 6 votes vote down vote up
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: SearchService.java    From jakduk-api with MIT License 6 votes vote down vote up
public void indexDocumentGallery(EsGallery esGallery) {

		String id = esGallery.getId();

		try {
			IndexResponse response = client.prepareIndex()
					.setIndex(elasticsearchProperties.getIndexGallery())
					.setType(Constants.ES_TYPE_GALLERY)
					.setId(id)
					.setSource(ObjectMapperUtils.writeValueAsString(esGallery), XContentType.JSON)
					.get();

		} catch (IOException e) {
			throw new ServiceException(ServiceError.ELASTICSEARCH_INDEX_FAILED, e.getCause());
		}
	}
 
Example #11
Source File: CheckpointDaoTests.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
@Test
public void putModelCheckpoint_getIndexRequest() {
    checkpointDao.putModelCheckpoint(modelId, model);

    ArgumentCaptor<IndexRequest> indexRequestCaptor = ArgumentCaptor.forClass(IndexRequest.class);
    verify(clientUtil)
        .timedRequest(
            indexRequestCaptor.capture(),
            anyObject(),
            Matchers.<BiConsumer<IndexRequest, ActionListener<IndexResponse>>>anyObject()
        );
    IndexRequest indexRequest = indexRequestCaptor.getValue();
    assertEquals(indexName, indexRequest.index());
    assertEquals(CheckpointDao.DOC_TYPE, indexRequest.type());
    assertEquals(modelId, indexRequest.id());
    Set<String> expectedSourceKeys = new HashSet<String>(Arrays.asList(CheckpointDao.FIELD_MODEL, CheckpointDao.TIMESTAMP));
    assertEquals(expectedSourceKeys, indexRequest.sourceAsMap().keySet());
    assertEquals(model, indexRequest.sourceAsMap().get(CheckpointDao.FIELD_MODEL));
    assertNotNull(indexRequest.sourceAsMap().get(CheckpointDao.TIMESTAMP));
}
 
Example #12
Source File: MaprElasticSearchServiceBuilder.java    From mapr-music with Apache License 2.0 6 votes vote down vote up
@Override
public void callback(String documentId, JsonNode changes) {

    JsonNode allowed = copyOnlyAllowedFields(changes);

    if (allowed == null) {
        log.info("Document with id: '{}' was changed, but none of the fields are allowed to be sent to the ES",
                documentId);
        return;
    }

    IndexResponse response = client.prepareIndex(indexName, typeName, documentId)
            .setSource(allowed.toString(), XContentType.JSON)
            .get();

    log.info("Elasticsearch Index Response: '{}'", response);
}
 
Example #13
Source File: ElasticSearchManualTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenDocumentId_whenJavaObject_thenDeleteDocument() throws Exception {
    String jsonObject = "{\"age\":10,\"dateOfBirth\":1471455886564,\"fullName\":\"Johan Doe\"}";
    IndexRequest indexRequest = new IndexRequest("people");
    indexRequest.source(jsonObject, XContentType.JSON);

    IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
    String id = response.getId();

    GetRequest getRequest = new GetRequest("people");
    getRequest.id(id);

    GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
    System.out.println(getResponse.getSourceAsString());

    DeleteRequest deleteRequest = new DeleteRequest("people");
    deleteRequest.id(id);

    DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);

    assertEquals(Result.DELETED, deleteResponse.getResult());
}
 
Example #14
Source File: ElasticsearchDataModel.java    From elasticsearch-taste with Apache License 2.0 6 votes vote down vote up
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 #15
Source File: ElasticsearchTransportFactory.java    From database-transform-tool with Apache License 2.0 6 votes vote down vote up
public String insert(String index,String type,Object json){
	try {
		if(client==null){
			init();
		}
		IndexResponse response = client.prepareIndex(index, type).setSource(JSON.parseObject(JSON.toJSONString(json)),XContentType.JSON).execute().actionGet();
		if(response.getResult().equals(Result.CREATED)){
			System.out.println(JSON.toJSONString(response));
		}
		return response.toString();
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return null;
}
 
Example #16
Source File: TestTransportClient.java    From jframe with Apache License 2.0 6 votes vote down vote up
@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 #17
Source File: EsTest.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * ドキュメント登録チェックでドキュメントがすでに存在している場合に正常終了すること.
 * @throws ParseException ParseException
 */
@Test
public void ドキュメント登録チェックでドキュメントがすでに存在している場合に正常終了すること() throws ParseException {
    String id = "id00001";
    EsIndex index = esClient.idxAdmin("index_for_test");
    index.create();
    EsTypeImpl type = (EsTypeImpl) esClient.type(index.getName(), "TypeForTest", "TestRoutingId", 5, 500);
    assertNotNull(type);
    JSONObject data = (JSONObject) new JSONParser()
    .parse("{\"u\":1406596187938,\"t\":\"K0QK5DXWT5qKIPDU2eTdhA\",\"b\":\"IKv5hMRPRDGc68BnIcVx6g\","
            + "\"s\":{\"P003\":\"secondDynamicPropertyValue\",\"P002\":\"true\",\"P001\":\"false\","
            + "\"P011\":\"null\",\"P012\":\"123.0\",\"P007\":\"123\",\"P006\":\"false\",\"P005\":null,"
            + "\"P004\":\"dynamicPropertyValue\",\"P009\":\"123.123\",\"P008\":\"true\",\"__id\":\"userdata001:\","
            + "\"P010\":\"123.123\"},\"c\":\"Q1fp4zrWTm-gSSs7zVCJQg\",\"p\":1406596187938,"
            + "\"n\":\"vWy9OQj2ScykYize2d7Z5A\",\"l\":[],\"h\":{}}");
    type.create(id, data);
    IndexResponse response = type.checkDocumentCreated(id, data, null);
    assertNotNull(response);
    assertEquals(id, response.getId());
}
 
Example #18
Source File: BulkItemResponse.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public void writeTo(StreamOutput out) throws IOException {
    out.writeVInt(id);
    out.writeString(opType);

    if (response == null) {
        out.writeByte((byte) 2);
    } else {
        if (response instanceof IndexResponse) {
            out.writeByte((byte) 0);
        } else if (response instanceof DeleteResponse) {
            out.writeByte((byte) 1);
        } else if (response instanceof UpdateResponse) {
            out.writeByte((byte) 3); // make 3 instead of 2, because 2 is already in use for 'no responses'
        }
        response.writeTo(out);
    }
    if (failure == null) {
        out.writeBoolean(false);
    } else {
        out.writeBoolean(true);
        failure.writeTo(out);
    }
}
 
Example #19
Source File: EsTypeImpl.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * ドキュメントが登録されているかのチェックを行う.
 * @param id UUID
 * @param data Request body
 * @param ese ElasticSearchException
 * @return ES応答
 */
@SuppressWarnings("rawtypes")
protected IndexResponse checkDocumentCreated(String id, Map data, ElasticsearchException ese) {
    DcGetResponse getResponse = get(id);
    if (getResponse != null) {
        Object reqUpdated = data.get("u");
        Object getUpdated = getResponse.getSource().get("u");
        if (reqUpdated == null && getUpdated == null) {
            // uがnullになることはありえないが、静的チェックの指摘回避
            log.info("Request data is already registered. Then, return success response. But value is null");
            return new IndexResponse(indexName, getType(), id, 1, false);
        } else if ((reqUpdated != null && getUpdated != null) && reqUpdated.equals(getUpdated)) {
            log.info("Request data is already registered. Then, return success response.");
            return new IndexResponse(indexName, getType(), id, 1, false);
        }
    }
    throw new EsClientException("create failed", ese);
}
 
Example #20
Source File: BulkItemResponse.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * The index name of the action.
 */
public String getIndex() {
    if (failure != null) {
        return failure.getIndex();
    }
    if (response instanceof IndexResponse) {
        return ((IndexResponse) response).getIndex();
    } else if (response instanceof DeleteResponse) {
        return ((DeleteResponse) response).getIndex();
    } else if (response instanceof UpdateResponse) {
        return ((UpdateResponse) response).getIndex();
    }
    return null;
}
 
Example #21
Source File: ElasticClientHelper.java    From phoebus with Eclipse Public License 1.0 5 votes vote down vote up
public boolean indexAlarmCmdDocument(String indexName, AlarmCommandMessage alarmCommandMessage) {
    IndexRequest indexRequest = new IndexRequest(indexName.toLowerCase(), "alarm_cmd");
    try {
        indexRequest.source(alarmCommandMessage.sourceMap());
        IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
        return indexResponse.getResult().equals(Result.CREATED);
    } catch (IOException e) {
        logger.log(Level.SEVERE, "failed to log message " + alarmCommandMessage + " to index " + indexName, e);
        return false;
    }
}
 
Example #22
Source File: ElasticsearchAsyncIT.java    From glowroot with Apache License 2.0 5 votes vote down vote up
@Override
public void executeApp() throws Exception {
    client = Util.client(new InetSocketAddress("127.0.0.1", 9300));
    IndexResponse response = client.prepareIndex("testindex", "testtype")
            .setSource("abc", 11, "xyz", "some text")
            .get();
    documentId = response.getId();
    transactionMarker();
    client.close();
}
 
Example #23
Source File: ElasticsearchSyncIT.java    From glowroot with Apache License 2.0 5 votes vote down vote up
@Override
public void executeApp() throws Exception {
    client = Util.client(new InetSocketAddress("127.0.0.1", 9300));
    IndexResponse response = client.prepareIndex("testindex", "testtype")
            .setSource("abc", 11, "xyz", "some text")
            .get();
    documentId = response.getId();
    transactionMarker();
    client.close();
}
 
Example #24
Source File: ElasticsearchSyncIT.java    From glowroot with Apache License 2.0 5 votes vote down vote up
@Override
public void executeApp() throws Exception {
    client = Util.client(new InetSocketAddress("127.0.0.1", 9300));
    IndexResponse response = client.prepareIndex("testindex", "testtype")
            .setSource("abc", 11, "xyz", "some text")
            .get();
    documentId = response.getId();
    transactionMarker();
    client.close();
}
 
Example #25
Source File: BaseElasticSearchIndexBuilder.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
protected void executeBulkRequest(BulkRequestBuilder bulkRequest) {
    BulkResponse bulkResponse = bulkRequest.execute().actionGet();

    getLog().info("Bulk request of batch size: " + bulkRequest.numberOfActions() + " took "
            + bulkResponse.getTookInMillis() + " ms in index builder: " + getName());

    for (BulkItemResponse response : bulkResponse.getItems()) {
        if (response.getResponse() instanceof DeleteResponse) {
            DeleteResponse deleteResponse = response.getResponse();

            if (response.isFailed()) {
                getLog().error("Problem deleting doc: " + response.getId() + " in index builder: " + getName()
                        + " error: " + response.getFailureMessage());
            } else if (!deleteResponse.isFound()) {
                getLog().debug("ES could not find a doc with id: " + deleteResponse.getId()
                        + " to delete in index builder: " + getName());
            } else {
                getLog().debug("ES deleted a doc with id: " + deleteResponse.getId() + " in index builder: "
                        + getName());
            }
        } else if (response.getResponse() instanceof IndexResponse) {
            IndexResponse indexResponse = response.getResponse();

            if (response.isFailed()) {
                getLog().error("Problem updating content for doc: " + response.getId() + " in index builder: "
                        + getName() + " error: " + response.getFailureMessage());
            } else {
                getLog().debug("ES indexed content for doc with id: " + indexResponse.getId()
                        + " in index builder: " + getName());
            }
        }
    }
}
 
Example #26
Source File: GraphitePluginIntegrationTest.java    From elasticsearch-graphite-plugin with Do What The F*ck You Want To Public License 5 votes vote down vote up
@Test
public void testThatFieldExclusionWorks() throws Exception {
    String excludeRegex = ".*\\.peakCount";
    node = createNode(clusterName, GRAPHITE_SERVER_PORT, "1s", null, excludeRegex, null);

    IndexResponse indexResponse = indexElement(node, index, type, "value");
    assertThat(indexResponse.getId(), is(notNullValue()));

    Thread.sleep(2000);

    ensureValidKeyNames();
    // ensure no global exclusion
    assertGraphiteMetricIsContained("elasticsearch." + clusterName + ".indexes." + index + ".id.0.indexing._all.indexCount 1");
    assertGraphiteMetricIsNotContained("elasticsearch." + clusterName + ".node.jvm.threads.peakCount ");
}
 
Example #27
Source File: ElasticsearchAsyncIT.java    From glowroot with Apache License 2.0 5 votes vote down vote up
@Override
public void executeApp() throws Exception {
    client = Util.client(new InetSocketAddress("127.0.0.1", 9300));
    IndexResponse response = client.prepareIndex("testindex", "testtype")
            .setSource("abc", 11, "xyz", "some text")
            .get();
    documentId = response.getId();
    transactionMarker();
    client.close();
}
 
Example #28
Source File: ESPipeline.java    From spider with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void process(ResultItems resultItems, Task task) {
    Iterator i$ = resultItems.getAll().entrySet().iterator();
    try {
        XContentBuilder xContentBuilder = jsonBuilder().startObject();
        while (i$.hasNext()) {
            Map.Entry entry = (Map.Entry) i$.next();
            xContentBuilder.field((String) entry.getKey(), entry.getValue());
        }
        String json = xContentBuilder.endObject().string();
        IndexResponse response = null;
        if (StringUtils.isNotBlank(resultItems.get("id"))) {
            response = client
                    .prepareIndex(INDEX_NAME, TYPE_NAME, resultItems.get("id"))
                    .setSource(json).get();
        } else {
            response = client
                    .prepareIndex(INDEX_NAME, TYPE_NAME)
                    .setSource(json).get();
        }
        if (response.getResult() != IndexResponse.Result.CREATED)
            LOG.error("索引失败,可能重复创建,resultItem:" + resultItems);
    } catch (IOException e) {
        LOG.error("索引出错," + e.getLocalizedMessage());
        e.printStackTrace();
    }
}
 
Example #29
Source File: SpiderInfoDAO.java    From Gather-Platform with GNU General Public License v3.0 5 votes vote down vote up
@Override
public String index(SpiderInfo spiderInfo) {
    IndexResponse indexResponse;
    if (getByDomain(spiderInfo.getDomain(), 10, 1).size() > 0) {
        List<SpiderInfo> mayDuplicate = Lists.newLinkedList();
        List<SpiderInfo> temp;
        int i = 1;
        do {
            temp = getByDomain(spiderInfo.getDomain(), 100, i++);
            mayDuplicate.addAll(temp);
        } while (temp.size() > 0);
        if (mayDuplicate.indexOf(spiderInfo) != -1 && (spiderInfo = mayDuplicate.get(mayDuplicate.indexOf(spiderInfo))) != null) {
            LOG.warn("已经含有此模板,不再存储");
            return spiderInfo.getId();
        }
    }
    try {
        indexResponse = client.prepareIndex(INDEX_NAME, TYPE_NAME)
                .setSource(gson.toJson(spiderInfo))
                .get();
        LOG.debug("索引爬虫模板成功");
        return indexResponse.getId();
    } catch (Exception e) {
        LOG.error("索引 Webpage 出错," + e.getLocalizedMessage());
    }
    return null;
}
 
Example #30
Source File: ElasticsearchUtil.java    From adaptive-alerting with Apache License 2.0 5 votes vote down vote up
public IndexResponse index(IndexRequest indexRequest, String json) {
    try {
        indexRequest.source(json, XContentType.JSON);
        return legacyElasticSearchClient.index(indexRequest, RequestOptions.DEFAULT);
    } catch (IOException e) {
        log.error(String.format("Indexing mapping %s failed", json), e);
        throw new RuntimeException(e);
    }
}