ElasticSearch query_string with filter failed to get the results - elasticsearch

I have the following Elasticsearch query (its usually bigger, but stripped out the part which causes the issues):
'query' => [
'bool' => [
'must' => [
'query_string' => [
'query' => $keywords,
'fields' => ['title']
]
],
'filter' => [
'term' => [
'school_id' => 1
]
]
]
]
But if I remove the filter it's working fine, but what I want is to filter only the search with the specific school id.

Why don't you instead of filtering the data - just take what you need in the first place?
Filtering is used for a binary result-in a sense, if you would like to know if a document field school_id is 1 or not. If you just want to get a result there is other ways to do it as well.
In your case I think believed you just "jumped" over the mustand the bool and this is the reason your query failed.
As so, you got 2 options, the first to fix yours as follows:
GET /_search
{
"query": {
"bool": {
"must": {
"query_string": {
"default_field": "keywords",
"query": "title"
}
},
"filter": {
"bool": {
"must": [
{
"term": {
"school_id": "1"
}
}
]
}
}
}
}
}
OR if you wish to get a scoring to your result you can use this one:
GET /_search
{
"query": {
"bool": {
"must": [
{
"query_string": {
"default_field": "keywords",
"query": "title"
}
},
{
"match": {
"school_id": {
"query": "1"
}
}
}
]
}
}
}

Related

Elasticsearch compare two fields

For example MySQL query
SELECT fields_a, fields_b FROM table WHERE fields_a > fields_b;
I am trying to implement for elasticsearch. I've tried, as follows:
$where = [ "query" => [ "filter" => [ "script" => [ "script" => "doc[\"fields_a\"].value > doc[\"fields_b\"].value" ] ] ] ];
What am I missing?
this should work for me
{
"query": {
"bool": {
"must": [{
"script": {
"script": "doc['field_a'].value > doc['field_b'].value"
}
}]
}
}
}
To select only few fields instead of the whole source document use stored_fields "stored_fields": ["field_a","field_b"] and make sure to have those fields as store=true in mappings .
{
stored_fields": ["field_a","field_b"]
"query": {
"bool": {
"must": [{
"script": {
"script": "doc['field_a'].value > doc['field_b']"
}
}]
}
}
}

Search from multiple nested level fields in elasticsearch

I want to search from multiple nested level fields. query like.
select * from product where brand='brand1' and category='category1'.
In elasticsearch I have two nested level mapping one is category and other is brand.
If i wrote only brand or category it return perfect result but how to write both in following query ?
$params = [
'index' => 'my_index',
'type' => 'product',
'body' => [
"query"=>[
"filtered"=>[
"filter"=>[
"bool"=>[
"must"=>[
"bool"=>[
"must"=>[
[
"query"=>[
"match"=>[
"brand"=>[
"query"=>"brand1",
"type"=>"phrase"
]
]
]
],
[
"query"=>[
"match"=>[
"category"=>[
"query"=>"category1",
"type"=>"phrase"
]
]
]
]
]
]
]
]
]
]
]
]
];
By above query I am getting 0 result
You can try below query it will help you out to get respected answer:
GET /product/ur_type/_search
{
"from": 0,
"size": 200,
"query": {
"filtered": {
"filter": {
"bool": {
"must": {
"bool": {
"must": [
{
"query": {
"match": {
"brand": {
"query": "brand1",
"type": "phrase"
}
}
}
},
{
"query": {
"match": {
"category": {
"query": "category1",
"type": "phrase"
}
}
}
}
]
}
}
}
}
}
}
}

elasticsearch bool query combine must with OR

I am currently trying to migrate a solr-based application to elasticsearch.
I have this lucene query:
((
name:(+foo +bar)
OR info:(+foo +bar)
)) AND state:(1) AND (has_image:(0) OR has_image:(1)^100)
As far as I understand this is a combination of must clauses combined with boolean OR:
Get all documents containing (foo AND bar in name) OR (foo AND bar in info). After that filter results by condition state=1 and boost documents that have an image.
I have been trying to use a bool query with must but I am failing to get boolean OR into must clauses. Here is what I have:
GET /test/object/_search
{
"from": 0,
"size": 20,
"sort": {
"_score": "desc"
},
"query": {
"bool": {
"must": [
{
"match": {
"name": "foo"
}
},
{
"match": {
"name": "bar"
}
}
],
"must_not": [],
"should": [
{
"match": {
"has_image": {
"query": 1,
"boost": 100
}
}
}
]
}
}
}
As you can see, must conditions for info are missing.
** UPDATE **
I have updated my elasticsearch query and got rid of that function score. My base problem still exists.
OR is spelled should
AND is spelled must
NOR is spelled should_not
Example:
You want to see all the items that are (round AND (red OR blue)):
{
"query": {
"bool": {
"must": [
{
"term": {"shape": "round"}
},
{
"bool": {
"should": [
{"term": {"color": "red"}},
{"term": {"color": "blue"}}
]
}
}
]
}
}
}
You can also do more complex versions of OR, for example, if you want to match at least 3 out of 5, you can specify 5 options under "should" and set a "minimum_should" of 3.
Thanks to Glen Thompson and Sebastialonso for finding where my nesting wasn't quite right before.
Thanks also to Fatmajk for pointing out that "term" becomes a "match" in ElasticSearch Version 6.
I finally managed to create a query that does exactly what i wanted to have:
A filtered nested boolean query.
I am not sure why this is not documented. Maybe someone here can tell me?
Here is the query:
GET /test/object/_search
{
"from": 0,
"size": 20,
"sort": {
"_score": "desc"
},
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"term": {
"state": 1
}
}
]
}
},
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"match": {
"name": "foo"
}
},
{
"match": {
"name": "bar"
}
}
],
"should": [
{
"match": {
"has_image": {
"query": 1,
"boost": 100
}
}
}
]
}
},
{
"bool": {
"must": [
{
"match": {
"info": "foo"
}
},
{
"match": {
"info": "bar"
}
}
],
"should": [
{
"match": {
"has_image": {
"query": 1,
"boost": 100
}
}
}
]
}
}
],
"minimum_should_match": 1
}
}
}
}
}
In pseudo-SQL:
SELECT * FROM /test/object
WHERE
((name=foo AND name=bar) OR (info=foo AND info=bar))
AND state=1
Please keep in mind that it depends on your document field analysis and mappings how name=foo is internally handled. This can vary from a fuzzy to strict behavior.
"minimum_should_match": 1 says, that at least one of the should statements must be true.
This statements means that whenever there is a document in the resultset that contains has_image:1 it is boosted by factor 100. This changes result ordering.
"should": [
{
"match": {
"has_image": {
"query": 1,
"boost": 100
}
}
}
]
Have fun guys :)
This is how you can nest multiple bool queries in one outer bool query
this using Kibana,
bool indicates we are using boolean
must is for AND
should is for OR
GET my_inedx/my_type/_search
{
"query" : {
"bool": { //bool indicates we are using boolean operator
"must" : [ //must is for **AND**
{
"match" : {
"description" : "some text"
}
},
{
"match" :{
"type" : "some Type"
}
},
{
"bool" : { //here its a nested boolean query
"should" : [ //should is for **OR**
{
"match" : {
//ur query
}
},
{
"match" : {}
}
]
}
}
]
}
}
}
This is how you can nest a query in ES
There are more types in "bool" like,
Filter
must_not
I recently had to solve this problem too, and after a LOT of trial and error I came up with this (in PHP, but maps directly to the DSL):
'query' => [
'bool' => [
'should' => [
['prefix' => ['name_first' => $query]],
['prefix' => ['name_last' => $query]],
['prefix' => ['phone' => $query]],
['prefix' => ['email' => $query]],
[
'multi_match' => [
'query' => $query,
'type' => 'cross_fields',
'operator' => 'and',
'fields' => ['name_first', 'name_last']
]
]
],
'minimum_should_match' => 1,
'filter' => [
['term' => ['state' => 'active']],
['term' => ['company_id' => $companyId]]
]
]
]
Which maps to something like this in SQL:
SELECT * from <index>
WHERE (
name_first LIKE '<query>%' OR
name_last LIKE '<query>%' OR
phone LIKE '<query>%' OR
email LIKE '<query>%'
)
AND state = 'active'
AND company_id = <query>
The key in all this is the minimum_should_match setting. Without this the filter totally overrides the should.
Hope this helps someone!
If you were using Solr's default or Lucene query parser, you can pretty much always put it into a query string query:
POST test/_search
{
"query": {
"query_string": {
"query": "(( name:(+foo +bar) OR info:(+foo +bar) )) AND state:(1) AND (has_image:(0) OR has_image:(1)^100)"
}
}
}
That said, you may want to use a boolean query, like the one you already posted, or even a combination of the two.
$filterQuery = $this->queryFactory->create(QueryInterface::TYPE_BOOL, ['must' => $queries,'should'=>$queriesGeo]);
In must you need to add the query condition array which you want to work with AND and in should you need to add the query condition which you want to work with OR.
You can check this: https://github.com/Smile-SA/elasticsuite/issues/972

Elastic Search: No query registered for [or]

Can anyone help me with this query? I am getting an error returned: "No query registered for [or]" Did I structure this wrong? It's supposed to filter all results where area is 530 and start is blank OR area is 530 and start is "06192013", then based on that boost the document with the other filters.
{
"query": {
"custom_filters_score": {
"query": {
"bool": {
"must": [
{"field":{"sector":"sector1"}},
{"term":{"user_type":"ghost"}},
{"term":{"area":"530"}}
]
},
"filter":{
"or": [
{
"and": [
{"term":{"area":"530"}},
{"term":{"start":"06192013"}}
]
},
{
"and": [
{"term":{"area":"530"}},
{"term":{"start":"blank"}}
]
}
]
}
},
"filters": [
{"filter":{"term":{"relevance" :5726}},"boost":"1000"},
{"filter":{"term":{"relevance2":5726}},"boost":"100"}
],
"score_mode":"total"
}
}
}
The filter object that contains the or filter is misplaced. I guess you wanted to use a filtered query containing the bool query and the or filter like this:
{
"filtered" : {
"bool": {
"must": [
{"field":{"sector":"sector1"}},
{"term":{"user_type":"ghost"}},
{"term":{"area":"530"}}
]
},
"filter" : {
"or": [
{
"and": [
{"term":{"area":"530"}},
{"term":{"start":"06192013"}}
]
},
{
"and": [
{"term":{"area":"530"}},
{"term":{"start":"blank"}}
]
}
]
}
}
}

Creating an Elastic Search AND OR Query

I am trying to write a query that requires "area" to be 530 and "starts" to be 06192013 OR "area" to be "530" and "starts" to be "blank". Additionally, in both of those scenarios "space" to be "top" OR "space2" to be "bottom". This query seems to be grabbing anything that matches any of these scenarios, how do I change it to make it work like I would like?
{
"size":25,
"from":0,
"query": {
"custom_filters_score": {
"query": {
"filtered": {
"query": {
"bool": {
"must": [
{"term":{"type":"ghost"}},
{"term":{"area":"530"}}
]
}
},
"filter" : {
"or": [
{"terms":{"space": ["top"]}},
{"terms":{"space2":["bottom"]}},
{
"and": [
{"term":{"area":"530"}},
{"term":{"start":"06192013"}}
]
},
{
"and": [
{"term":{"area":"530"}},
{"term":{"starts":"blank"}}
]
}
]
}
}
},
"filters": [
{"filter":{"term":{"filter1":5743}}, "boost":"1000"},
{"filter":{"term":{"filter2":4451}}, "boost":"64"},
{"filter":{"term":{"filter3":["tech"]}}, "boost":"16"},
{"filter":{"terms":{"filter4":[]}}, "boost":"8"},
{"filter":{"terms":{"filter5":[]}}, "boost":"5"},
{"filter":{"term":{"access":"1"}}, "boost":"2"}
],
"score_mode":"total"
}
}
}
You are almost there! Try this:
...
"query" : { "match_all":{} },
"filter" : {
"and": [
{
"or": [
{"term":{"space": "top"}},
{"term":{"space2":"bottom"}}
]
},
{
"and": [
{"term":{"area":"530"}},
{
"or": [
{"term":{"start":"06192013"}},
{"term":{"starts":"blank"}}
]
}
]
}
]
}
...
Unless you need scoring (which it doesnt look like) you should do all of this with filters, they don't calculate scores and are faster because of this.
The "query" : { "match_all":{} } will end up giving all docs witch match the filter the same score.
Note: I also turned your terms query with an array of one element to a term query...

Resources