Getting Distinct Values


(vijuitech) #1

Hi,

I have a problem on selecting the distinct data based on the name:

The problem is

In a Table which contains the following data:

ID | Name | Age |

Rank

1 Abc 22 1

2 cbc 20
2

3 Abc 22
1

4 ccc 21
3

5 Abc 22
1

If it is a MySQL Database.. We can select the distinct value by

SELECT * FROM Table GROUP BY Name;

Is there any alternative in elasticsearch to select the Distinct
Value so we get the output as

ID | Name | Age |

Rank

1 Abc 22 1

2 cbc 20
2

4 ccc 21
3

Cheers!!

George


(vijuitech) #2

Hi,

Please consider this Post

I have a problem on selecting the distinct data based on the name:
The problem is

In a Table which contains the following data:

ID Name Age



1 Abc 22

2 cbc 20

3 Abc 22

4 ccc 21

5 Abc 22

If it is a MySQL Database.. We can select the distinct value by

SELECT * FROM Table GROUP BY Name;

Is there any alternative in elasticsearch to select the Distinct
Value so we get the output as

ID Name Age



1 Abc 22

2 cbc 20

4 ccc 21

Cheers!!

George


(Clinton Gormley) #3

Hi George

I have a problem on selecting the distinct data based on the name:
The problem is

In a Table which contains the following data:

ID Name Age



1 Abc 22
2 cbc 20
3 Abc 22
4 ccc 21
5 Abc 22

If it is a MySQL Database.. We can select the distinct value by

SELECT * FROM Table GROUP BY Name;

First, the query that you are using here is a MySQL "feature" (read
"bug"). You are grouping by name, but not telling the database what to
do with the ungrouped columns.

So it should read something like this (depending on what you actually
want):

SELECT Name, min(ID), min(Age) from Table GROUP BY Name.

The ElasticSearch equivalent is called "facets"
http://www.elasticsearch.org/guide/reference/api/search/facets/
and the particular facet that you should use depends on what other data
you need.

For instance, for just distinct terms, look at the terms facet:
http://www.elasticsearch.org/guide/reference/api/search/facets/terms-facet.html

If you need to group on one field and process another numeric field,
have a look at the terms stats facet:
http://www.elasticsearch.org/guide/reference/api/search/facets/terms-stats-facet.html

etc

clint

Is there any alternative in elasticsearch to select the Distinct
Value so we get the output as

ID Name Age



1 Abc 22

2 cbc 20

4 ccc 21

Cheers!!

George


(vijuitech) #4

Thanks a lot Clinton :slight_smile:

Cheers!!

George

On Mar 16, 11:54 am, Clinton Gormley cl...@traveljury.com wrote:

Hi George

I have a problem on selecting the distinct data based on the name:
The problem is

In a Table which contains the following data:

ID Name Age



1 Abc 22
2 cbc 20
3 Abc 22
4 ccc 21
5 Abc 22

If it is a MySQL Database.. We can select the distinct value by

SELECT * FROM Table GROUP BY Name;

First, the query that you are using here is a MySQL "feature" (read
"bug"). You are grouping by name, but not telling the database what to
do with the ungrouped columns.

So it should read something like this (depending on what you actually
want):

SELECT Name, min(ID), min(Age) from Table GROUP BY Name.

The ElasticSearch equivalent is called "facets"http://www.elasticsearch.org/guide/reference/api/search/facets/
and the particular facet that you should use depends on what other data
you need.

For instance, for just distinct terms, look at the terms facet:http://www.elasticsearch.org/guide/reference/api/search/facets/terms-...

If you need to group on one field and process another numeric field,
have a look at the terms stats facet:http://www.elasticsearch.org/guide/reference/api/search/facets/terms-...

etc

clint

Is there any alternative in elasticsearch to select the Distinct
Value so we get the output as

ID Name Age



1 Abc 22

2 cbc 20

4 ccc 21

Cheers!!

George


(system) #5