# Filtering a query using a term filter with a value containing a dash

**URL:** <https://discuss.elastic.co/t/filtering-a-query-using-a-term-filter-with-a-value-containing-a-dash/6576>\
**Category:** Elasticsearch\
**Created:** [February 2, 2012, 6:24pm UTC](https://discuss.elastic.co/t/filtering-a-query-using-a-term-filter-with-a-value-containing-a-dash/6576 "2012-02-02T18:24:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gonyoda](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gonyoda/32/3005_2.png) [@Gonyoda](https://discuss.elastic.co/u/Gonyoda)\
**Post date:** [February 2, 2012, 6:24pm UTC](https://discuss.elastic.co/t/filtering-a-query-using-a-term-filter-with-a-value-containing-a-dash/6576/1 "2012-02-02T18:24:20Z")

</div>

I've got data like this (simplified for your sanity):  
{  
MessageText : "Object reference not set to an instance of an  
object",  
Logger : "Some-Value"  
}

{  
MessageText : "Object reference not set to an instance of an  
object",  
Logger : "Another.Value"  
}

Mapping: MessageText is "string" and "analyzed" while "Logger" is  
"string" and "not\_analyzed"

I can create a "text\_phrase" query against MessageText to return these  
two rows, now I want to filter to the Logger "Some-Value". The query  
below returns no results:

{  
"query": {  
"filtered": {  
"query": {  
"text\_phrase": {  
"MessageText": "Object reference not set to an instance of  
an object"  
}  
},  
"filter": {  
"query": {  
"term": {  
"Logger": "Some-Value"  
}  
}  
}  
}  
}  
}

However, this one returns the correct results:

{  
"query": {  
"filtered": {  
"query": {  
"text\_phrase": {  
"MessageText": "Object reference not set to an instance of  
an object"  
}  
},  
"filter": {  
"query": {  
"term": {  
"Logger": "Another.Value"  
}  
}  
}  
}  
}  
}

I feel like I need to escape the "-" character somehow for the term  
filter to work or something. I can perform a term query using "Some-  
Value" and the correct results are returned, but the term filter does  
not.

Any ideas appreciated. If you need more details, let me know.

Thanks!  
John

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [February 5, 2012, 11:25am UTC](https://discuss.elastic.co/t/filtering-a-query-using-a-term-filter-with-a-value-containing-a-dash/6576/2 "2012-02-05T11:25:16Z")

</div>

You don't need to escape the `-` sign. Gist a full recreation and we can have a look at it ([Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/help)).

On Thursday, February 2, 2012 at 8:24 PM, Gonyoda wrote:

> I've got data like this (simplified for your sanity):  
> {  
> MessageText : "Object reference not set to an instance of an  
> object",  
> Logger : "Some-Value"  
> }
> 
> {  
> MessageText : "Object reference not set to an instance of an  
> object",  
> Logger : "Another.Value"  
> }
> 
> Mapping: MessageText is "string" and "analyzed" while "Logger" is  
> "string" and "not\_analyzed"
> 
> I can create a "text\_phrase" query against MessageText to return these  
> two rows, now I want to filter to the Logger "Some-Value". The query  
> below returns no results:
> 
> {  
> "query": {  
> "filtered": {  
> "query": {  
> "text\_phrase": {  
> "MessageText": "Object reference not set to an instance of  
> an object"  
> }  
> },  
> "filter": {  
> "query": {  
> "term": {  
> "Logger": "Some-Value"  
> }  
> }  
> }  
> }  
> }  
> }
> 
> However, this one returns the correct results:
> 
> {  
> "query": {  
> "filtered": {  
> "query": {  
> "text\_phrase": {  
> "MessageText": "Object reference not set to an instance of  
> an object"  
> }  
> },  
> "filter": {  
> "query": {  
> "term": {  
> "Logger": "Another.Value"  
> }  
> }  
> }  
> }  
> }  
> }
> 
> I feel like I need to escape the "-" character somehow for the term  
> filter to work or something. I can perform a term query using "Some-  
> Value" and the correct results are returned, but the term filter does  
> not.
> 
> Any ideas appreciated. If you need more details, let me know.
> 
> Thanks!  
> John

---

<div class="post-metadata">

**Author:** ![hans\_melgers](https://avatars.discourse-cdn.com/v4/letter/h/b77776/32.png) [@hans\_melgers](https://discuss.elastic.co/u/hans_melgers)\
**Post date:** [April 5, 2012, 1:23pm UTC](https://discuss.elastic.co/t/filtering-a-query-using-a-term-filter-with-a-value-containing-a-dash/6576/3 "2012-04-05T13:23:40Z")

</div>

Hello John,

Did you find a solution for this problem? I'm new to elastic search and have the same problem. The term query (2) has 0 hits in my testcase and the field query returns 10 hists as expected.

I figured out that passing either the right hand side (0000) or the right hand side (sender) actually also gives me hits in the term query.

I don't need the terms search per se but the documentations states it's more efficient so I'd like to use it if possible.

Kind regards,  
Hans

// 1  
FieldQueryBuilder fqb = fieldQuery("sender-name", "sender-0002"). defaultOperator(org.elasticsearch.index.query.  
FieldQueryBuilder.Operator.AND);

SearchResponse response = client.prepareSearch("unittest").setQuery(fqb).execute().actionGet();  
// end 1

// 2  
TermQueryBuilder tqb = termQuery("sender-name", "sender-0002");   
SearchResponse response2 = client.prepareSearch("unittest").setQuery(tqb).setTypes("document").execute().actionGet();  
// end 2

---

<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, 3:33am UTC](https://discuss.elastic.co/t/filtering-a-query-using-a-term-filter-with-a-value-containing-a-dash/6576/4 "2017-07-06T03:33:28Z")

</div>


