Clinton,
Thanks for the answer and sorry to make you write an example. However,
I am still having problems trying to understand what you said...
I had written a query like this before:
documents/document/_search
{
"min_score": 0,
"query": {
"has_child" : {
"type" : "comment",
"score_type" : "sum",
"boost": 1,
"query" : {
"range": {
"date": {
"lte": 20130204,
"gte": 20130201,
"boost": 1
}
}
}
}
}
} pro
Even without using function score, it does what I want, but function
score is really more flexible because I can have a condition such as (N1 <
score < N2). But even if this solves my problem for 1 has_child query, what
to do when I have multiple has_child queries? each function score would
have boost_mode = mult, right? But wouldn't that mean that if the two funct
wilion scores returned -1 I would have a positive number as final result?
I didn't properly understand how the bool query will get the score from
the function_score queries and how it will be combined. For instance, if I
use should, one of the two scores should be greater than 0, but if I use
must, both scores should be greater than 0... I was guessing my need to to
have min_score inside the must clause, not in the top level.
Would you show me a working example with more than 1 query? The example
bellow was my try to make your suggestion work.
{
"query":{
"bool": {
"must": [{
"function_score": {
"query": {
"has_child" : {
"type" : "comment",
"score_type" : "sum",
"boost": 1,
"query" : {
"range": {
"date": {
"lte": 20130204,
"gte": 20130201,
"boost": 1
}
}
}
}
},
"boost": "1",
"script_score" : {
"script" : "_score<3?-1:1"
},
"boost_mode":"mult"
}},{
"function_score": {
"query": {
"has_child" : {
"type" : "comment",
"score_type" : "sum",
"boost": 1,
"query" : {
"constant_score": {
"filter": {
"term": { "text": "finally" }
}
}
}
}
},
"boost": "1",
"script_score" : {
"script" : "_score<1?-1:1"
},
"boost_mode":"mult"
}}
]
}
}
}
================================
curl -XPUT http://localhost:9200/documents
curl -XPUT http://localhost:9200/documents/comment/_mapping -d '{
"comment" : {
"_parent" : {
"type" : "document"
}
}
}'
curl -XPUT http://localhost:9200/documents/document/1 -d '{
"title": "Parent document with comments 1",
"content": "several test comments :D"
}'
curl -XPUT http://localhost:9200/documents/document/2 -d '{
"title": "Parent document with comments 2",
"content": "several test comments :D"
}'
curl -XPUT http://localhost:9200/documents/document/3 -d '{
"title": "Parent document with comments 3",
"content": "several test comments :D"
}'
curl -XPUT http://localhost:9200/documents/document/4 -d '{
"title": "Parent document with comments 4",
"content": "several test comments :D"
}'
curl -XPUT 'http://localhost:9200/documents/comment/1?parent=1' -d '{
"root_id": 1,
"text": "Oh my god ...",
"author": "John Doe",
"date": [20130201, 20130202],
"field1": 1,
"field2": 10
}'
curl -XPUT 'http://localhost:9200/documents/comment/2?parent=1' -d '{
"root_id": 1,
"text": "Finally!",
"author": "Jane Roe",
"date": [20130202],
"field1": 1,
"field2": 10
}'
curl -XPUT 'http://localhost:9200/documents/comment/3?parent=1' -d '{
"root_id": 1,
"text": "text 3",
"author": "Walter",
"date": [20130201],
"field1": 1,
"field2": 20
}'
curl -XPUT 'http://localhost:9200/documents/comment/4?parent=1' -d '{
"root_id": 1,
"text": "text 4",
"author": "Mario",
"date": [20130112],
"field1": 1,
"field2": 20
}'
curl -XPUT 'http://localhost:9200/documents/comment/5?parent=1' -d '{
"root_id": 1,
"text": "text 5",
"author": "Maria",
"date": [20130203],
"field1": 2,
"field2": 10
}'
curl -XPUT 'http://localhost:9200/documents/comment/6?parent=1' -d '{
"root_id": 1,
"text": "text 6",
"author": "Marcel",
"date": [20130205],
"field1": 2,
"field2": 20
}'
curl -XPUT 'http://localhost:9200/documents/comment/7?parent=2' -d '{
"root_id": 2,
"text": "text 7",
"author": "Marcelo",
"date": [20130204],
"field1": 2,
"field2": 20
}'
Best regards,
Marcelo.
2013/10/18 Clinton Gormley clint@traveljury.com
Hi Marcelo
I've written up a working example of how to use the function_score
query
to achieve what you want:
test.asciidoc · GitHub
You can only use min_score
at the top level, but I've used the
function_score
to multiply the real score by -1 if a parent document
doesn't have enough children (cutoff).
I also demonstrate how you could combine this technique with a full text
query on the parent docs.
clint
On 18 October 2013 10:30, Luca Cavanna cavannaluca@gmail.com wrote:
HI Marcelo,
you can use a bool query
to achieve the same, can't you? Must clauses are mandatory, should clauses
are optional, and you can control how many of them must match to achieve
something that's actually between a logic AND and a logic OR.
We also have the bool filter,
which is good in most of the cases to do the same with filters, when you
don't care about scoring and you just want to filter out results. The
reason why we have and, or and not filters too is the way they get executed
which is, briefly, less cache-friendly compared to the bool filter, but
helps when you have expensive filters that are not easily cacheable and you
want to control the execution order of those. Have a look at this article
that clearly explains the difference.
Cheers
Luca
On Thursday, October 17, 2013 11:37:28 PM UTC+2, Marcelo Elias Del Valle
wrote:
Ivan,
But if I cannot user filter query executed after score is
calculated, is there another way of executing boolean conditions (like AND,
OR, NOT) on query results?
Best regards,
Marcelo.
2013/10/17 Ivan Brusic iv...@brusic.com
I have not looked at your query, but you can always apply a filter to
your query after it executes.
With a filtered query, the filter happens first, which is normally
optimal because you do not want to score documents that will eventually be
filtered out. There are a few use cases where a filtered query (aka pre
filter) does not work and your use case might be one of them. Of course,
you can have both a filtered query and then a standard filter.
Cheers,
Ivan
On Thu, Oct 17, 2013 at 1:50 PM, Marcelo Elias Del Valle <
mval...@gmail.com> wrote:
Ivan,
But I need the filtering. What I am trying to do is using the
score to filter based on result frequency.
I am already using function_score, as follows bellow. However, I
need to bring only parent docs which have at least N childs.
Ins't the query inside the "or" filter executed before the filter
itself? I was guessing min_score should apply to the "function_score" query
bellow...
{
"query": {
"filtered" : {
"query": {
"match_all": {
}
},
"filter" : {
"or" : [
{
"min_score": 0,
"query": {
"function_score": {
"query": {
"has_child" : {
"type" : "comment",
"score_type" : "sum",
"boost": 1,
"query" : {
"range": {
"date": {
"lte": 20130204,
"gte": 20130201,
"boost": 1
}
}
}
}
},
"boost": "1",
"script_score" : {
"script" : "_score"
},
"boost_mode":"replace"
}
}
}
]
}
}
}
}
Best regards,
Marcelo.
Em quinta-feira, 17 de outubro de 2013 17h42min15s UTC-3, Ivan Brusic
escreveu:
The filter that is part of a filtered query is executed before the
query, therefore the filter does not have any scores since scoring happens
during the query.
min_score is the easiest solution. Have you looked at the custom
filter score, which has been replaced by the function score query (but I
have never used so I cannot comment on it)? Perhaps if you adjust your
scoring, there would be no need to do additional filtering.
Cheers,
Ivan
On Thu, Oct 17, 2013 at 1:20 PM, Marcelo Elias Del Valle <
mval...@gmail.com> wrote:
I would like to filter results based on score, but min_score only
works on top of my search, it doesn 't work inside a filtered query.
Is there any other way to not return results with score = 0, besides
min_score?
--
You received this message because you are subscribed to the Google
Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it,
send an email to elasticsearc...@**googlegroups.**com.
For more options, visit https://groups.google.com/**grou**ps/opt_outhttps://groups.google.com/groups/opt_out
.
--
You received this message because you are subscribed to the Google
Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to elasticsearc...@**googlegroups.com.
For more options, visit https://groups.google.com/**groups/opt_outhttps://groups.google.com/groups/opt_out
.
--
You received this message because you are subscribed to a topic in the
Google Groups "elasticsearch" group.
To unsubscribe from this topic, visit https://groups.google.com/d/**
topic/elasticsearch/**qLXupHz0PKo/unsubscribehttps://groups.google.com/d/topic/elasticsearch/qLXupHz0PKo/unsubscribe
.
To unsubscribe from this group and all its topics, send an email to
elasticsearc...@**googlegroups.com.
For more options, visit https://groups.google.com/**groups/opt_outhttps://groups.google.com/groups/opt_out
.
--
Marcelo Elias Del Valle
http://mvalle.com - @mvallebr
--
You received this message because you are subscribed to the Google Groups
"elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to elasticsearch+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to a topic in the
Google Groups "elasticsearch" group.
To unsubscribe from this topic, visit
https://groups.google.com/d/topic/elasticsearch/qLXupHz0PKo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to
elasticsearch+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
--
Marcelo Elias Del Valle
http://mvalle.com - @mvallebr
--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.