Elasticsearch simultaneously search by two documents - elasticsearch

I have two different documents in the Elasticsearch - Decision and Nomination.
Right now I can only search all of the documents wit Decision type.
I use Spring Data Elasticsearch for this purpose:
PageRequest pageRequest = DecisionUtils.createPageRequest(pageNumber, pageSize);
MultiMatchQueryBuilder fuzzyMmQueryBuilder = multiMatchQuery(query, "name", "description").fuzziness("AUTO");
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder().should(fuzzyMmQueryBuilder);
NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
nativeSearchQueryBuilder.withIndices(ESDecision.INDEX_NAME).withTypes(ESDecision.TYPE).withPageable(pageRequest);
NativeSearchQuery nativeSearchQuery = nativeSearchQueryBuilder.withQuery(boolQueryBuilder).withPageable(pageRequest).build();
return elasticsearchTemplate.queryForPage(nativeSearchQuery, ESDecision.class);
Is it possible to update this code to search by Decision and Nomination simultaneously in order to get the search result from both of them ? If the answer is yes - please show an example how to implement this and also please show how to determine at search results who is Decision and who is Nomination ? Is there any classification field that can be added into search result entity for this purpose ?

Related

Elastic search duplicate aggregation section in a query with NEST

I am creating a simple elastic request with Nest.
var searchRequest = new SearchRequest()
{
Aggregations = new AggregationDictionary()
{
}
};
When I serialize the query with elasticClient.RequestResponseSerializer.SerializeToString(searchRequest the query I am seeing like this :
{"aggs":{},"aggregations":{}}
Aggregation sections were created two times.
What should I do?
Is this a problem?
My nest version and elastic Search version are 7.6.2
Thanks.

Spring MongoDB query with or operator and text search

How can i build this MongoDB query with Spring Criteria?
{
$or: [
{ "$text" : { "$search" : "570-11024" } },
{"productDetails.code": "572-R110"}
]
}
It combines a fulltext index search with normal Where criteria with an orOperator.
Query's orOperator(Criteria... criteria) method takes only Criteria and no TextCriteria and also no CriteriaDefinition interface.
Yeah you are right, in spring data mongo you could do this,
final TextCriteria textCriteria = TextCriteria.forDefaultLanguage().matchingAny("570-11024");
final DBObject tc = textCriteria.getCriteriaObject();
final Criteria criteria = Criteria.where("productDetails.code").is("572-R110");
final DBObject co = criteria.getCriteriaObject();
BasicDBList or = new BasicDBList();
or.add(tc);
or.add(co);
DBObject qq = new BasicDBObject("$or", or);
// Use MongoTemplate to execute command
mongoTemplate.executeCommand(qq);
Yes, you currently cannot use the Query's orOperator method to combine Criteria and TextCriteria. A workaround involves converting both the Criteria and TextCriteria objects to its Document representations, adding it to a BasicDbList and then converting back to a "$or" Criteria object.
TextCriteria textCriteria = TextCriteria.forDefaultLanguage().matchingAny("570-11024");
Criteria criteria = Criteria.where("productDetails.code").is("572-R110");
BasicDBList bsonList = new BasicDBList();
bsonList.add(criteria.getCriteriaObject());
bsonList.add(textCriteria.getCriteriaObject());
Query query = new Query();
query.addCriteria(new Criteria("$or").is(bsonList));
mongoTemplate.find(query, YourEntity.class);
PS: Someone has raised this issue in the spring-data-mongodb repo with a proposed fix by
changing the parameter types of orOperator from Criteria to CriteriaDefinition.
https://github.com/spring-projects/spring-data-mongodb/issues/3895.

matchAllQuery() in ElasticSearch

matchAllQuery() in Elasticsearch gets me only 10 results how do I increase its output so that I can get as many results as per my requirement.
Code
QueryBuilder query = QueryBuilders.matchAllQuery();
By default 10 results are returned, you need to increase the size parameter:
SearchRequestBuilder request = client.prepareSearch(index)
.setQuery(QueryBuilders.matchAllQuery())
.setSize(100);
Yes u can do , here you can pass aPageRequestcount whatever you want and If you want no of records exist in Elastic search than repository.count() will work for that :-
int aPageRequestcount = (int) repository.count();
NativeSearchQueryBuilder aNativeSearchQueryBuilder = new NativeSearchQueryBuilder();
aNativeSearchQueryBuilder.withIndices(indexName).withTypes(type).withPageable(new PageRequest(0, aPageRequestcount));
final BoolQueryBuilder aQuery = new BoolQueryBuilder();
NativeSearchQuery nativeSearchQuery = aNativeSearchQueryBuilder.withQuery(aQuery).build();
= elasticsearchTemplate.queryForList(nativeSearchQuery, A.class);

How to create a search with or clause using Elasticsearch

I want to create a query with Elasticsearch Java API but I don't know how to create an OR clause? What I want to query is;
SELECT *
FROM USERS
WHERE (user.name = "admin") AND (user.message LIKE "test*") AND (user.age = "30" OR user.status = "major")
I have created a query like below but I don't know how to create an OR clause like sql query;
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.matchQuery("name", "admin"));
boolQueryBuilder.must(QueryBuilders.matchQuery("message", "test*"));
boolQueryBuilder.must(QueryBuilders.matchQuery("age", "30"));
boolQueryBuilder.must(QueryBuilders.matchQuery("status","major"));
You simply need to capture the OR condition inside another bool/should query
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.matchQuery("name", "admin"));
boolQueryBuilder.mustNot(QueryBuilders.matchQuery("message", "test*"));
BoolQueryBuilder orQuery = QueryBuilders.boolQuery();
orQuery.should(QueryBuilders.matchQuery("age", "30"));
orQuery.should(QueryBuilders.matchQuery("status","major"));
orQuery.minimumNumberShouldMatch(1);
boolQueryBuilder.must(orQuery);
PS: not sure why you have a mustNot for your second constraint.
The "should" keyword in bool queries works like "OR", but in Elasticsearch those queries are not very sharp as everything is scored.
Have a look at:
https://www.elastic.co/guide/en/elasticsearch/guide/current/bool-query.html#bool-query
Also there are very good examples in the book Elasticsearch in Action.

NEST ElasticSearch, Query Result Is Empty

I'm trying to query an elastic search index using a match_all query.
Uri uri = new Uri("http://10.10.10.67:9200");
ConnectionSettings connection = new ConnectionSettings(uri);
connection.SetDefaultIndex("leases");
int port = connection.Port;
ElasticClient client = new ElasticClient(connection);
var feeQueryObject = client.Search<FeeQueryResult>(s => s
.Type("leases").MatchAll());
Using sense, I get results but not with NEST.
Not sure if I have to set up my mapped class exactly how the schema is in the ElasticSearch document? - I only have a few properties in my class, not all of them.
Any ideas as to why there are no results returned?
This was my fault.
The type was incorrect. Should be 'fee' not 'leases'.

Resources