I"m trying to get a function_score working, but it appears to be ignored. Here's my query:
{
"explain" : true,
"query": {
"function_score": {
"functions": [
{
"script_score": {
"script": "_score * 999 * doc['total_digital_items_sold'].value"
}
}
],
"query": {
"filtered": {
"query": {
"query_string": {
"query": "photo",
"fields": [ "title^3" ],
"use_dis_max": true,
"analyzer": "snowball"
}
},
"filter": { "and": [ { } ] }
}
}
}
}
}
}
It appears to not affect the results, nor show up in the explain. Any ideas on what I'm doing wrong?
Related
Coming from here i'm asking myselve for the elasticsearch syntax for such querys:
WHERE text LIKE "%quick%"
AND text LIKE "%brown%"
AND text LIKE "%fox%"
my try (unfortunately without success)
"query": {
"bool": {
"filter": [
{
"bool": {
"must": [
{
"terms": {
"text": [
"*quick*",
"*brown*",
"*fox*"
]
}
}
]
}
}
]
}
}
Try using bool and wildcard to do such a query.
{
"query": {
"bool": {
"must": [
{
"wildcard": {
"text": "*quick*"
}
},
{
"wildcard": {
"text": "*brown*"
}
},
{
"wildcard": {
"text": "*fox*"
}
}
]
}
}
}
Wildcard Query Matches documents that have fields matching a wildcard expression (not analyzed). Supported wildcards are *, which matches any character sequence (including the empty one), and ?, which matches any single character.
That's what you're looking for. Just put desired amount of wildcard queries in your bool/must:
{
"query": {
"bool": {
"must": [
{
"wildcard": {
"text": {
"value": "*quick*"
}
}
},
{
"wildcard": {
"text": {
"value": "*brown*"
}
}
},
{
"wildcard": {
"text": {
"value": "*fox*"
}
}
}
]
}
}
}
How can I get this simple SQL query running on Elasticsearch?
SELECT * FROM [mytype] where (id=123 and cid = classroomIdA) or
(id=234 and cid = classroomIdB) or (id=345 and cid = classroomIdC)
I'm really having troubles with its syntax, multi-match queries doesn't work in my case. What type of query should I use?
The right way to do it is to combine bool/should (for the outer OR conditions) and bool/filter (for the inner AND conditions) together.
POST mytype/_search
{
"query": {
"bool": {
"minimum_should_match": 1,
"should": [
{
"bool": {
"filter": [
{
"term": {
"id": 123
}
},
{
"term": {
"cid": "classroomIdA"
}
}
]
}
},
{
"bool": {
"filter": [
{
"term": {
"id": 234
}
},
{
"term": {
"cid": "classroomIdB"
}
}
]
}
},
{
"bool": {
"filter": [
{
"term": {
"id": 345
}
},
{
"term": {
"cid": "classroomIdC"
}
}
]
}
}
]
}
}
}
UPDATE
The equivalent ES 1.7 query would be (just replace bool/filter by bool/must):
POST mytype/_search
{
"query": {
"bool": {
"minimum_should_match": 1,
"should": [
{
"bool": {
"must": [
{
"term": {
"id": 123
}
},
{
"term": {
"cid": "classroomIdA"
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"id": 234
}
},
{
"term": {
"cid": "classroomIdB"
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"id": 345
}
},
{
"term": {
"cid": "classroomIdC"
}
}
]
}
}
]
}
}
}
I ran these following queries.
This query gives me proper score as expected.
{
"query": {
"filtered": {
"query": {
"function_score": {
"query": {
"bool": {
"should": [
{
"match": {
"categorytags_snow": "gyms"
}
},
{
"match": {
"locationtags_snow": "gyms"
}
},
{
"match": {
"offerings_snow": ""
}
},
{
"match": {
"title_snow": "gyms"
}
},
{
"match": {
"locationcluster_snow": "gyms"
}
},
{
"match": {
"facilities_snow": "gyms in bandra"
}
},
{
"match": {
"info_service_snow": "gyms"
}
}
]
}
},
"functions": [
{
"filter": {
"query": {
"bool": {
"should": [
{
"match": {
"categorytags_snow": "gyms"
}
}
]
}
}
},
"boost_factor": 8
},
{
"filter": {
"query": {
"bool": {
"should": [
{
"match": {
"locationtags_snow": "gyms"
}
}
]
}
}
},
"boost_factor": 10
},
{
"filter": {
"query": {
"bool": {
"should": [
{
"match": {
"offerings_snow": "gyms"
}
}
]
}
}
},
"boost_factor": 4
},
{
"filter": {
"query": {
"bool": {
"should": [
{
"match": {
"title_snow": "gyms"
}
}
]
}
}
},
"boost_factor": 12
},
{
"filter": {
"query": {
"bool": {
"should": [
{
"match": {
"locationcluster_snow": "gyms"
}
}
]
}
}
},
"boost_factor": 2
},
{
"filter": {
"query": {
"bool": {
"should": [
{
"match": {
"facilities_snow": "gyms"
}
}
]
}
}
},
"boost_factor": 2
},
{
"filter": {
"query": {
"bool": {
"should": [
{
"match": {
"info_service_snow": "gyms"
}
}
]
}
}
},
"boost_factor": 6
}
],
"boost_mode": "max",
"score_mode": "sum"
}
},
"filter": {
"bool": {
"must": [
{
"term": {
"city": "bankok"
}
}
]
}
}
}
}
}
While this query where i am using post_filter to wrap my query is not computing desired score(score is always 1)
{
"post_filter": {
"query": {
"filtered": {
"query": {
"function_score": {
"query": {
"bool": {
"should": [
{
"match": {
"categorytags_snow": "gyms"
}
},
{
"match": {
"locationtags_snow": "gyms"
}
},
{
"match": {
"offerings_snow": "gyms"
}
},
{
"match": {
"title_snow": "gyms "
}
},
{
"match": {
"locationcluster_snow": "gyms"
}
},
{
"match": {
"facilities_snow": "gyms"
}
},
{
"match": {
"info_service_snow": "gyms"
}
}
]
}
},
"functions": [
{
"filter": {
"query": {
"bool": {
"should": [
{
"match": {
"categorytags_snow": "gyms"
}
}
]
}
}
},
"boost_factor": 8
},
{
"filter": {
"query": {
"bool": {
"should": [
{
"match": {
"locationtags_snow": "gyms"
}
}
]
}
}
},
"boost_factor": 10
},
{
"filter": {
"query": {
"bool": {
"should": [
{
"match": {
"offerings_snow": "gyms"
}
}
]
}
}
},
"boost_factor": 4
},
{
"filter": {
"query": {
"bool": {
"should": [
{
"match": {
"title_snow": "gyms"
}
}
]
}
}
},
"boost_factor": 12
},
{
"filter": {
"query": {
"bool": {
"should": [
{
"match": {
"locationcluster_snow": "gyms"
}
}
]
}
}
},
"boost_factor": 2
},
{
"filter": {
"query": {
"bool": {
"should": [
{
"match": {
"facilities_snow": "gyms"
}
}
]
}
}
},
"boost_factor": 2
},
{
"filter": {
"query": {
"bool": {
"should": [
{
"match": {
"info_service_snow": "gyms"
}
}
]
}
}
},
"boost_factor": 6
}
],
"score_mode": "sum"
}
},
"filter": {"bool": {
"must": [
{
"term": {
"city": "bankok"
}
}
]
}}
}
}
}
}
Is it like query inside post_filter don't support function_score.
Thanks in advance
This is expected, as stated in the official doc and the definitive guide, anything in post_filter will run after the query has executed and will not affect the query scope, hence will not affect the score either.
Besides post_filter is a basically a filter and filters do not affect scoring, score 1 is always returned.
I'm not quite sure why the term filter "term": {"language": "Austrian"} is causing an elastic search parse exception.
The surprising thing is it works if I remove the query_string query.
Where would I put "term": {"language": "Austrian"} filter if it doesn't go there?
{
"query": {
"filtered": {
"query": {
"bool": {
"must": [
{
"terms": {
"status_type": [
"1",
"2",
"7"
]
}
}
]
}
},
"filter": {
"query": {
"query_string": {
"fields": [
[
"name",
"message"
]
],
"query": "Arnold AND Schwarz"
}
},
"term": { <-- Causes parse exception
"language": "Austrian"
}
}
}
},
"sort": [
{
"total": {
"order": "desc"
}
}
]
}
Inside your filter, you need a bool filter if you have more than one constraints, which is your case, since you have a query filter and a term filter. So the correct way of doing it is like this:
{
"query": {
"filtered": {
"query": {
"bool": {
"must": [
{
"terms": {
"status_type": [
"1",
"2",
"7"
]
}
}
]
}
},
"filter": {
"bool": { <---- add this
"must": [ <---- and this
{
"query": {
"query_string": {
"fields": [
[
"name",
"message"
]
],
"query": "Arnold AND Schwarz"
}
}
},
{
"term": {
"language": "Austrian"
}
}
]
}
}
}
},
"sort": [
{
"total": {
"order": "desc"
}
}
]
}
However, if I may add something, I would rewrite your query a bit differently and move the query_string over to the query part and the status_type term over to the filter part, it would feel more "natural". Also, in your query part you don't need a bool/must if you have only one constraint.
{
"query": {
"filtered": {
"query": {
"query_string": {
"fields": [
[
"name",
"message"
]
],
"query": "Arnold AND Schwarz"
}
},
"filter": {
"bool": {
"must": [
{
"terms": {
"status_type": [
"1",
"2",
"7"
]
}
},
{
"term": {
"language": "Austrian"
}
}
]
}
}
}
},
"sort": [
{
"total": {
"order": "desc"
}
}
]
}
I'm using ElasticSearch for logging within an application. I need to write a log viewer that filters on all the fields of my document.
My documents look like this:
"_source": {
"timestamp": 1373502012000,
"userId": 6,
"paId": 56331,
"lId": 6,
"prId": 2,
"vId": 6336,
"actionType": "LOAD_DATA"
}
actionType is an enum (Java).
I need to write a ElasticSearch equivalent to the following SQL query:
SELECT * FROM snapshot.action_log_item
WHERE timestamp BETWEEN 1372718783286 AND 1372718783286
AND userId=6
AND paId=56331
AND lId=6
AND prId=2
AND vId=6336
AND (
actionType='LOAD_DATA' OR
actionType='SAVE_DATA' OR
actionType='LOG_IN'
);
Please help me write a properly nested query and/or filter to get a result equivalent to my SQL statement.
EDIT Here's my current code (that works without the { "or"... portion).
{
"query" : {
"bool" : {
"must" : [ {
"term" : {
"userId" : 6
}
}, {
"term" : {
"lId" : 6
}
}, {
"term" : {
"vId" : 6336
}
} ]
}
},
"filter" : {
"and" : {
"filters" : [ {
"term" : {
"paId" : 56331
}
}, {
"range" : {
"timestamp" : {
"from" : 1372718783286,
"to" : 1377643583286,
"include_lower" : true,
"include_upper" : true
}
}
}, {
"or" : {
"filters" : [ {
"term" : {
"actionType" : "LOAD_DATA"
}
}, {
"term" : {
"actionType" : "SAVE_DATA"
}
}, {
"term" : {
"actionType" : "LOG_IN"
}
} ]
}
} ]
}
}
}
EDIT: The following query works. It's not the same query as above, but it returns the expected result. It seems that these filters/queries don't work on the actionType field.
{
"size": 30,
"query": {
"filtered": {
"query": {
"bool": {
"must": [
{
"term": {
"uId": 6
}
},
{
"term": {
"loId": 6
}
},
{
"term": {
"prId": 2
}
},
{
"terms": {
"paId": [
56331,
56298
],
"minimum_should_match": 1
}
}
]
}
},
"filter": {
"range": {
"timestamp": {
"from": 1372718783286,
"to": 1377643583286,
"include_lower": true,
"include_upper": true
}
}
}
}
}
}
The {or... portion should like this:
{
"or": [
{
"term": {
"actionType": "LOAD_DATA"
}
},
{
"term": {
"actionType": "SAVE_DATA"
}
},
{
"term": {
"actionType": "LOG_IN"
}
}
]
}
You can check the doc for that filter here
Edit
As I see you are having problems I rewrote your query. I hope it helps
{
"query": {
"filtered": {
"query": {
"bool": {
"must": [
{
"term": {
"userId": 6
}
},
{
"term": {
"paId": 56331
}
},
{
"term": {
"lId": 6
}
},
{
"term": {
"prId": 2
}
},
{
"term": {
"vId": 6336
}
},
{
"terms": {
"actionType": [
"LOAD_DATA",
"SAVE_DATA",
"LOG_IN"
],
"minimum_should_match": 1
}
}
]
}
},
"filter": {
"range": {
"timestamp": {
"from": 1372718783286,
"to": 1377643583286,
"include_lower": true,
"include_upper": true
}
}
}
}
}
}
Basically I put the date range as filter and the other conditions are term queries inside the must clause of the boolean query. You can see that the or part is now inside the must clause as a terms query that act as or between those 3 values.