Creates and prepares an {@link IndexRequestBuilder} from the supplied
{@link Client} via delegation to the subclass-hook template methods
{@link #getIndexName(String, long)} and
{@link #prepareIndexRequest(IndexRequestBuilder, String, String, Event)}
438489
@Override
  public IndexRequestBuilder createIndexRequest(Client client,
        String indexPrefix, String indexType, Event event) throws IOException {
    IndexRequestBuilder request = prepareIndex(client);
    String realIndexPrefix = BucketPath.escapeString(indexPrefix, event.getHeaders());
    String realIndexType = BucketPath.escapeString(indexType, event.getHeaders());

    TimestampedEvent timestampedEvent = new TimestampedEvent(event);
    long timestamp = timestampedEvent.getTimestamp();

    String indexName = getIndexName(realIndexPrefix, timestamp);
    prepareIndexRequest(request, indexName, realIndexType, timestampedEvent);
    return request;
  }
1329999
public Optional<IndexFieldTypesDTO> pollIndex(final String indexName, final String indexSetId) {
        final GetMapping getMapping = new GetMapping.Builder()
                .addIndex(indexName)
                .build();

        final JestResult result;
        try (final Timer.Context ignored = pollTimer.time()) {
            result = JestUtils.execute(jestClient, getMapping, () -> "Unable to get index mapping for index: " + indexName);
        } catch (Exception e) {
            if (LOG.isDebugEnabled()) {
                LOG.error("Couldn't get mapping for index <{}>", indexName, e);
            } else {
                LOG.error("Couldn't get mapping for index <{}>: {}", indexName, ExceptionUtils.getRootCauseMessage(e));
            }
            return Optional.empty();
        }

        final JsonNode properties = result.getJsonObject()
                .path(indexName)
                .path("mappings")
                .path("message") // TODO: Hardcoded index type name
                .path("properties");

        if (properties.isMissingNode()) {
            LOG.error("Invalid mapping response: {}", result.getJsonString());
            return Optional.empty();
        }

        final Spliterator<Map.Entry<String, JsonNode>> fieldSpliterator = Spliterators.spliteratorUnknownSize(properties.fields(), Spliterator.IMMUTABLE);

        final Set<FieldTypeDTO> fieldsMap = StreamSupport.stream(fieldSpliterator, false)
                .map(field -> Maps.immutableEntry(field.getKey(), field.getValue().path("type").asText()))
                // The "type" value is empty if we deal with a nested data type
                // TODO: Figure out how to handle nested fields, for now we only support the top-level fields
                .filter(field -> !field.getValue().isEmpty())
                .map(field -> FieldTypeDTO.create(field.getKey(), field.getValue()))
                .collect(Collectors.toSet());

        return Optional.of(IndexFieldTypesDTO.create(indexSetId, indexName, fieldsMap));
    }
1281926
@CheckReturnValue
  public static <ReqT, RespT> Builder<ReqT, RespT> newBuilder(
      Marshaller<ReqT> requestMarshaller, Marshaller<RespT> responseMarshaller) {
    return new Builder<ReqT, RespT>()
        .setRequestMarshaller(requestMarshaller)
        .setResponseMarshaller(responseMarshaller);
  }
171457
public <T> T build(final AwsClientBuilder builder, final Class<T> clientType) {
        val cfg = new ClientConfiguration();
        try {
            val localAddress = getSetting("localAddress");
            if (StringUtils.isNotBlank(localAddress)) {
                cfg.setLocalAddress(InetAddress.getByName(localAddress));
            }
        } catch (final Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
        builder.withClientConfiguration(cfg);

        val key = getSetting("credentialAccessKey");
        val secret = getSetting("credentialSecretKey");
        val credentials = ChainingAWSCredentialsProvider.getInstance(key, secret);
        builder.withCredentials(credentials);

        var region = getSetting("region");
        val currentRegion = Regions.getCurrentRegion();
        if (currentRegion != null && StringUtils.isBlank(region)) {
            region = currentRegion.getName();
        }
        var regionOverride = getSetting("regionOverride");
        if (currentRegion != null && StringUtils.isNotBlank(regionOverride)) {
            regionOverride = currentRegion.getName();
        }
        val finalRegion = StringUtils.defaultIfBlank(regionOverride, region);
        if (StringUtils.isNotBlank(finalRegion)) {
            builder.withRegion(finalRegion);
        }

        val endpoint = getSetting("endpoint");
        if (StringUtils.isNotBlank(endpoint)) {
            builder.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, finalRegion));
        }

        val result = builder.build();
        return clientType.cast(result);
    }
1110374
@Override
    protected List<IndexRequestBuilder> toRequest(JCas jCas, Client client,
            String indexName) throws IOException {
        List<IndexRequestBuilder> requests = newArrayList();

        String pmId = getHeaderDocId(jCas);

        try {
            Collection<Neuron> neurons = select(jCas, Neuron.class);

            // UIMA CAS indexes so that we build them once for all sentences.
            Map<AnnotationFS, Collection<AnnotationFS>> idxNeuronProperties = indexCovered(
                    jCas.getCas(), //
                    getType(jCas, Neuron.class),
                    getType(jCas, NeuronProperty.class));
            Map<AnnotationFS, Collection<AnnotationFS>> idxNeurons = indexCovered(
                    jCas.getCas(), //
                    getType(jCas, Neuron.class),
                    getType(jCas, NeuronTrigger.class));

            // only index abstracts that contain a neuron
            if (!(onlyIndexNeurons && neurons.isEmpty())) {

                final XContentBuilder doc = jsonBuilder().startObject();
                doc.field(FIELD_PUBMED_ID, pmId);
                doc.field(FIELD_SENTENCE_TEXT, jCas.getDocumentText());
                // authors & date
                try {
                    List<String> authors = new ArrayList<>();
                    Header header = selectSingle(jCas, Header.class);
                    for (int i = 0; i < header.getAuthors().size(); i++) {
                        AuthorInfo a = header.getAuthors(i);
                        authors.add(a.getForeName() + " " + a.getLastName());
                    }
                    doc.field(FIELD_AUTHORS, authors);

                    String pubDate = header.getCopyright();// 1976-01-16
                    if (pubDate.indexOf("-") > -1) {
                        doc.field(FIELD_PUBLISHED_DATE, pubDate);
                    }
                } catch (Exception e) {// nope
                    LOG.debug("failed to index authors or date for {}: {}",
                            pmId, e.toString());
                }

                // INDEX NEURONS
                doc.startArray("neuron");
                for (Neuron nwp : neurons) {
                    doc.startObject();
                    doc.field(FIELD_NEURON_TEXT, nwp.getCoveredText());
                    doc.field(FIELD_START, nwp.getBegin());
                    doc.field(FIELD_END, nwp.getEnd());

                    // properties
                    doc.startArray("neuron_properties");
                    // neuron(trigger), handle as it were a property
                    NeuronTrigger n = ((NeuronTrigger) (idxNeurons.get(nwp)
                            .iterator().next()));
                    doc.startObject();
                    doc.field(FIELD_NEURON_TYPE, getName(n));
                    doc.field(FIELD_PROPERTY_TEXT, n.getCoveredText());
                    doc.field(FIELD_START, n.getBegin());
                    doc.field(FIELD_END, n.getEnd());
                    doc.endObject(); // end 'n' (neuron trigger)

                    // every NeuronProperty
                    if (idxNeuronProperties.containsKey(nwp)) {
                        for (AnnotationFS np : idxNeuronProperties.get(nwp)) {
                            doc.startObject();
                            doc.field(FIELD_NEURON_TYPE, getName(np));
                            doc.field(FIELD_PROPERTY_TEXT, np.getCoveredText());
                            doc.field(FIELD_START, np.getBegin());
                            doc.field(FIELD_END, np.getEnd());
                            try {
                                String id = ((NeuronProperty) np)
                                        .getOntologyId();
                                if (id != null)
                                    doc.field(FIELD_ONTOLOGY_ID, id);
                            } catch (Exception e) {// nope
                            }
                            doc.endObject();
                        }
                    }
                    doc.endArray(); // end 'properties'
                    doc.endObject(); // end nwp
                }
                doc.endArray(); // end 'neuron'

                // INDEX SEPARATE PROPERTIES
                doc.startArray("all_neuron_properties");
                for (NeuronProperty np : select(jCas, NeuronProperty.class)) {
                    doc.startObject();
                    doc.field(FIELD_PROPERTY_TEXT, np.getCoveredText());
                    doc.field(FIELD_NEURON_TYPE, getName(np));
                    doc.field(FIELD_START, np.getBegin());
                    doc.field(FIELD_END, np.getEnd());
                    try {
                        String id = np.getOntologyId();
                        if (id != null)
                            doc.field(FIELD_ONTOLOGY_ID, id);
                    } catch (Exception e) {// nope
                    }
                    doc.endObject();
                }
                doc.endArray();

                String uid = pmId;// + "_" + sentenceStart; // uid for Lucene
                requests.add(client.prepareIndex(indexName, "abstract_", uid)
                        .setSource(doc));

                LOG.trace(doc.string());
            }

        } catch (Exception e) {
            LOG.warn("could not index pmid" + pmId, e);
        }
        return requests;
    }
175153
public Index createResourceIndex() throws Exception {
        if (!ResourceIndex.INSTANCE.isLoaded()) {
            // Load resource index defined by the BELFramework instance
            final SystemConfiguration sysConfig = getSystemConfiguration();
            final String resourceIndexURL = sysConfig.getResourceIndexURL();
            File indexFile = new File(resourceIndexURL);
            if (!indexFile.exists() || !indexFile.canRead()) {
                // try the index as an online resource.
                ResolvedResource resolvedResource = cacheService
                        .resolveResource(ResourceType.RESOURCE_INDEX,
                                resourceIndexURL);
                indexFile = resolvedResource.getCacheResourceCopy();
            }

            ResourceIndex.INSTANCE.loadIndex(indexFile);
        }
        return ResourceIndex.INSTANCE.getIndex();
    }
1213062
public static XContentBuilder marshall(ClientVersionBean bean) throws StorageException {
        try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
            ClientBean client = bean.getClient();
            OrganizationBean org = client.getOrganization();
            preMarshall(bean);
            builder
                .startObject()
                    .field("organizationId", org.getId())
                    .field("organizationName", org.getName())
                    .field("clientId", client.getId())
                    .field("clientName", client.getName())
                    .field("clientDescription", client.getDescription())
                    .field("version", bean.getVersion())
                    .field("apikey", bean.getApikey())
                    .field("status", bean.getStatus())
                    .field("createdBy", bean.getCreatedBy())
                    .field("createdOn", bean.getCreatedOn().getTime())
                    .field("modifiedBy", bean.getModifiedBy())
                    .field("modifiedOn", bean.getModifiedOn().getTime())
                    .field("publishedOn", bean.getPublishedOn() != null ? bean.getPublishedOn().getTime() : null)
                    .field("retiredOn", bean.getRetiredOn() != null ? bean.getRetiredOn().getTime() : null)
                .endObject();
            postMarshall(bean);
            return builder;
        } catch (IOException e) {
            throw new StorageException(e);
        }
    }
1099899
public SearchRequestBuilder getSearchRequestBuilder(QueryBuilder
            queryBuilder, String... indices) {
        return getSearchRequestBuilder(indices, null, queryBuilder);
    }
1315677
private void initializeSecondaryIndexer() {
    SecondaryIndex secondaryIndexAnnotation = field.getAnnotation(SecondaryIndex.class);
    if (secondaryIndexAnnotation == null) {
      return;
    }
    String indexName = secondaryIndexAnnotation.name();
    if (indexName == null || indexName.trim().length() == 0) {
      indexName = DEFAULT_SECONDARY_INDEX_PREFIX + mappedName;
    }
    this.secondaryIndexName = indexName;
    try {
      secondaryIndexer = IndexerFactory.getInstance().getIndexer(field);
    } catch (Exception exp) {
      String pattern = "No suitable Indexer found or error occurred while creating the indexer "
          + "for field %s in class %s";
      String message = String.format(pattern, field.getName(), field.getDeclaringClass().getName());
      throw new EntityManagerException(message, exp);
    }

  }
669332
public String toCreateIndexDDL(final String indexName, final String indexType) {
    final StringBuilder ddl = new StringBuilder("create index ");
    ddl.append(indexName).append(' ').append(indexType).append(' ');
    ddl.append("runtime ").append(serializer.getId());
    return ddl.toString();
  }
522007
public Client<? extends Options, ? extends OptionsBuilder, ? extends RequestBuilder> newClient() {
        if (clientClassName == null) {
            return new DefaultClient();
        } else {
            try {
                return (Client) Thread.currentThread().getContextClassLoader().loadClass(clientClassName).newInstance();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
263123
private boolean createIndex(String index, Map<String, Object> defaultSettings) {
        IndicesExistsResponse res = client.admin().indices().prepareExists(index).execute().actionGet();
        boolean created = false;
        if (!res.isExists()) {
            CreateIndexRequestBuilder req = client.admin().indices().prepareCreate(index);
            req.setSettings(defaultSettings);
            created = req.execute().actionGet().isAcknowledged();
            if (!created) {
                throw new RuntimeException("Could not create index [" + index + "]");
            }
        }

        return created;
    }
806845
private void createIndexesTemplates() {
        try {
            initIndexesTemplates();
            updateIndexesNames();
            Executors.newScheduledThreadPool(1).scheduleAtFixedRate(this::updateIndexesNames, 0, 1, TimeUnit.HOURS);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
769872
public SearchRequestBuilder getSearchRequestBuilderWithMatchAll(
            boolean isSetExplain, String[] indices, String[] types) {
        return getSearchRequestBuilderWithMatchAll(isSetExplain, indices, types,
                null, null);
    }
425382
@Override
  public BindingBuilder<T> to(Class<? super T> api)
  {
    Objects.requireNonNull(api);
    
    _api = api;
    
    _builder.to(api);

    return this;
  }
722930
@Override
  public List<ApiType> byIndex(String indexName, String indexKey) {
    lock.lock();
    try {
      if (!this.indexers.containsKey(indexName)) {
        throw new IllegalArgumentException(String.format("index %s doesn't exist!", indexName));
      }
      Map<String, Set<String>> index = this.indices.get(indexName);
      Set<String> set = index.get(indexKey);
      if (set == null) {
        return Arrays.asList();
      }
      List<ApiType> items = new ArrayList<>(set.size());
      for (String key : set) {
        items.add(this.items.get(key));
      }
      return items;
    } finally {
      lock.unlock();
    }
  }
555625
public static ClientRequestContextBuilder of(RpcRequest request, String uri) {
        return of(request, URI.create(requireNonNull(uri, "uri")));
    }