searching for a word in a string indexed field in elasticsearch java - elasticsearch

I have a String field countries of format eg "SS,SX,US,IND,CND,TN" and I have a input String field countryCode which will be of form "SS". How can I query all the records where a given countryCode is in countries in elasticsearch? I have tried with match and match_phrase queries but didn't get the desired results.
Query.must(QueryBuilders.matchPhraseQuery("countries", countryCode))

You can try with a bool query instead:
QueryBuilder qb = boolQuery().must(termQuery("countries", countryCode))
(Your "countries" field should not be defined as not_analyzed)

Related

Nested document full text query with filter capability

My index mappings and sample data as follows
I need full text search on these type of documents with following criteria:
country is one input of this search
If I search "Alex 4455" and country is "xxx" this document will be matched and return following document.
If I search "Landing" and country is "xxx" this document will be matched and return following document.
If I search "Martin 4455" and country is "xxx", result is null.
In the other hand, I need combined_field in nested document with filter capability!!!
I try combined_field and saw that is not good for nested document. Also I try query_string and found that not good for my needs!

How to prevent slow match / match_phrase queries for keywords in Kibana?

How can I achieve that a match query for certain fields is equivalent to a term query?
I have a larger index in Elastic covering events. Each event has an eventid field consisting of a random hex string (e.g. f4fc38c993c1a8273f9c40eedc9050b7) as well as some other fields. The eventid is indexed as keyword in Elastic.
If I query based on this field in Kibana, the query often runs into timeouts, because Kibana automatically generates a match query for eventid:f4fc38c993c1a8273f9c40eedc9050b7.
If I set a manual filter using { "query": { "term": { "eventid": "f4fc38c993c1a8273f9c40eedc9050b7" } } } (so a term instead of match query) I get a response quite quickly.
From my understanding, these should be pretty much equivalent, as keyword fields aren't analyzed, so the match query should be equivalent to a term query.
What am I missing?

Invalid result returned with queryString on string array field elasticsearch

I have field tags:["software engineer", "IT implementation"].
I have prepared term aggregation on tags field. which return me bucket: [{software engineer: 1} , {IT professional: 1} ]
now in term aggregation filter, I am using queryString to filter "software profess" tags. Term aggregation filter doesn't filter data.
I have used position_increment_gap:100 for tagsList field in indexing. Still, I am getting invalid result.

Elasticsearch : Query on one of the fields given in the list

Elasticsearch has documents indexed with the following fields:
{"id":"1", "title":"test", "locale_1_title":"locale_test"}
Given a query, following behaviour is needed at each document level:
1) If locale_1_title field is not empty(""), search only on locale_1_title field. Do not search on title field.
2) If locale_1_title field is empty, search on title field.
What can be a simple elasticsearch query to get the above behaviour ?

BoolFilter and BoolQuery in ElasticSearch

I am applying to two search requests a filter and a query semantically identical like so:
static FilterBuilder filter(String field1Value, String field2Value){
return FilterBuilders.boolFilter().must(FilterBuilders.termFilter("field1",field1Value)).should(FilterBuilders.termFilter("field2",field2Value));
}
static QueryBuilder query(String field1Value, String field2Value){
return QueryBuilders.boolQuery().must(QueryBuilders.termQuery("field1",field1Value)).should(QueryBuilders.termQuery("field2",field2Value));
}
client.prepareSearch(indexName).setPostFilter(filter("hello", "world")).setTypes("mytype");
client.prepareSearch(indexName).setQuery(query("hello","world")).setTypes("mytype");
However, while the search with the query returns results, the search with the filter doesn't return any result. Aren't the two suppose to behave identically and if not, why?
They are not exactly the same.
In a bool query with a must clause a document would be a match if none of the clauses in should are matched provided there is no explicit minimum_should_match in the query.
In filter bool query at-least one should clause needs to be satisfied for a document to be considered a match. In filters there is no option of minimum_should_match and can be treated as always set to one.
i.e for filters it can be viewed as follows
[must_clause] && [should_clause1 || should_clause_2]
For the example in the OP :
1) the documents would pass the filter if and only if they match field1 criteria in must clause and field2 criteria in should clause .
2) Whereas for bool query it would suffice for a document to be considered a match if must-clause is satisfied i.e field1 match

Resources