Spring Data Elasticsearch - How to get mapping for a field - elasticsearch

Wondering how to do this in java using spring data elasticsearch library.
GET /my-index-000001/_mapping/field/user

This is not supported by Spring Data Elasticsearch. You'll have to get the mapping for the index and extract the part you need from the returned Map<String, Object>.

Related

Delete data from nested fields in ES via java springboot

How to delete data based on condition from elastic search index using RestHighLevelClient in spring boot
The stackoverflow above describe how to remove main_phone1=1 from companyphones[] array inside the ES document via 'painless'
Anyone know how to remove data from fields[] from ES document without using 'painless' script inside the java code. For example construct the QueryBuilder and using esresttemplate to invoke the delete data?
currently I am using springboot + springdataelasticsearch jar + mvn
Thank you

_doc + Spring data elasticsearch + find document

How to convert below query to Spring Data elasticsearch
GET indexName/_doc/{id}?_source_includes=jsonField1,jsonField2
source filtering for get requests is currently not supported in Spring Data Elasticsearch.
Edit 09.12.2021:
If you don't need source filtering, to get a Document by id you can either use
ElasticsearchOperations#get(java.lang.String, java.lang.Class<T>)
or
ElasticsearchRepository#findById(ID)

spring-data-elasticsearch search template

We have a few similar queries and I wanted to do some templating based on parameters. Recently I've found that elastic supports search templates so I'm wondering whether this is supported by spring-data-elasticearch.
Currently my query looks something like:
final Query query = new NativeSearchQueryBuilder().addAggregation(aggregationBuilder)
.withPageable(EmptyPage.INSTANCE)
.withQuery(queryBuilder)
.build();
I'm wondering if I can somehow pass the template that I've stored in application and get the result from elastic. Or if I can store the template in elastic and get the result based on parameters.
No, Spring Data Elasticsearch currently does not support search templates.
Edit 16.03.2021: search template support has been added to the ReactiveElasticsearchClientin Spring Data Elasticsearch by a pull request from bilak. Thanks for that.

disable TypeHints in the Document Generated for Spring Data ElasticSearch 4.X

Is there a way i can disable TypeHints in the Document Generated for Spring Data ElasticSearch.
https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.mapping.meta-model.rules
I have the Mapping Definition for my elastic Index (7.X) Dynamic Mapping Set to Strict and when i am trying to Index a Document it was created a Field _class in the Elastic Document which is failing the Document Insertion into the ElasticSearch index 7.X with Below Error
Elasticsearch exception [type=strict_dynamic_mapping_exception, reason=mapping set to strict, dynamic introduction of [_class] within [_doc] is not allowed]
Currently this is not possible. You can create an issue in Jira to have this implemented as a new feature, but beware that if type hints are not written, you wont be able to properly read collection-like values of generics.
For example if you have two classes Foo and Bar and in an entity you have a property of type List<Object> which contains Foos and Bars you won't be able to read back such an entity from Elasticsearch, because the type information of the objects would be lost.

Guidance on how to index Elasticsearch documents using Spring Data

My application uses both Spring Data JPA and Spring Data Elasticsearch.
I plan to first persist the JPA entities, then map them to a slightly different java class (the Elasticsearch document) and finally index that document into the Elasticsearch index.
However, I have a few questions as how, where and when to index the documents.
Is indexing a time consuming process that should be asynchronous?
What design pattern could help me avoid having problematic code such as the following?
saveAdvertisement method from AdvertisementService:
public void saveAdvertisement(Advertisement jpaAdvertisement) {
jpaAdvertisementRepository.save(jpaAdvertisement);
//somehow map the jpa entity to the es document
elasticSearchTemplate.index(esAdvertisement);
}
whereby I have to have two concerns in the same method:
JPA persist
Elasticsearch indexing

Resources