# Comparing two terms

**URL:** https://discuss.elastic.co/t/comparing-two-terms/46897
**Category:** Kibana
**Created:** [April 10, 2016, 2:41pm UTC](https://discuss.elastic.co/t/comparing-two-terms/46897 "2016-04-10T14:41:39Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![astickler](https://avatars.discourse-cdn.com/v4/letter/a/53a042/32.png) [@astickler](https://discuss.elastic.co/u/astickler)
#### Post date: [April 10, 2016, 2:41pm UTC](https://discuss.elastic.co/t/comparing-two-terms/46897/1 "2016-04-10T14:41:39Z")

</div>

In Kibana (e.g Visualise), is it possible to compare one term with another?

For example, I have indexed records that contain an indoorTemp value and a setpointTemp value - I have a line chart that shows the indoorTemp over time (using the Date histogram on the x-axis), but I would like to find just those times (e.g. hour buckets), where the value of indoorTemp exceeded that of setpointTemp.

I would settle for being able to show buckets where indoorTemp was greater than a constant (I can't see how do do that either), but would prefer to use setpointTemp.

Many thanks,  
Andrew

P.S. I am using the Elastic Cloud trial at the moment.

---

<div class="post-metadata">

### Author: ![LeeDr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leedr/32/9289_2.png) [@LeeDr](https://discuss.elastic.co/u/LeeDr)
#### Post date: [April 11, 2016, 2:40pm UTC](https://discuss.elastic.co/t/comparing-two-terms/46897/2 "2016-04-11T14:40:42Z")

</div>

You can create a scripted field that takes the difference between those two fields (assume each document has both fields). And then you can filter data like `tempDiff: >0` or \<0 depending on which way you subtract them.

1. Go to Settings
2. Select your index pattern name near the upper-left corner
3. click the "Scripted fields" tab
4. There's an example on the page. Just take the difference of the value of your two fields and then try using a filter on that scripted field.

Regards,  
Lee

---

<div class="post-metadata">

### Author: ![astickler](https://avatars.discourse-cdn.com/v4/letter/a/53a042/32.png) [@astickler](https://discuss.elastic.co/u/astickler)
#### Post date: [April 11, 2016, 4:20pm UTC](https://discuss.elastic.co/t/comparing-two-terms/46897/3 "2016-04-11T16:20:55Z")

</div>

Hi Lee - thanks for the suggestion. Unfortunately, although I have successfully created the scripted field and can see it has valid values, I can't get it to filter. Please take a look at my screenshots and see if you can work out what I have missed.  
Regards,  
Andrew

 ![](https://us1.discourse-cdn.com/elastic/original/2X/3/3dd3a3885dfb119afab4cd1c4c936df157787ee1.PNG)  
 ![](https://us1.discourse-cdn.com/elastic/original/2X/e/e2aab98dc958857a011b96cc8c56fe564f3e975d.PNG)  
 ![](https://us1.discourse-cdn.com/elastic/original/2X/c/c7522f24f1b8f4a6c750e3c3c1bee64bfab6c6f6.PNG)

---

<div class="post-metadata">

### Author: ![LeeDr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leedr/32/9289_2.png) [@LeeDr](https://discuss.elastic.co/u/LeeDr)
#### Post date: [April 11, 2016, 7:21pm UTC](https://discuss.elastic.co/t/comparing-two-terms/46897/4 "2016-04-11T19:21:11Z")

</div>

Can you try to create a filter on a Visualization by clicking and then pin the filter. Here's an example. Create a histogram of your HeatDiff, and click on the bar where HeatDiff = 0. Then just edit the filter.

 ![](https://us1.discourse-cdn.com/elastic/original/2X/8/8162ef1d265054fe523c498ad95569d3a91bc5ff.gif)

---

<div class="post-metadata">

### Author: ![astickler](https://avatars.discourse-cdn.com/v4/letter/a/53a042/32.png) [@astickler](https://discuss.elastic.co/u/astickler)
#### Post date: [April 11, 2016, 7:52pm UTC](https://discuss.elastic.co/t/comparing-two-terms/46897/5 "2016-04-11T19:52:26Z")

</div>

My filter definition looks very different to yours:

{  
"script": {  
"script": "(doc['indoorTemp'].value - doc['heatSetPoint'].value) == value",  
"lang": "expression",  
"params": {  
"value": 3  
}  
}  
}

How do I modify that to do the 'greater than' test?

---

<div class="post-metadata">

### Author: ![astickler](https://avatars.discourse-cdn.com/v4/letter/a/53a042/32.png) [@astickler](https://discuss.elastic.co/u/astickler)
#### Post date: [April 11, 2016, 8:00pm UTC](https://discuss.elastic.co/t/comparing-two-terms/46897/6 "2016-04-11T20:00:48Z")

</div>

Actually, I didnt have a histogram. I now get:

```
{
  "script": {
    "script": "(doc['indoorTemp'].value - doc['heatSetPoint'].value)>=gte && (doc['indoorTemp'].value - doc['heatSetPoint'].value)<lt",
    "params": {
      "gte": 0,
      "lt": 5,
      "value": ">=0 <5"
    },
    "lang": "expression"
  }
}

```

I am still not clear how that helps me.

---

<div class="post-metadata">

### Author: ![LeeDr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leedr/32/9289_2.png) [@LeeDr](https://discuss.elastic.co/u/LeeDr)
#### Post date: [April 11, 2016, 8:50pm UTC](https://discuss.elastic.co/t/comparing-two-terms/46897/7 "2016-04-11T20:50:11Z")

</div>

Hi Andrew,

I think you want to edit your filter like this so that you only get results where your temp difference is greater than 0. You can pin this filter and go back to Discover tab and see the individual documents.

```
{
  "script": {
    "script": "(doc['indoorTemp'].value - doc['heatSetPoint'].value)>gt",
    "params": {
      "gt": 0,
      "value": ">0"
    },
    "lang": "expression"
  }
}
```

---

<div class="post-metadata">

### Author: ![astickler](https://avatars.discourse-cdn.com/v4/letter/a/53a042/32.png) [@astickler](https://discuss.elastic.co/u/astickler)
#### Post date: [April 12, 2016, 12:02pm UTC](https://discuss.elastic.co/t/comparing-two-terms/46897/8 "2016-04-12T12:02:20Z")

</div>

Lee, that seems to work, but I don't understand the syntax. Why is there a 'gt' in the script field, and also in the params node? Why, in the params node, do I have to specify the threshold (0 in this case) against the 'gt' field and the 'value' field?

Thanks,  
Andrew

---

<div class="post-metadata">

### Author: ![LeeDr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leedr/32/9289_2.png) [@LeeDr](https://discuss.elastic.co/u/LeeDr)
#### Post date: [April 12, 2016, 2:12pm UTC](https://discuss.elastic.co/t/comparing-two-terms/46897/9 "2016-04-12T14:12:24Z")

</div>

Hi Andrew,

I'm pretty new on this scripting but it looks like the "gt" in the script is a variable and it's value is set to 0 in the params.

But I'm confused why the params would have the `"value": ">0"` part. It wouldn't make sense to substitute `">0"` in place of `value` in the script. I'll try to find some documentation or expert help on it.

Here's one reference of scripting which does not have the value param;  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-script-fields.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-script-fields.html)

In a quick test I just did, I removed the value param and still had the same results.

The more I think about it, the more I think that "value" param is just naming the result.

Regards,  
Lee

---

<div class="post-metadata">

### Author: ![astickler](https://avatars.discourse-cdn.com/v4/letter/a/53a042/32.png) [@astickler](https://discuss.elastic.co/u/astickler)
#### Post date: [April 12, 2016, 4:37pm UTC](https://discuss.elastic.co/t/comparing-two-terms/46897/10 "2016-04-12T16:37:25Z")

</div>

But this query syntax isn't even using the heatDiff scripted field I created earlier. Lets go back to basics - if I just wanted to filter on my indoorTemp field being greater than 70, how would I format that in the query bar?  
Thanks,  
Andrew

---

<div class="post-metadata">

### Author: ![LeeDr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leedr/32/9289_2.png) [@LeeDr](https://discuss.elastic.co/u/LeeDr)
#### Post date: [April 12, 2016, 6:17pm UTC](https://discuss.elastic.co/t/comparing-two-terms/46897/11 "2016-04-12T18:17:42Z")

</div>

Hi Andrew,

It took the script `doc['indoorTemp'].value - doc['heatSetPoint'].value` of your scripted field and put it in the filter. I would guess that's because Kibana knows about your heatDiff field but Elasticsearch doesn't.

If you just want to filter based on numeric fields \> or \< than something you can just put `indoorTemp:>70` in the query bar, or create/edit a filter like this;

```
{
  "range": {
    "bytes": {
      "gt": 70
    }
  }
}
```

---

<div class="post-metadata">

### Author: ![astickler](https://avatars.discourse-cdn.com/v4/letter/a/53a042/32.png) [@astickler](https://discuss.elastic.co/u/astickler)
#### Post date: [April 13, 2016, 10:11am UTC](https://discuss.elastic.co/t/comparing-two-terms/46897/12 "2016-04-13T10:11:51Z")

</div>

Thanks Lee - that helps. I hadn't appreciated that query bar just applied to elastic search, but it kind of makes sense.  
Andrew

---

<div class="post-metadata">

### Author: ![Pankaj\_Lad](https://avatars.discourse-cdn.com/v4/letter/p/6de8d8/32.png) [@Pankaj\_Lad](https://discuss.elastic.co/u/Pankaj_Lad)
#### Post date: [January 16, 2017, 1:03pm UTC](https://discuss.elastic.co/t/comparing-two-terms/46897/13 "2017-01-16T13:03:03Z")

</div>

Hi Lee,

I am not a technical person. The question I had is -

I want to compare two fields which have string like data in it. I tried creating a script - "doc['country\_1'].value != doc['country\_2'].value" both these fields are present in 1 index. When I tried to create a visualisation using the script I got shard failure message. Can you suggest how do we go ahead with this comparison.

---

<div class="post-metadata">

### Author: ![LeeDr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leedr/32/9289_2.png) [@LeeDr](https://discuss.elastic.co/u/LeeDr)
#### Post date: [January 18, 2017, 9:12pm UTC](https://discuss.elastic.co/t/comparing-two-terms/46897/14 "2017-01-18T21:12:13Z")

</div>

Hi Pankaj,

Is your script `Language: painless` and your `Type: boolean`?  
Selecting `Format: Boolean` didn't seem to make any difference in my test.

Does every doc in your index have that field? If not, I think you would have to test them both in your Painless script.  
Let me know if you need help with that.

Regards,  
Lee

---

<div class="post-metadata">

### Author: ![Pankaj\_Lad](https://avatars.discourse-cdn.com/v4/letter/p/6de8d8/32.png) [@Pankaj\_Lad](https://discuss.elastic.co/u/Pankaj_Lad)
#### Post date: [January 19, 2017, 9:39am UTC](https://discuss.elastic.co/t/comparing-two-terms/46897/15 "2017-01-19T09:39:45Z")

</div>

> [@LeeDr](#):
>
> Language: painless

Thanks Lee. I am not sure, let me check with My tech team and get back to you.

---

<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 6, 2017, 1:33pm UTC](https://discuss.elastic.co/t/comparing-two-terms/46897/16 "2017-07-06T13:33:23Z")

</div>


