ProgLearner ProgLearner - 1 month ago 20
C# Question

Elastic Search: JSON to NEST query without duplicates

How to convert json query to nest query below and remove duplicates?

{
"size": 30,
"query": {
"multi_match": {
"query": "london",
"operator": "OR",
"fields": [
"name",
"venueTown"
]
}
}
}

Answer

You can simplify the aggregations section a little and put the top hits in

var searchResult = client.Search<SearchResult>(request => request
    // Your existing query below...
    //.Query(q => q)
    .Size(0)
    .Aggregations(a => a
        // simplify the terms aggregation
        .Terms("query", tr => tr
            .Field("name")
            .Size(30)
        )
        // Add the top hits aggregation
        .TopHits("top", th => th
            .Size(1)
        )
    )
);