Home > Blockchain >  Elastic Search - grouping on a field and get most matched single record
Elastic Search - grouping on a field and get most matched single record

Time:09-16

I am new in Elastic Search. I would like to get category wise most matched(Score wise) single record. Lets take a example.

  1. My Index detail

    PUT commentdetail { "mappings": { "properties": { "category" : { "type": "text" }, "comment" : { "type": "text" }

     }
    

    } }

  2. Sample Data

     category    Comment
    

    fruit       I like apple
    fruit       I like apple and banana
    fruit       I like banana and apple
    fruit       I like banana but not apple
    fruit       I like apple but not banana
    fruit       I like banana
    vegetable   I like potato
    vegetable   I like potato and tomato
    vegetable   I like tomato and potato
    vegetable   I like tomato but not potato
    vegetable   I like potato but not tomato
    vegetable   I like tomato
  1. Expected Result If I search "i like" on comment field then expected output is

    GET commentdetail/_search { "query": { "match_phrase": { "comment": "I like" } } }

Result:

    category    Comment
    -----------------------
    Fruit       I like apple
    Vegetable   I like potato

CodePudding user response:

You can achieve your required result by using a combination of terms and top hits aggregation.

But in order to use aggregation you need to update your index mapping, to have keyword type field for category

You can either update your index mapping, by following the documentation or create a new index, with the below index mapping.


Adding a working example:

Index Mapping:

PUT <index-name>
{
    "mappings": {
        "properties": {
            "category": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword"
                    }
                }
            },
            "comment": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword"
                    }
                }
            }
        }
    }
}

Search Query:

POST <index-name>/_search?filter_path=aggregations
{
    "size": 0,
    "query": {
        "match_phrase": {
            "comment": "I like"
        }
    },
    "aggs": {
        "category": {
            "terms": {
                "field": "category.keyword"
            },
            "aggs": {
                "top_comments_based_on_category": {
                    "top_hits": {
                        "sort": [
                            {
                                "_score": {
                                    "order": "desc"
                                }
                            }
                        ],
                        "size": 1
                    }
                }
            }
        }
    }
}

Search Result:

"aggregations": {
        "category": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "fruit",
                    "doc_count": 6,
                    "top_comments_based_on_category": {
                        "hits": {
                            "total": {
                                "value": 6,
                                "relation": "eq"
                            },
                            "max_score": 0.09186296,
                            "hits": [
                                {
                                    "_index": "73716847",
                                    "_id": "5",
                                    "_score": 0.09186296,
                                    "_source": {
                                        "comment": "I like apple",
                                        "category": "fruit"
                                    }
                                }
                            ]
                        }
                    }
                },
                {
                    "key": "vegetable",
                    "doc_count": 6,
                    "top_comments_based_on_category": {
                        "hits": {
                            "total": {
                                "value": 6,
                                "relation": "eq"
                            },
                            "max_score": 0.09186296,
                            "hits": [
                                {
                                    "_index": "73716847",
                                    "_id": "3",
                                    "_score": 0.09186296,
                                    "_source": {
                                        "comment": "I like potato",
                                        "category": "vegetable"
                                    }
                                }
                            ]
                        }
                    }
                }
            ]
        }
    }
  • Related