Search in sources :

Example 1 with GetFieldMappingsResponse

use of org.elasticsearch.client.indices.GetFieldMappingsResponse in project ARLAS-server by gisaia.

the class ElasticTool method isDateField.

public static boolean isDateField(String field, ElasticClient client, String index) throws ArlasException {
    GetFieldMappingsResponse response = client.getFieldMapping(index, field);
    String lastKey = field.substring(field.lastIndexOf(".") + 1);
    return response.mappings().keySet().stream().anyMatch(indexName -> {
        GetFieldMappingsResponse.FieldMappingMetadata data = response.fieldMappings(indexName, field);
        boolean isFieldMetadaAMap = (data != null && data.sourceAsMap().get(lastKey) instanceof Map);
        if (isFieldMetadaAMap) {
            return Optional.of(((Map) data.sourceAsMap().get(lastKey))).map(m -> m.get(ES_TYPE)).map(Object::toString).filter(t -> t.equals(ES_DATE_TYPE)).isPresent();
        } else {
            // TODO : check if there is another way to fetch field type in this case
            return false;
        }
    });
}
Also used : RestClient(org.elasticsearch.client.RestClient) StringUtil(io.arlas.server.core.utils.StringUtil) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) ElasticConfiguration(io.arlas.server.core.app.ElasticConfiguration) Strings(org.elasticsearch.common.Strings) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) LinkedHashMap(java.util.LinkedHashMap) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) ArlasException(io.arlas.server.core.exceptions.ArlasException) NodeSelector(org.elasticsearch.client.NodeSelector) Sniffer(org.elasticsearch.client.sniff.Sniffer) Map(java.util.Map) ElasticsearchNodesSniffer(org.elasticsearch.client.sniff.ElasticsearchNodesSniffer) SniffOnFailureListener(org.elasticsearch.client.sniff.SniffOnFailureListener) CollectionReference(io.arlas.server.core.model.CollectionReference) Joda(org.elasticsearch.common.joda.Joda) NotFoundException(io.arlas.server.core.exceptions.NotFoundException) InternalServerErrorException(io.arlas.server.core.exceptions.InternalServerErrorException) GetFieldMappingsResponse(org.elasticsearch.client.indices.GetFieldMappingsResponse) IOException(java.io.IOException) AcknowledgedResponse(org.elasticsearch.action.support.master.AcknowledgedResponse) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) InputStreamReader(java.io.InputStreamReader) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) NodesSniffer(org.elasticsearch.client.sniff.NodesSniffer) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) AuthScope(org.apache.http.auth.AuthScope) CreateIndexResponse(org.elasticsearch.client.indices.CreateIndexResponse) Optional(java.util.Optional) CollectionUtil(io.arlas.server.core.utils.CollectionUtil) IOUtils(org.apache.logging.log4j.core.util.IOUtils) CredentialsProvider(org.apache.http.client.CredentialsProvider) HttpHost(org.apache.http.HttpHost) InputStream(java.io.InputStream) GetFieldMappingsResponse(org.elasticsearch.client.indices.GetFieldMappingsResponse) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with GetFieldMappingsResponse

use of org.elasticsearch.client.indices.GetFieldMappingsResponse in project jackrabbit-oak by apache.

the class ElasticSimilarQueryTest method vectorSimilarityElastiknnIndexConfiguration.

@Test
public void vectorSimilarityElastiknnIndexConfiguration() throws Exception {
    final String indexName = "test1";
    final String fieldName1 = "fv1";
    final String similarityFieldName1 = FieldNames.createSimilarityFieldName(fieldName1);
    IndexDefinitionBuilder builder = createIndex(fieldName1);
    Tree tree = builder.indexRule("nt:base").property(fieldName1).useInSimilarity(true).nodeScopeIndex().similaritySearchDenseVectorSize(2048).getBuilderTree();
    tree.setProperty(ElasticPropertyDefinition.PROP_INDEX_SIMILARITY, "cosine");
    tree.setProperty(ElasticPropertyDefinition.PROP_NUMBER_OF_HASH_TABLES, 10);
    tree.setProperty(ElasticPropertyDefinition.PROP_NUMBER_OF_HASH_FUNCTIONS, 12);
    setIndex(indexName, builder);
    root.commit();
    String alias = ElasticIndexNameHelper.getElasticSafeIndexName(esConnection.getIndexPrefix(), "/oak:index/" + indexName);
    GetFieldMappingsRequest fieldMappingsRequest = new GetFieldMappingsRequest();
    fieldMappingsRequest.indices(alias).fields(similarityFieldName1);
    GetFieldMappingsResponse mappingsResponse = esConnection.getClient().indices().getFieldMapping(fieldMappingsRequest, RequestOptions.DEFAULT);
    final Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetadata>> mappings = mappingsResponse.mappings();
    assertEquals("More than one index found", 1, mappings.keySet().size());
    @SuppressWarnings("unchecked") Map<String, Object> map1 = (Map<String, Object>) (((Map<String, Object>) mappings.entrySet().iterator().next().getValue().get(similarityFieldName1).sourceAsMap().get(similarityFieldName1)).get("elastiknn"));
    assertEquals("Dense vector size doesn't match", 2048, (int) map1.get("dims"));
    assertEquals("Similarity doesn't match", "cosine", map1.get("similarity"));
    assertEquals("Similarity doesn't match", 10, map1.get("L"));
    assertEquals("Similarity doesn't match", 12, map1.get("k"));
}
Also used : IndexDefinitionBuilder(org.apache.jackrabbit.oak.plugins.index.search.util.IndexDefinitionBuilder) Tree(org.apache.jackrabbit.oak.api.Tree) GetFieldMappingsRequest(org.elasticsearch.client.indices.GetFieldMappingsRequest) GetFieldMappingsResponse(org.elasticsearch.client.indices.GetFieldMappingsResponse) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

Map (java.util.Map)2 GetFieldMappingsResponse (org.elasticsearch.client.indices.GetFieldMappingsResponse)2 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)1 ElasticConfiguration (io.arlas.server.core.app.ElasticConfiguration)1 ArlasException (io.arlas.server.core.exceptions.ArlasException)1 InternalServerErrorException (io.arlas.server.core.exceptions.InternalServerErrorException)1 NotFoundException (io.arlas.server.core.exceptions.NotFoundException)1 CollectionReference (io.arlas.server.core.model.CollectionReference)1 CollectionUtil (io.arlas.server.core.utils.CollectionUtil)1 StringUtil (io.arlas.server.core.utils.StringUtil)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Optional (java.util.Optional)1 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)1 HttpHost (org.apache.http.HttpHost)1 AuthScope (org.apache.http.auth.AuthScope)1 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)1