Search in sources :

Example 86 with IndexResponse

use of org.elasticsearch.action.index.IndexResponse in project bw-calendar-engine by Bedework.

the class BwIndexEsImpl method indexEvent.

private IndexResponse indexEvent(final EventInfo ei) throws CalFacadeException {
    try {
        /* If it's not recurring or a stand-alone instance index it */
        final BwEvent ev = ei.getEvent();
        if (!ev.testRecurring() && (ev.getRecurrenceId() == null)) {
            return indexEvent(ei, ItemKind.master, ev.getDtstart(), ev.getDtend(), // ev.getRecurrenceId(),
            null, null);
        }
        if (ev.getRecurrenceId() != null) {
            error("Not implemented - index of single override");
            return null;
        }
        /* Delete all instances of this event: we'll do a delete by query
       * We need to find all with the same path and uid.
       */
        /* TODO - do a query for all recurrence ids and delete the ones
          we don't want.
       */
        deleteEvent(ei);
        /* Create a list of all instance date/times before overrides. */
        final int maxYears;
        final int maxInstances;
        final DateLimits dl = new DateLimits();
        if (ev.getPublick()) {
            maxYears = unauthpars.getMaxYears();
            maxInstances = unauthpars.getMaxInstances();
        } else {
            maxYears = authpars.getMaxYears();
            maxInstances = authpars.getMaxInstances();
        }
        final RecurPeriods rp = RecurUtil.getPeriods(ev, maxYears, maxInstances);
        if (rp.instances.isEmpty()) {
            // No instances for an alleged recurring event.
            return null;
        // throw new CalFacadeException(CalFacadeException.noRecurrenceInstances);
        }
        final String stzid = ev.getDtstart().getTzid();
        int instanceCt = maxInstances;
        final boolean dateOnly = ev.getDtstart().getDateType();
        /* First build a table of overrides so we can skip these later
       */
        final Map<String, String> overrides = new HashMap<>();
        /*
      if (!Util.isEmpty(ei.getOverrideProxies())) {
        for (BwEvent ov: ei.getOverrideProxies()) {
          overrides.put(ov.getRecurrenceId(), ov.getRecurrenceId());
        }
      }
      */
        final IndexResponse iresp;
        if (!Util.isEmpty(ei.getOverrides())) {
            for (final EventInfo oei : ei.getOverrides()) {
                final BwEvent ov = oei.getEvent();
                overrides.put(ov.getRecurrenceId(), ov.getRecurrenceId());
                final String start;
                if (ov.getDtstart().getDateType()) {
                    start = ov.getRecurrenceId().substring(0, 8);
                } else {
                    start = ov.getRecurrenceId();
                }
                final BwDateTime rstart = BwDateTime.makeBwDateTime(ov.getDtstart().getDateType(), start, stzid);
                final BwDateTime rend = rstart.addDuration(BwDuration.makeDuration(ov.getDuration()));
                /*iresp = */
                indexEvent(oei, ItemKind.override, rstart, rend, ov.getRecurrenceId(), dl);
                instanceCt--;
            }
        }
        for (final Period p : rp.instances) {
            String dtval = p.getStart().toString();
            if (dateOnly) {
                dtval = dtval.substring(0, 8);
            }
            final BwDateTime rstart = BwDateTime.makeBwDateTime(dateOnly, dtval, stzid);
            if (overrides.get(rstart.getDate()) != null) {
                // Overrides indexed separately - skip this instance.
                continue;
            }
            final String recurrenceId = rstart.getDate();
            dtval = p.getEnd().toString();
            if (dateOnly) {
                dtval = dtval.substring(0, 8);
            }
            final BwDateTime rend = BwDateTime.makeBwDateTime(dateOnly, dtval, stzid);
            /*iresp = */
            indexEvent(ei, entity, rstart, rend, recurrenceId, dl);
            instanceCt--;
            if (instanceCt == 0) {
                // That's all you're getting from me
                break;
            }
        }
        // </editor-fold>
        // <editor-fold desc="Emit the master event with a date range covering the entire period.">
        final BwDateTime start = BwDateTime.makeBwDateTime(dateOnly, dl.minStart, stzid);
        final BwDateTime end = BwDateTime.makeBwDateTime(dateOnly, dl.maxEnd, stzid);
        iresp = indexEvent(ei, ItemKind.master, start, end, null, null);
        return iresp;
    } catch (final CalFacadeException cfe) {
        throw cfe;
    } catch (final Throwable t) {
        throw new CalFacadeException(t);
    }
}
Also used : EventInfo(org.bedework.calfacade.svc.EventInfo) BwDateTime(org.bedework.calfacade.BwDateTime) HashMap(java.util.HashMap) Period(net.fortuna.ical4j.model.Period) BwEvent(org.bedework.calfacade.BwEvent) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse) DeleteIndexResponse(org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) RecurPeriods(org.bedework.icalendar.RecurUtil.RecurPeriods)

Example 87 with IndexResponse

use of org.elasticsearch.action.index.IndexResponse in project bw-calendar-engine by Bedework.

the class BwIndexEsImpl method indexEntity.

@Override
public void indexEntity(final Object rec) throws CalFacadeException {
    try {
        /* XXX later with batch
      XmlEmit xml;

      if (batchMaxSize > 0) {
        synchronized (batchLock) {

          if (batchCurSize == 0) {
            batch = new XmlEmit();
            xmlWtr = new StringWriter();
            batch.startEmit(xmlWtr);

            batch.openTag(solrTagAdd);
          }

          index(batch, rec);

          if (batchMaxSize == batchCurSize) {
            batch.closeTag(solrTagAdd);
            indexAndCommit(xmlWtr.toString());
            batchCurSize = 0;
          }
        }

        return;
      }
      */
        // Unbatched
        markUpdated(docTypeFromClass(rec.getClass()));
        final IndexResponse resp = index(rec);
        if (debug) {
            if (resp == null) {
                debug("IndexResponse: resp=null");
            } else {
                debug("IndexResponse: index=" + resp.getIndex() + " id=" + resp.getId() + " type=" + resp.getType() + " version=" + resp.getVersion());
            }
        }
    } catch (final Throwable t) {
        if (debug) {
            error(t);
        }
        throw new CalFacadeException(t);
    } finally {
        lastIndexTime = System.currentTimeMillis();
    }
}
Also used : CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse) DeleteIndexResponse(org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Example 88 with IndexResponse

use of org.elasticsearch.action.index.IndexResponse in project tutorials by eugenp.

the class ElasticSearchManualTest method givenDocumentId_whenJavaObject_thenDeleteDocument.

@Test
public void givenDocumentId_whenJavaObject_thenDeleteDocument() {
    String jsonObject = "{\"age\":10,\"dateOfBirth\":1471455886564,\"fullName\":\"Johan Doe\"}";
    IndexResponse response = client.prepareIndex("people", "Doe").setSource(jsonObject).get();
    String id = response.getId();
    DeleteResponse deleteResponse = client.prepareDelete("people", "Doe", id).get();
    assertTrue(deleteResponse.isFound());
}
Also used : DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) Test(org.junit.Test)

Example 89 with IndexResponse

use of org.elasticsearch.action.index.IndexResponse in project tutorials by eugenp.

the class ElasticSearchManualTest method givenJsonString_whenJavaObject_thenIndexDocument.

@Test
public void givenJsonString_whenJavaObject_thenIndexDocument() {
    String jsonObject = "{\"age\":20,\"dateOfBirth\":1471466076564,\"fullName\":\"John Doe\"}";
    IndexResponse response = client.prepareIndex("people", "Doe").setSource(jsonObject).get();
    String index = response.getIndex();
    String type = response.getType();
    assertTrue(response.isCreated());
    assertEquals(index, "people");
    assertEquals(type, "Doe");
}
Also used : IndexResponse(org.elasticsearch.action.index.IndexResponse) Test(org.junit.Test)

Example 90 with IndexResponse

use of org.elasticsearch.action.index.IndexResponse in project tutorials by eugenp.

the class GeoQueriesTest method givenGeoPointData_whenExecutedGeoDistanceQuery_thenResultNonEmpty.

@Test
public void givenGeoPointData_whenExecutedGeoDistanceQuery_thenResultNonEmpty() {
    String jsonObject = "{\"name\":\"Lighthouse of alexandria\",\"location\":[31.131302,29.976480]}";
    IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS).setSource(jsonObject).get();
    String lighthouseOfAlexandriaId = response.getId();
    client.admin().indices().prepareRefresh(WONDERS_OF_WORLD).get();
    QueryBuilder qb = QueryBuilders.geoDistanceQuery("location").point(29.976, 31.131).distance(10, DistanceUnit.MILES);
    SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD).setTypes(WONDERS).setQuery(qb).execute().actionGet();
    List<String> ids = Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId).collect(Collectors.toList());
    assertTrue(ids.contains(lighthouseOfAlexandriaId));
}
Also used : IndexResponse(org.elasticsearch.action.index.IndexResponse) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) Test(org.junit.Test)

Aggregations

IndexResponse (org.elasticsearch.action.index.IndexResponse)103 Test (org.junit.Test)26 SearchResponse (org.elasticsearch.action.search.SearchResponse)18 IOException (java.io.IOException)15 CreateIndexResponse (org.elasticsearch.action.admin.indices.create.CreateIndexResponse)15 HashMap (java.util.HashMap)13 DeleteResponse (org.elasticsearch.action.delete.DeleteResponse)13 IndexRequest (org.elasticsearch.action.index.IndexRequest)13 ElasticsearchException (org.elasticsearch.ElasticsearchException)11 CountDownLatch (java.util.concurrent.CountDownLatch)10 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)8 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)7 Settings (org.elasticsearch.common.settings.Settings)7 ArrayList (java.util.ArrayList)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 DeleteIndexResponse (org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse)6 ExecutionException (java.util.concurrent.ExecutionException)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)5