2
votes

When I query my index with query_string, I am getting results

{
"query": {
"bool": {
"must": [ ],
"must_not": [ ],
"should": [
{
"query_string": {
"default_field": "Printer.Name",
"query": "HL-2230"
}
}
]
}
},
"from": 0,
"size": 10,
"sort": [ ],
"aggs": { }
}

But when I query using term query, I dont get any results

{
"query": {
"bool": {
"must": [ ],
"must_not": [ ],
"should": [
{
"term": {
"Printer.Name": "HL-2230"
}
}
]
}
},
"from": 0,
"size": 10,
"sort": [ ],
"aggs": { }
}

I know that term is not_analyzed and query_string is analyzed but Name is already as "HL-2230", why doesnt it match with term query? I tried also searching with "hl-2230", I still didnt get any result.

enter image description here

EDIT: mapping looks like as below. Printer is the child of Product. Not sure if this makes difference

    {
"state": "open",
"settings": {
"index": {
"creation_date": "1453816191454",
"number_of_shards": "5",
"number_of_replicas": "1",
"version": {
"created": "1070199"
},
"uuid": "TfMJ4M0wQDedYSQuBz5BjQ"
}
},
"mappings": {
"Product": {
"properties": {
"index": "not_analyzed",
"store": true,
"type": "string"
},
"ProductName": {
"type": "nested",
"properties": {
"Name": {
"store": true,
"type": "string"
}
}
},
"ProductCode": {
"type": "string"
},
"Number": {
"index": "not_analyzed",
"store": true,
"type": "string"
},
"id": {
"index": "no",
"store": true,
"type": "integer"
},
"ShortDescription": {
"store": true,
"type": "string"
},
"Printer": {
"_routing": {
"required": true
},
"_parent": {
"type": "Product"
},
"properties": {
"properties": {
"RelativeUrl": {
"index": "no",
"store": true,
"type": "string"
}
}
},
"PrinterId": {
"index": "no",
"store": true,
"type": "integer"
},
"Name": {
"store": true,
"type": "string"
}
}
},
"aliases": [ ]
}
}
1
What is the mapping of your Printer.Name field?Val
@Val Please see my edit. I added the mapping as well. Printer is basically child of Product. Could this be the problem or difference?Emil

1 Answers

3
votes

As per mapping provided by you above

"Name": {
 "store": true,
 "type": "string"
 }

Name is analysed. So HL-2230 will split into two tokens, HL and 2230. That's why term query is not working and query_string is working. When you use term query it will search for exact term HL-2230 which is not there.