# Elasticsearch: how to get distinct value from two indeices?

**URL:** https://discuss.elastic.co/t/elasticsearch-how-to-get-distinct-value-from-two-indeices/53083
**Category:** Elasticsearch
**Created:** [June 17, 2016, 3:17am UTC](https://discuss.elastic.co/t/elasticsearch-how-to-get-distinct-value-from-two-indeices/53083 "2016-06-17T03:17:32Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![mattcrawfoord](https://avatars.discourse-cdn.com/v4/letter/m/8e8cbc/32.png) [@mattcrawfoord](https://discuss.elastic.co/u/mattcrawfoord)
#### Post date: [June 17, 2016, 3:17am UTC](https://discuss.elastic.co/t/elasticsearch-how-to-get-distinct-value-from-two-indeices/53083/1 "2016-06-17T03:17:32Z")

</div>

i have index1 and index2 indexs , index2 have two types :user,tweet ,  
index1 have a index1\_loc field and index2 have indice2\_loc , index1 and index2 document will be looks like as follows,

index1 documents:

```
{"index1_loc": "10.0.0.1"},
{"index1_loc": "10.0.0.1"},
{"index1_loc": "10.1.1.1"},
{"index1_loc": "10.1.1.1"},
{"index1_loc": "10.0.0.2"}

```

index2 documents:

```
{"index2_loc": "10.0.0.1"},
{"index2_loc": "10.0.2.2"},
{"index2_loc": "10.1.1.1"},
{"index2_loc": "10.0.1.1"},
{"index2_loc": "10.0.0.2"}

```

i can use aggregation but it only returned from one index with the query

```
{
 "size": 0,
 "aggs" : {
"langs" : {
        "terms" : { 
           "field" : "index1_loc" ,
            "size": 0
                  }
          }
     }
 }'

```

in php,

```
$url = '<myurl>:<myport>/index1-*/_search?pretty';
$data = array(
		 "size" => 0,
		  "aggs" => array (
		       "Loc" => array (
		           "terms" => array ( 
		                 "field" => "index1_loc" ,
		                 "size" => 0
					 	            )
				              )
				           )
			   )

```

i want to get the results like the sql ,

```
select distinct t.loc as loc from (
   select index1_loc as loc from index1
   union 
   select index2_loc as loc from index2
) t

```

expected results document will be get as follows,

```
{"loc": "10.0.0.1"},
{"loc": "10.1.1.1"},
{"loc": "10.0.2.2"},
{"loc": "10.0.0.2"},
{"loc": "10.0.1.1"},

$url = '<myurl>:<myport>/index1-*,index2-*/_search?pretty';
$data = //howo to get distinct value from two indeices with two fields ???????

```

Thank for your help!

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 5, 2017, 10:42pm UTC](https://discuss.elastic.co/t/elasticsearch-how-to-get-distinct-value-from-two-indeices/53083/2 "2017-07-05T22:42:00Z")

</div>


