Elasticsearch should not operation query - elasticsearch

Say we can match ("name"="A" AND "name!="B") as,
BoolQueryBuilder queryBuilder1 = QueryBuilders.boolQuery()
.must(QueryBuilders.matchQuery("name","A"))
.mustNot(QueryBuilders.matchQuery("name","B"));
How can I imply a "shouldNot" operation for ("name"="A" OR "name"!="B")
like,
BoolQueryBuilder queryBuilder1 = QueryBuilders.boolQuery()
.should(QueryBuilders.matchQuery("name","A"))
.sholdNot(QueryBuilders.matchQuery("name","B")); // This API not exists..!

You can use this:
BoolQueryBuilder queryBuilder1 = QueryBuilders.boolQuery()
.should(QueryBuilders.matchQuery("name","A"))
.should(QueryBuilders.boolQuery()
.mustNot(QueryBuilders.matchQuery("name","B")))

Related

Add Size in Elastic NativeSearchQuery

final QueryBuilder contentTagQuery = QueryBuilders.boolQuery()
.filter( QueryBuilders.termQuery("tenantId" , "en"));
SearchHits<Content> searchHits =
elasticsearchOperations.search(
new NativeSearchQuery(contentTagQuery), Content.class,
IndexCoordinates.of("index"));
How can I add size in this query, as I only need the first result.
You can set this on the Query instance:
NativeSearchQuery query = new NativeSearchQuery(contentTagQuery);
query.setMaxResults(1);

ElasticSearch RestHighLevelClient provides inaccurate results

I am using ES with SpringBoot. I try to search results using the following code snippet.
SearchRequest searchRequest = new SearchRequest("businesses");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.termQuery("name", "Microsoft"));
SearchResponse response = highLevelClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHit[] searchHit = response.getHits().getHits();
When I iterate through "searchHits", none of the names matches "Microsoft". It always returns some other names. Also, the list of other names is consistent evrytime. Is anything wrong in the code?
The type of the name field should be keyword in your document mapping. I guess you didn't set it explicitly and by default it is set to text.

Elasticsearch should operation query

How can I imply a "should" operation for
("tag"="A" OR "tag"="B") and "name"="cat"
like ?
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery()
.should(QueryBuilders.matchQuery("tag","A"))
.shold(QueryBuilders.matchQuery("tag","B"))
.must(QueryBuilders.matchQuery("name","cat"));
If you want to do ("tag"="A" OR "tag"="B") and "name"="cat", you can do as follow :
BoolQueryBuilder aOrB = QueryBuilders.boolQuery()
.should(QueryBuilders.matchQuery("tag","A"))
.should(QueryBuilders.matchQuery("tag","B"))
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery()
.must(aOrB);
.must(QueryBuilders.matchQuery("name","cat"));

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.

Resources