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


(Gonyoda) #1

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


(Shay Banon) #2

You don't need to escape the - sign. Gist a full recreation and we can have a look at it (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


(hans.melgers) #3

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


(system) #4