illug
illug

Reputation: 823

Unique search results from ElasticSearch

I am new to ElasticSearch and can't quite figure out what I want is possible or not.

I can query like this:

GET entity/_search
{
  "query": {
    "bool": { 
      "must": [
        { "match": { "searchField":   "searchValue" }}
      ]
    }
  },
      "aggs" : {
    "uniq_Id" : {
      "terms" : { "field" : "Id", "size":500 }
      }
  }
}

and it will return top search results and the term aggregation buckets. But ideally what I would like for the search results to return, is only one (perhaps the top one, does not matter) for each of unique Id's defined in the aggregation terms.

Upvotes: 0

Views: 54

Answers (1)

Kamal Kunjapur
Kamal Kunjapur

Reputation: 8840

You can make use of Terms Aggregation along with the Top Hits Aggregation to give you the result you are looking for.

Now once you do that, specify the size as 1 in the Top Hits Aggregation

Based on your query I've created sample mapping,documents, aggregation query and the response for your reference.

Mapping:

PUT mysampleindex
{
  "mappings": {
    "mydocs": {
      "properties": {
        "searchField":{
          "type": "text"
        },
        "Id": {
          "type": "keyword"
        }
      }
    }
  }
}

Sample Documents:

POST mysampleindex/mydocs/1
{
  "searchField": "elasticsearch",
  "Id": "1000"
}

POST mysampleindex/mydocs/2
{
  "searchField": "elasticsearch is awesome",
  "Id": "1000"
}

POST mysampleindex/mydocs/3
{
  "searchField": "elasticsearch is awesome",
  "Id": "1001"
}

POST mysampleindex/mydocs/4
{
  "searchField": "elasticsearch is pretty cool",
  "Id": "1001"
}

POST mysampleindex/mydocs/5
{
  "searchField": "elasticsearch is pretty cool",
  "Id": "1002"
}

Query:

POST mysampleindex/_search
{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "searchField": "elasticsearch"
          }
        }
      ]
    }
  },
  "aggs": {
    "myUniqueIds": {
      "terms": {
        "field": "Id",
        "size": 10
      },
      "aggs": {
        "myDocs": {
          "top_hits": {                     <---- Top Hits Aggregation
            "size": 1                       <---- Note this
          }
        }
      }
    }
  }
}

Sample Response:

{
  "took": 7,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 5,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "myUniqueIds": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "1000",
          "doc_count": 2,
          "myDocs": {
            "hits": {
              "total": 2,
              "max_score": 0.2876821,
              "hits": [
                {
                  "_index": "mysampleindex",
                  "_type": "mydocs",
                  "_id": "1",
                  "_score": 0.2876821,
                  "_source": {
                    "searchField": "elasticsearch",
                    "Id": "1000"
                  }
                }
              ]
            }
          }
        },
        {
          "key": "1001",
          "doc_count": 2,
          "myDocs": {
            "hits": {
              "total": 2,
              "max_score": 0.25316024,
              "hits": [
                {
                  "_index": "mysampleindex",
                  "_type": "mydocs",
                  "_id": "3",
                  "_score": 0.25316024,
                  "_source": {
                    "searchField": "elasticsearch is awesome",
                    "Id": "1001"
                  }
                }
              ]
            }
          }
        },
        {
          "key": "1002",
          "doc_count": 1,
          "myDocs": {
            "hits": {
              "total": 1,
              "max_score": 0.2876821,
              "hits": [
                {
                  "_index": "mysampleindex",
                  "_type": "mydocs",
                  "_id": "5",
                  "_score": 0.2876821,
                  "_source": {
                    "searchField": "elasticsearch is pretty cool",
                    "Id": "1002"
                  }
                }
              ]
            }
          }
        }
      ]
    }
  }
}

Notice that I am not returning any bool results in the above, the search result you are looking for comes in the form of Top Hits Aggregation.

Hope this helps!

Upvotes: 1

Related Questions