Elasticsearch How to add bool query inside must_not with multiple match in java API? - elasticsearch

I have a query like this in Elasticsearch:
{
"query": {
"bool": {
"must_not": [
{ "match": "filed1" : "value1"},
{ "match": "filed2" : "value2"}
]
}
}
}
I have tried below code.but it only for single match.
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.mustNot(QueryBuilders.matchQuery("field1","value1");
What is the way to use must_not with multiple match in java API.Thanks.

Good start! You can simply call mustNot() as many times as conditions you need:
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.mustNot(QueryBuilders.matchQuery("field1","value1"));
boolQueryBuilder.mustNot(QueryBuilders.matchQuery("field2","value2"));
Simple as that ;-)

Looks a bit, like the original question was misunderstood. If you have multiple conditions in a single mustNot-constellation, it is not just as easy, as adding multiple .mustNot. Example:
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.mustNot(QueryBuilders.matchQuery("name","Donald");
boolQueryBuilder.mustNot(QueryBuilders.matchQuery("hometown","Washington");
If You want to exclude all Donalds from Washington, this is not the right solution, because it will exclude ALL Donalds and ALL from Washington.
If you want to combine these two attributes, just create a new Query (think as if you'd code it with brackets...):
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
BoolQueryBuilder subQuery = QueryBuilders.boolQuery();
subQuery.must(QueryBuilders.matchQuery("name","Donald"); // must (positive!)
subQuery.must(QueryBuilders.matchQuery("hometown","Washington"); // must (positive!)
queryBuilder.mustNot(subQuery); // negate subQuery!

Related

Elasticsearch Jest client add condition to json query

I am using Elasticsearch 6.3 with Jest client 6.3 (Java API)
Search search = new Search.Builder(jsonQueryString)
.addIndex("SOME_INDEX")
.build();
SearchResult result = jestClient.execute(search);
And this is my sample JSON query
{
"query": {
"bool" : {
"filter": {
"match" :{
"someField" : "some value"
}
}
}
}
}
The JSON query string is accepted as a POST request body and then passed to the Jest client. Before I can execute the json query on the Jest client, I need to add conditions to the query for e.g.
{
"query": {
"bool" : {
"filter": {
"match" :{
"someField" : "some value"
}
}
},
"must": {
"match" :{
"systemField" : "pre-defined value"
}
}
}
}
}
Is there an API that allows to parse the JSON query and add conditions to it before it can be executed on Jest client? The JSON query can be any query supported by Query DSL and not necessarily contain bool condition. I need to add a pre-defined condition to the query. I appreciate any help on this. Thanks very much.
There is no out of the box Elasticsearch or Jest API to achieve the above, the workaround I implemented is using Jackson ObjectMapper
// convert the search request body into object node
ObjectNode searchRequestNode = objectMapper.readValue(queryString, ObjectNode.class);
// extract the query
String query = searchRequestNode.get("query").toString();
// wrap the original query and add conditions
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.wrapperQuery(query));
boolQueryBuilder.filter(QueryBuilders.termsQuery("fieldA", listOfValues));
boolQueryBuilder.filter(QueryBuilders.termQuery("fieldB", value));
// convert querybuilder to json query string
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(queryBuilder);
String queryWithFilters = searchSourceBuilder.toString();
// convert json string to object node
ObjectNode queryNode = objectMapper.readValue(queryWithFilters, ObjectNode.class);
// replace original query with the new query containing added conditions
searchRequestNode.set("query", queryNode.get("query"));
String finalSearchRequestWithOwnFilters = searchRequestNode.toString();

Elasticsearch search Java API doesn't give correct results

I have following code for ES search:
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http")));
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
qb = QueryBuilders.termQuery("title", "Java");
searchSourceBuilder.query(qb);
searchSourceBuilder.from(0);
searchSourceBuilder.size(5);
SearchRequest searchRequest = new SearchRequest("myindex");
searchRequest.types("books");
searchRequest.source(searchSourceBuilder);
SearchResponse sr = client.search(searchRequest);
System.out.println(sr.getHits().totalHits);
It gives me no results, even though there are documents meeting this criteria.
When I run similar term query from Kibana, I get results:
GET /myindex/books/_search
{
"query":
{
"term" : {
"title" : {
"value" : "java"
}
}
}
}
The difference is Java (with uppercase) in your Java code and java (in lowercase) in your Kibana query.
Since you're doing a term query, the case matters. If you used a match query, the case doesn't matter and both would work as you expect.

How can I make a SearchRequest in ES 6.0 from a json string using High level REST Client in Scala

I'm migrating from ES 1.7 to 6.0 and I'm trying to figure out a way to perform a search request passing a plain query to the High Level REST Java client. To keep queries easy to read (and maintain), we want to use a Json string instead of building them with the SearchSourceBuilder. This was possible before but I'm not finding a nice way to do it now.
Example query:
{
"filter": {
"bool": {
"must": [
{
"term": {
"status": "Success"
}
},
{
"term": {
"type": "someType"
}
},
{
"range": {
"endDateTime": {
"lte": "someDateTime"
}
}
}
]
}
}
}
Example code:
val searchRequest = buildSearchRequest(indexName, indexType, query)
val searchResponse = restHighLevelClient.search(searchRequest)
I know it's possible to send the query using the LowLevelClient but it returns a Response and not a SearchResponse. Has anyone faced this issue before? Any workaround or solution?
I'm a little bit late, but I have a close solution for your problem.
Firstly, you need to build your query with something like this:
private QueryBuilder buildQuery(MyObject object) {
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
QueryBuilder statusTerm = QueryBuilders.termQuery("status", object.getStatus());
QueryBuilder typeTerm = QueryBuilders.termQuery("type", object.getType());
QueryBuilder boolQuery = QueryBuilders.boolQuery()
.must(statusTerm)
.must(typeTerm);
queryBuilder.should(boolQuery);
return queryBuilder;
}
Then, you can perform your search and return SearchResponse:
private SearchResponse search(QueryBuilder query) throws IOException {
SearchRequest request = new SearchRequest(elasticSearchClient.getElasticsearchClientProperties().getIndices());
SearchSourceBuilder searchRequestBuilder = request.source();
searchRequestBuilder.query(query);
//SearchSourcebuilder allows you to set your query into your request
return this.elasticSearchClient.search(request);
}
Finally, can have a method that calls both:
public void processSearch(MyObject object) {
QueryBuilder query = buildQuery(object);
SearchResponse response = search(query);
List<FoundObjects> objects = parseResponse(response);
}
I hope this could help. Good luck!

Elasticsearch Query: Boosting specific field

I am using Elasticsearch 2.4.3 and want to boost specific fields in my query. Is this possible? I only see how I can boost an index.
Greetings!
UPDATE
Mapping:
"firstName":{"type":"string",
"analyzer":"customNGram"
},
"lastName":{
"type":"string",
"analyzer":"customNGram"
},
"note":{
"type":"string",
"analyzer":"customNGram"
}
Query (Java API):
QueryBuilder qb = new BoolQueryBuilder()
.must(QueryBuilders.matchQuery("_all", term)
.analyzer("atsCustomSearchAnalyzer")
.operator(Operator.AND));
SearchRequestBuilder searchRequestBuilder = elasticsearchClient.prepareSearch("persons", "activities").setTypes("person", "activity")
.setQuery(qb)
.addHighlightedField("*").setHighlighterRequireFieldMatch(false)
.setHighlighterOrder("score")
.setHighlighterFragmentSize(150)
.setHighlighterForceSource(true)
.setSize(100)
.addIndexBoost("persons", 200)
.setFrom(offset);
return searchRequestBuilder.execute().get();
If you split up your match-query to match individual fields, eg using a multi match query (https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html), you can boost the field you like. So something like:
QueryBuilder qb = new BoolQueryBuilder()
.must(QueryBuilders.multiMatchQuery(term, "firstName^3",
"lastName^3", "note")
.analyzer("atsCustomSearchAnalyzer")
.operator(Operator.AND));
should boost firstName and lastName 3 times relative to the note field.

Obtaining string query (JSON) from SearchQuery object

For debugging purposes, I need to know what query spring-data-elasticsearch is sending to the ElasticSearch cluster. I have tried to call the toString method on the SearchQuery object, and doesn't return what I need.
What I am doing in Java (using spring-data-elasticsearch) is:
private FilterBuilder getFilterBuilder(String id) {
return orFilter(
termFilter("yaddayaddayadda.id", id),
termFilter("blahblahblah.id", id)
);
}
SearchQuery sq = NativeSearchQueryBuilder()
.withQuery(new MatchAllQuery())
.withFilter(fb)
.build();
And I expect to return something like this plain query executed in ES cluster REST API is returning:
{
"query": {
"filtered": {
"filter": {
"or": [
{
"term": {
"yaddayaddayadda.id": "9"
}
},
{
"term": {
"blahblahblah.id": "9"
}
}
]
}
}
}
}
Thanks in advance!
One way to achieve this is to log the queries on the ES/server-side into the slowlog file. Open your elasticsearch.yml config file and towards the bottom uncomment/edit the two lines below:
...
index.search.slowlog.threshold.query.info: 1ms
...
index.search.slowlog.threshold.fetch.info: 1ms
...
The advantage of this solution is that whatever client technology you're using to query your ES server (Spring Data, Ruby, Browser, Javascript, etc), you'll be able to dump and debug your queries in a single location.
SearchQuery Interface has a method getQuery() and getFilter() to get the information you need.
System.out.println(searchQuery.getQuery());
System.out.println(searchQuery.getFilter());
Hope this helps.
When using SearchRequest or SearchSourceBuilder, calling .toString() method on their instance will get you actual JSON query:
SearchRequest searchRequest = new SearchRequest("index");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// building the query
// ...
searchSourceBuilder.query(query);
searchRequest.source(searchSourceBuilder);
System.out.println(searchSourceBuilder.toString()); // prints json query
System.out.println(searchRequest.toString()); // prints json query + other information

Resources