We are using Spring Data Elasticsearch Reactive Template
Query searchQuery = new NativeSearchQueryBuilder()
.withQuery(queryBuilder)
.withPageable(PageRequest.of(0, 10))
.addAggregation(AggregationBuilders.terms("categories").field("category"))
.build();
reactiveElasticsearchTemplate.search(searchQuery, documentType, IndexCoordinates.of(indexName))
In response we have Flux<SearchHit<T>> but there are no methods to retrieve aggregations.
How to retrieve the aggregations?
The ReactiveElasticsearchTemplate has aggregate methods.
See the corresponding API interface
There is no combination of the single entities in a flux and the aggregations in the reactive part.
Related
I want to write a mongo query to fetch All Documents in a collection and project just 2 fields and no criteria.
I want to use streams here and iterate over the list of documents to process them.
How can I write this in Java Spring Mongo?
I have something like this:
public CloseableIterator<Employees> streamAllEmployees() {
Query query = new Query();
query.fields().include("_id.name","_id.dept");
query.addCriteria(Criteria.where("_id.dept").is(1));
return mongoOperations.stream(query, Employee.class);
}
but how can I change this to not have any QueryCriteria?
Assume spring-boot-starter-data-elasticsearch version 2.1.0.RC1.
Take the following, simple implementation for indexing an entity:
IndexQuery indexQuery = new IndexQueryBuilder().withId(entity.getId()).withObject(entity).build();
String id = elasticsearchTemplate.index(indexQuery);
How do I set the OpType.CREATE on this operation, so that I can assure only documents get indexed which don't already exist?
The equivalent REST API request would look like the following:
POST /{index}/{entity id}?op_type=create
{
"id" : "{entity id}",
"attribute" : "value"
}
This is not supported at the moment by Spring Data ES.
There's a open issue that reports exactly that feature, you might want to check it out: https://jira.spring.io/browse/DATAES-247
I am dumping data in SOLR database. Earlier I was using Elastic Search and it was allowing me to store nested JSON objects.
Is there any way I can dynamically create nested JSON values when inserting in SOLR?
I am using JAVA as a backend language. My code is:
SolrInputDocument document = new SolrInputDocument();
document.addField("UUID", eventID);
document.addField("eventCategory", eventCategory);
.
.
.
.
document.addField("source", source);
I want something like this:
{
"UUID":"1",
"source":abcd,
"eventCategory": {
"event1":"a",
"event2":"b",
"event3":"c"
}
}
Solr is a collection of flat schema documents. You can add dynamic field to solr but it does not support nested JSON object.
But you can use nested documents specified in following resource.
https://lucene.apache.org/solr/guide/6_6/uploading-data-with-index-handlers.html
But querying on child documents is not as straightforward as ElasticSearch or MongoDB.
you can use addChildDocument to add the nested doc to the parent one. So code would be:
SolrInputDocument document = new SolrInputDocument();
document.addField("UUID", eventID);
document.addField("eventCategory", eventCategory);
...
SolrInputDocument child = new SolrInputDocument();
...
document.addChildDocument(child);
How can I reference two mongodb collections using spring data while the localField is of type ObjectId and foreignField is of type String?
ProjectionOperation convertId=Aggregation.project().and("_id").as("agentId");
LookupOperation activityOperation = LookupOperation.newLookup().
from("activity").
localField("agentId").
foreignField("agent_id").
as("activities");
Aggregation aggregation = Aggregation.newAggregation(convertId,activityOperation);
return mongoTemplate.aggregate(aggregation, "agents", AgentDTO.class).getMappedResults()
However, this doesn't return any records because of the type issue. Is it possible to implement $toString or $convert in ProjectionOperation? or what other options are there?
I was able to solve it by writing native mongodb aggregation operation in java code as described in MongoDB $aggregate $push multiple fields in Java Spring Data
After implementing this solution I was able to add native $addfields as follows:
AggregationOperation addField=new GenericAggregationOperation("$addFields","{ \"agId\": { \"$toString\": \"$_id\" }}");
I want to put this query with #Query annotation in my repository.
This is the query:
`db.report.find({'company' : 'Random'}).sort( { 'reportDate' : -1} ).limit(1)`
Which is the best way to implement custom queries with #Query annotations or to use MongoTemplate ?
Using Mongo Template.
Criteria find = Criteria.where("company").is("Random");
Query query = new Query().addCriteria(find).with(new Sort(Sort.Direction.DESC, "reportDate"));
BasicDBObject result = mongoOperations.findOne(query, BasicDBObject.class, "collection_name");
Using Mongo Repository
Report findTopByCompanyOrderByReportDateDesc(String company)
Note that in the new version of Springboot(v2.2.5), the correct method is sort.by().
like this:
Query query = new Query().addCriteria(find).with(Sort.by(Sort.Direction.DESC, "reportDate"));