Query string embeded with bool query - elasticsearch

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 }
}
}
}

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*"
}
}
}

MySql equivalent `IN()` query in elastic search

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"]}}
]
}
}
}

multiple match must fields not working in elastic search

below query is fetching result if i give existing record that is fine , but if i change name field from 'John' to 'John1' then still record is fetching.
{
"query" : {
"bool" : {
"must" : [
{ "match" : {"employeeId" : "1234"}},
{ "match" : {"name" : "John"}}
]
}
}
}
I tried another alternative query as well but still giving result.which query is correct in terms of performance?but both are giving results if i change name record from 'John' to 'John1'
{
"filter": {
"bool" : {
"must" : {
"term" : {
"employeeId" : "1234"
}
}
}
},
"query": {
"match" : {
"name" : {
"query" : "John",
"type" : "phrase"
}
}
}
}
This because you are doing match, if you want do exact search you need to use filter
Notice we assuce the mapping of name column is analyzed
{
"query" :{
"filtered" : {
"filter" : {
"bool" : {
"must" : [
{ "term" : {"employeeId" : "1234"}},
{ "term" : {"name" : "john"}}
]
}
}
}
}
}

query_string filter inside a nested filter throws an error

In this example I've placed a full query example that's been paired down to the non-working portion that has me stuck.
Basically we have a document that has a nested document type under datailItems and trying to do a query_string filter against a field in that nested document fails. I take the same portion and run it against a non-nested field and it works. So obviously I'm doing something wrong.
The error I receive is nested: QueryParsingException[[******] [_na] filter malformed, must start with start_object]
So, What is the proper way to do this?
Some caveats. The "and"s are used for further filter requirements that contain bools, ranges, etc... I have stripped out those additional reqs for this example.
{
"size" : 1,
"query" : {
"filtered" : {
"filter" : {
"and" : [{
"nested" : {
"path" : "detailItems",
"filter" : {
"and" : [{
"query" : {
"query_string" : {
"detailItems.name" : {
"query" : "mastersettings",
"minimum_should_match" : 1
}
}
}
}
]
}
}
}
]
}
}
}
}
The query_string in the OP is not syntactically correct, below is an example of how the query string could be re-written:
"size" : 1,
"query" : {
"filtered" : {
"filter" : {
"and" : [{
"nested" : {
"path" : "detailItems",
"filter" : {
"and" : [{
"query" : {
"query_string" : {
"fields": [
"detailItems.name"
],
"query" : "mastersettings",
"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