MySql equivalent `IN()` query in elastic search - elasticsearch

I would like to get all entry that country code is US or CA. I prepare my query by the following way but getting zero number or records instead of having te records by country code US and CA
Way 1
GET my_index/_search
{
"query" : {
"bool" : {
"filter" : {
"terms" : {
"country_code" : ["CA","US"]
}
}
}
}
}
Way 2
GET my_index/_search
{
"query" : {
"bool" : {
"must" : {
"terms" : {
"country_code" : ["CA","US"]
}
}
}
}
}
Could you please help me to find out my fault in query?

I got my solution by using lowercase
GET my_index/_search
{
"query": {
"bool": {
"filter": [
{"terms": {"country_code": ["us","ca"]}}
]
}
}
}

Related

Elasticsearch searching all fields containing a value?

{
"from" : 0,
"size" : 1000,
"query" : {
"bool" : {
"must" : {
"wildcard" : {
"_all" : "*tunnel*"
}
}
}
}
}
I'm trying to execute a query to search all fields containing the phrase tunnel. I have titles called tunnel.xml and get no results. What am I missing here?
Try Query String Query
GET data/Company/_search
{
"query": {
"query_string": {
"query": "*tunnel*"
}
}
}

Elasticsearch - EXISTS syntax + Filter not working

I am trying to query for a date range where a particular field exists. This seems like it would be easy but I am sensing that the keyword "exists" has changed per the documentation. I am on 5.4. https://www.elastic.co/guide/en/elasticsearch/reference/5.4/query-dsl-exists-filter.html
I use #timestamp for dates and the field "error_data" is in the mapping and only appears if an error condition is found.
Here is my query....
GET /filebeat-2017.07.25/_search
{
"query": {
"bool" : {
"filter" : {
"range" : {
"#timestamp" : {
"gte" : "now-5m",
"lte" : "now-1m"
}
}
},
"exists": {
"field": "error_data"
}
}
}
}
but it says that "[bool] query does not support [exists]" whereas the following does not work either but gets an parsing error message of "[exists] malformed query, expected [END_OBJECT] but found [FIELD_NAME]" on line 6 column 9. Thanks for your help.
GET /filebeat-2017.07.25/_search
{
"query": {
"exists": {
"field": "error_data"
},
"bool" : {
"filter" : {
"range" : {
"#timestamp" : {
"gte" : "now-5m",
"lte" : "now-1m"
}
}
}
}
}
}
You're almost there. Try like this:
GET /filebeat-2017.07.25/_search
{
"query": {
"bool" : {
"filter" : [
{
"range" : {
"#timestamp" : {
"gte" : "now-5m",
"lte" : "now-1m"
}
}
},
{
"exists": {
"field": "error_data"
}
}
]
}
}
}
i.e. the bool/filter clause must be an array if you have several clauses to put in it:

Query string embeded with bool query

HI iam using the following query with bool must
{
"query": {
"bool" : {
"must" : [ {
"match" : {
"orgid" : {
"query" : 13831,
"type" : "boolean"
}
}
}, {
"query_string" : {
"query" : "*07* AND *fres*"
}
} ]
}
}
}
It's hitting null even though there documents with org id as 13831 & in documents there is one data in all fields as 07 and fres.Is anything wrong in this query
Edit: A problem might be you're using a match query instead of a term query. Term queries are used for exact matches. The following will filter the result set to only the org_id you want, and it will then look for 07 and fres. Please give it a try.
"query": {
"bool" : {
"must" : {
"query_string" : {
"query" : "*07* AND *fres*"
}
},
"filter": {
"term" : { "orgid" : 13831 }
}
}
}

OR & AND Operators in Elastic search

Hi I want to achieve this in Elasticsearch.
select * from products where brandName = Accu or brandName = Perfor AND cat=lube(any where in any filed of an elastic search ).
I am using this query in Elasticsearch.
{
"bool": {
"must": {
"query_string": {
"query": "oil"
}
},
"should": [
{
"term": {
"BrandName": "Accu"
}
},
{
"term": {
"BrandName": "Perfor"
}
}
]
}
}
By this query m not getting the combination exact results.
You need to add minimum_should_match: 1 to your query and probably use match instead of term if your BrandName field is an analyzed string.
{
"bool" : {
"must" : {
"query_string" : {
"query" : "oil OR lube OR lubricate"
}
},
"minimum_should_match": 1, <---- add this
"should" : [ {
"match" : {
"BrandName" : "Accu"
}
}, {
"match" : {
"BrandName" : "Perfor"
}
} ]
}
}
This query satisfy your condition.
{
"bool" : {
"must" : { "term" : { "cat" : "lube" } },
"should" : [
{ "term" : { "BrandName" : "Accu" } },
{ "term" : { "BrandName" : "Perfor" } }
],
"minimum_should_match" : 1
}
}

Field Priority on full text search

I have a situation in which my products are described in a few and the following structure is used:
{
"defaultDescription" : "Default Description",
"i18nDescription" : {
"pt" : "Descrição Padrão",
"de" : "Standard-Beschreibung"
}
}
Now I have the following requirement: perform a search following a list of prioritized languages (3 languages). If the first language isn't in the i18nDescription, use just the second language, if the second language isn't there use just the third one otherwise match against defaultDescription.
My solution would be something like:
// suppose request comes with the following languages: en, de, pt
{
"size":10,
"fields" : ["defaultDescription", "i18nDescription.en^50", "i18nDescription.de^20", "i18nDescription.pt^10"],
"query": {
"multi_match" : { "query" : "default", "fields" : ["description","descriptions.fr-CA"] }
}
}
But this solution will just sort the result by priority language, I would like to do something like: i18nDescription.en:search OR (i18nDescription.de:search AND _empty_:i18nDescription.en) OR (i18nDescription.pt:search AND _empty_:i18nDescription.en AND _empty_:i18nDescription.de) OR (description:search AND _empty_:i18nDescription.pt AND _empty_:i18nDescription.en AND _empty_:i18nDescription.de)
Is there a way to represent this is a ElasticSearch query?
Playing a bit with bool queries we could reach the desired effect.
It basically needs to check if one field has the text and others (that are more important) are empty, so it would consider just the most important present field.
Query would be something similar to:
{
"size":10,
"query": {
"bool" : {
"should" : [
{
"bool" : {
"must" : [
{ "multi_match" : { "fields":["defaultDescription"], "query" : "default" } },
{ "query_string" : { "query" : "+_missing_:i18nDescription.en +_missing_:i18nDescription.de +_missing_:i18nDescription.pt" } }
]
}
},
{
"bool" : {
"must" : [
{ "multi_match" : { "fields":["i18nDescription.pt"], "query" : "default" } },
{ "query_string" : { "query" : "+_missing_:i18nDescription.en +_missing_:i18nDescription.de" } }
]
}
},
{
"bool" : {
"must" : [
{ "multi_match" : { "fields":["i18nDescription.de"], "query" : "default" } },
{ "query_string" : { "query" : "+_missing_:i18nDescription.en" } }
]
}
},
{
"bool" : {
"must" : [
{ "multi_match" : { "fields":["i18nDescription.en"], "query" : "default" } }
]
}
}
]
}
}
}

Resources