# Entity Centric index on two value

**URL:** https://discuss.elastic.co/t/entity-centric-index-on-two-value/109678
**Category:** Elasticsearch
**Created:** [November 30, 2017, 3:28am UTC](https://discuss.elastic.co/t/entity-centric-index-on-two-value/109678 "2017-11-30T03:28:55Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![DILIP\_SHARMA](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dilip_sharma/32/42672_2.png) [@DILIP\_SHARMA](https://discuss.elastic.co/u/DILIP_SHARMA)
#### Post date: [November 30, 2017, 3:28am UTC](https://discuss.elastic.co/t/entity-centric-index-on-two-value/109678/1 "2017-11-30T03:28:55Z")

</div>

Hi i have one basic question ,  
i am using entity centric index to create entity index for salesman my data look like -

```
"s1","salesman01","2017-06-22 00:00:00.000","2017-06-22 16:10:23.000","2017-06-22 16:11:44.000","5"
"s2","salesman01","2017-06-22 00:00:00.000","2017-06-22 17:10:23.000","2017-06-22 17:21:44.000","8"
"s3","salesman01","2017-06-22 00:00:00.000","2017-06-22 13:10:23.000","2017-06-22 14:11:44.000","7"
"s4","salesman01","2017-06-22 00:00:00.000","2017-06-22 12:10:23.000","2017-06-22 13:11:44.000","4"
"s5","salesman01","2017-06-22 00:00:00.000","2017-06-22 15:10:23.000","2017-06-22 16:10:44.000","2"
"s1","salesman01","2017-06-22 00:00:00.000","2017-06-22 20:10:23.000","2017-06-22 20:12:44.000","1"
"s2","salesman01","2017-06-22 00:00:00.000","2017-06-22 20:10:23.000","2017-06-22 21:21:44.000","3"
"s3","salesman01","2017-06-22 00:00:00.000","2017-06-22 19:10:23.000","2017-06-22 20:11:44.000","5"
"s4","salesman01","2017-06-22 00:00:00.000","2017-06-22 19:10:23.000","2017-06-22 20:11:44.000","8"
"s5","salesman01","2017-06-22 00:00:00.000","2017-06-22 21:10:23.000","2017-06-22 22:11:44.000","5"
"s1","salesman01","2017-06-23 00:00:00.000","2017-06-23 11:10:23.000","2017-06-23 12:11:44.000","5"
"s2","salesman01","2017-06-23 00:00:00.000","2017-06-23 12:10:23.000","2017-06-23 13:21:44.000","8"
"s3","salesman01","2017-06-23 00:00:00.000","2017-06-23 13:10:23.000","2017-06-23 14:11:44.000","7"
"s4","salesman01","2017-06-23 00:00:00.000","2017-06-23 14:10:23.000","2017-06-23 15:11:44.000","4"
"s5","salesman01","2017-06-23 00:00:00.000","2017-06-23 15:10:23.000","2017-06-23 16:10:44.000","2"
"s1","salesman01","2017-06-23 00:00:00.000","2017-06-23 16:10:23.000","2017-06-23 17:12:44.000","1"
"s2","salesman01","2017-06-23 00:00:00.000","2017-06-23 17:10:23.000","2017-06-23 18:21:44.000","3"
"s3","salesman01","2017-06-23 00:00:00.000","2017-06-23 18:10:23.000","2017-06-23 19:11:44.000","5"
"s4","salesman01","2017-06-23 00:00:00.000","2017-06-23 19:10:23.000","2017-06-23 20:11:44.000","8"
"s5","salesman01","2017-06-23 00:00:00.000","2017-06-23 20:10:23.000","2017-06-23 21:11:44.000","5"

```

my painless script -

```
  //Copy doc source to local variable with shorter name
	def docSrc = ctx._source;

	if("create".equals(ctx.op)){
		//initialize entity state
		docSrc.Salesmans = [];
// docSrc.profile = "newbie";
	}    
	
    // Convert seller array into map for ease of manipulation
    def SalesmanMap =[:];
    for (salesman in docSrc.Salesmans) {
      SalesmanMap[salesman.SalesmanCode]=salesman;
    }
    
    // Consolidate latest batch of events

//def sf1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
def sf2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

//def dt1=sf1.parse("2017-06-22 15:12:00");
//def dt2 = sf1.parse("2017-06-24 15:15:00");

//def dtdif1=dt2.getTime()-dt1.getTime();

def minstarttime;
def maxendtime;

    for (event in params.events) {

        def salesman =SalesmanMap[event.SalesmanCode];
	def datediff =(sf2.parse(event.VisitEndDateTime).getTime()-sf2.parse(event.VisitStartDateTime).getTime()) / 60000;	
if(minstarttime ==null)
	{
	minstarttime=sf2.parse(event.VisitStartDateTime);
	maxendtime = sf2.parse(event.VisitEndDateTime);
docSrc.MinVisitStartTime = event.VisitStartDateTime;
docSrc.MaxVisitEndTime = event.VisitEndDateTime;

	}	

	if(salesman == null)
{	
        salesman=[
          "SalesmanCode": event.SalesmanCode,
	  "VisitDate": event.VisitDate,
          "timespend":datediff];
	

        SalesmanMap[salesman.SalesmanCode]=salesman;
}	else
{
salesman.timespend +=datediff;
}

if(minstarttime.getTime() > sf2.parse(event.VisitStartDateTime).getTime())
{
minstarttime= sf2.parse(event.VisitStartDateTime);
docSrc.MinVisitStartTime = event.VisitStartDateTime;

}
if(maxendtime.getTime() < sf2.parse(event.VisitEndDateTime).getTime())
{
maxendtime=sf2.parse(event.VisitEndDateTime);
docSrc.MaxVisitEndTime = event.VisitEndDateTime;

}
docSrc.VisitDate = event.VisitDate;
docSrc.TimeinMarket=salesman.timespend;

}    
docSrc.Salesmans=SalesmanMap.values();

```

but as per current script its create one record per salesman ,  
now i wanted to create entity index salesman and date wise , means entity index should have record salemancode and date wise

one approach is i should create one extra field when uploading index with combination of salesman code and date and apply centric index on that .

there is any other way of doing this ?

thanks .

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [November 30, 2017, 9:36am UTC](https://discuss.elastic.co/t/entity-centric-index-on-two-value/109678/2 "2017-11-30T09:36:43Z")

</div>

> [@DILIP\_SHARMA](#):
>
> now i wanted to create entity index salesman and date wise

That would mean that the `_id` of your documents would need to be a concatenation of salesmanID and date.  
Entity updates rely on providing the unique elasticsearch ID of the document you want to update. I'm not sure what granularity you will choose to use for date (day? week? month?...) but you'll just need to format the date appropriately for that time unit e.g.the ID `salesman01_2017-06-22`

---

<div class="post-metadata">

### Author: ![DILIP\_SHARMA](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dilip_sharma/32/42672_2.png) [@DILIP\_SHARMA](https://discuss.elastic.co/u/DILIP_SHARMA)
#### Post date: [November 30, 2017, 9:48am UTC](https://discuss.elastic.co/t/entity-centric-index-on-two-value/109678/3 "2017-11-30T09:48:42Z")

</div>

yes ,same we thought to merging both column and will create id column.  
thanks for reply .

---

<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: [December 28, 2017, 9:49am UTC](https://discuss.elastic.co/t/entity-centric-index-on-two-value/109678/4 "2017-12-28T09:49:11Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
