0

I need distinct records from elastic search query like we do in database as

select distinct(title) from tablename

instead of

select count(distinct(title)) from tablename

I have used aggregation but it does not solve my problem because it gives only count , i need some search filter or anything else that give me result like this, if i have 3 records:

id   title    
1    ab is
2    sdf sdf
3    ab is

and then i fetch from query it gives me output as :

hits : [
   1 : ab is
   2 : sdf sdf
]

query i am applying is

{ "query": {
    "match" : {"title" : "sing alongs"}
  },
 "from": 0,
 "aggs": {
   "genres": {
     "terms": {
       "field": "title"
      }
    }
 },
 "size": 30,
 "_source": [  
   "title"   
 ]}

and result i am getting is

{
{
  "_index": "events",
  "_type": "Activity",
  "_id": "F-m5MGABRY8M87iCVeBZ",
  "_score": 21.652287,
  "_source": {
  "title": "Sing Alongs"
}
} ,
{
  "_index": "events",
  "_type": "Activity",
  "_id": "6OuBwmABRY8M87iCPxNh",
  "_score": 9.333357,
  "_source": {
    "title": "Sing Along"
  }
},
{
  "_index": "events",
  "_type": "Activity",
  "_id": "Q-mEIWABRY8M87iC7Ytl",
  "_score": 9.320941,
  "_source": {
    "title": "Sing-Through"
  }
}

}

record with title

sing alongs

should appear only once here

Muhammad Aadil Banaras
  • 1,134
  • 1
  • 11
  • 21

1 Answers1

0

Use size=1. I don't recognize your query language or the environment you work in, but here is the documentation for using 'size' in a query:
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-from-size.html

MrSimple
  • 599
  • 4
  • 14
  • if i use size = 1 , it will give only 1 record , i need a solution that gives all records but the records with same title should be shown once only , not repeatedly , just as we get result by db query , select distinct(title) from table_name – Muhammad Aadil Banaras Jan 11 '18 at 10:34
  • i dont understand what you are trying to do, can you give an example with an input and expected output? i mean what you need? the entire record, or just a field? also i dont have much experience in sql, im not sure i would understand those examples. and pls show your current query – MrSimple Jan 11 '18 at 10:52
  • i have updated my question for you , kindly read question again. – Muhammad Aadil Banaras Jan 11 '18 at 10:57
  • i did read it and i'm somewhat closer, but you haven't told me if you need only that field (implying you have multiple fields in a different setting), or you need the first record (by some sorting) for each title. – MrSimple Jan 11 '18 at 11:14
  • actually i mistakenly added some records twice or even thrice when i was adding data in elastic-search , some records are added twice some are added thrice , if a record exists twice or thrice , i need to get it once whether it fetches first occurrence or last or random – Muhammad Aadil Banaras Jan 11 '18 at 11:48
  • then i think you rather need a db cleanup then a unique result query. try this: https://stackoverflow.com/questions/30574868/elasticsearch-remove-duplicates-from-index right now i can't check it out, but it seems promising – MrSimple Jan 11 '18 at 12:19