Untitled

mail@pastecode.io avatar
unknown
plain_text
15 days ago
3.4 kB
3
Indexable
Never
package com.ericsson.msdp.spg.repositories;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.mapping.DynamicMapping;
import co.elastic.clients.elasticsearch._types.mapping.Property;
import co.elastic.clients.elasticsearch._types.mapping.SearchAsYouTypeProperty;
import co.elastic.clients.elasticsearch._types.mapping.TypeMapping;
import co.elastic.clients.elasticsearch.core.SearchRequest;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.Hit;
import co.elastic.clients.elasticsearch.core.search.TotalHitsRelation;
import co.elastic.clients.util.ObjectBuilder;
import com.ericsson.msdp.spg.model.SiteNode;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;

public class SitesRepo extends BaseRepo<SiteNode, SitesRepo> {

  public SitesRepo(String nodeIndex, ElasticsearchClient elasticsearchClient) {
    super(nodeIndex, elasticsearchClient, SiteNode.class);
  }

  @Override
  protected ObjectBuilder<TypeMapping> indexMapping(TypeMapping.Builder builder) {
    Function<Property.Builder, ObjectBuilder<Property>> fn = b -> {
      b.searchAsYouType(SearchAsYouTypeProperty.of(a -> a.index(true)));
      b.keyword(a -> a.store(true));
      return b;
    };

    Function<Property.Builder, ObjectBuilder<Property>> number = b -> {
      b.integer(f -> f.index(true));
      return b;
    };

    return builder.dynamic(DynamicMapping.False)
        .properties("id", fn)
        .properties("eai_id", number)
        .properties("eai_type", number)
        .properties("name", fn)
        .properties("type", fn)
        .properties("subtype", fn)
        .properties("region", fn)
        .properties("status", fn)
        .properties("vendor", fn)
        .properties("location", b -> b.geoPoint(g -> g.ignoreZValue(true)))
        .properties("attributes.name", fn)
        .properties("attributes.value", fn)
        .properties("npa_nxx", fn)
        .properties("city", fn)
        .properties("state_prov", fn);
  }

  protected List<FieldType> searchableFields() {
    return List.of(
        new FieldType("name", "string", Collections.emptyMap()),
        new FieldType("type", "select", Collections.emptyMap()),
        new FieldType("subtype", "select", Collections.emptyMap()),
        new FieldType("status", "select", Collections.emptyMap()),
        new FieldType("vendor", "select", Collections.emptyMap()));
  }

  public Optional<SiteNode> queryByName(String siteName) {
    try {
      Objects.requireNonNull(siteName, "Site name cannot be null");
      SearchRequest request = SearchRequest
          .of(s -> s.index(getIndexName()).size(10_000).query(q -> q.match(t -> t.field("name").query(siteName))));
      SearchResponse<SiteNode> response = getElasticsearchClient().search(request, SiteNode.class);
      if (response.hits().total().relation() != TotalHitsRelation.Eq) {
        logger.warn("There are more results");
      }
      return response.hits().hits().stream().map(Hit::source).findFirst();
    } catch (Exception e) {
      throw new RepositoryException(e);
    }
  }

  public Long lastModifiedTimestamp() {
    return null;
  }
}
Leave a Comment