How to use aggregations?


(chimpsarehungry) #1

Is there a way to aggregate only the top 2 results based on _score for docs
with the same value in a certain field?

the hits before this aggregation would be like:

{
"_index":"myindex",
"_score":100,
"_source": {
"myfield1": "i have a twin",
"name":"fred"
}
},

{
"_index":"myindex",
"_score":50,
"_source": {
"myfield1": "i have a twin",
"name":"george"
}
},

{
"_index":"myindex",
"_score":10,
"_source": {
"myfield1": "i have a twin",
"name":"tom"
}
},

{
"_index":"myindex",
"_score":10,
"_source": {
"myfield1": "i DONT have a twin",
"name":"doug"
}
}

Then after this filter I want this ... tom removed because he has same
value for myfield1, but the lowest score. Doug stays because he has a
different value for myfield1.

{
"_index":"myindex",
"_score":100,
"_source": {
"myfield1": "i have a twin",
"name":"fred"
}
},

{
"_index":"myindex",
"_score":50,
"_source": {
"myfield1": "i have a twin",
"name":"george"
}
},

{
"_index":"myindex",
"_score":10,
"_source": {
"myfield1": "i DONT have a twin",
"name":"doug"
}
}

I would like to incorporate this agg in my query here...

{
"query": {
"function_score": {
"query": {
"bool": {
"should": [
{
"match_phrase_prefix": {
"myfield": {
"query": "i have",
"fuzziness": 1,
"slop":2,
"max_expansions": 10,
"prefix_length": 1
}
}
}
]
}
},
"boost_mode": "replace",
"functions": [
{
"script_score": {
"script": "_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 elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/0d364cae-d823-4dc2-ae34-306fa37a7455%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(chimpsarehungry) #2

I was hoping this would have the right effect ... but it didn't. Am I close?

{
"query": {
"function_score": {
"query": {
"bool": {
"should": [
{
"match_phrase_prefix": {
"myfield1": {
"query": "i have",
"fuzziness": 1,
"slop": 2,
"max_expansions": 10,
"prefix_length": 1
}
}
}
]
}
},
"boost_mode": "replace",
"functions": [
{
"script_score": {
"script": "_score * [...] "
}
}
]
}
},
"aggs": {
"myfield1": {
"terms": {
"field": "myfield1",
"size": 2,
"order": {
"max_score": "desc"
}
},
"aggs": {
"max_score": {
"max": {
"field": "_doc.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 elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/9613fb75-626a-445c-a92b-1e4fbeef84be%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(system) #3