# Keyword Query Does not Work

**URL:** <https://discuss.elastic.co/t/keyword-query-does-not-work/101687>\
**Category:** Elasticsearch\
**Created:** [September 25, 2017, 11:25am UTC](https://discuss.elastic.co/t/keyword-query-does-not-work/101687 "2017-09-25T11:25:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ibrahim\_ozgon](https://avatars.discourse-cdn.com/v4/letter/i/ac91a4/32.png) [@Ibrahim\_ozgon](https://discuss.elastic.co/u/Ibrahim_ozgon)\
**Post date:** [September 25, 2017, 11:25am UTC](https://discuss.elastic.co/t/keyword-query-does-not-work/101687/1 "2017-09-25T11:25:42Z")

</div>

Hello everyone,

I have a problem with keyword query.

I want to filter categoryProperties by key and value.

key is"color" and value contains "mavi"

But it gives me document it contains key is "color" and value contains "Beyaz"

Do you have any idea why?

**REQUEST**

```
{
	"from": 0,
	"size": 50,
	"sort": [{
		"id": {
			"order": "asc"
		}
	}],
	"query": {
		"bool": {
			"must": [{
				"nested": {
					"query": {
						"bool": {
							"must": [{
								"nested": {
									"query": {
										"bool": {
											"should": [{
												"bool": {
													"must_not": [{
														"term": {
															"searchQuery.categoryProperties.key": {
																"value": "color"
															}
														}
													}]
												}
											},
											{
												"bool": {
													"must": [{
														"term": {
															"searchQuery.categoryProperties.key": {
																"value": "color"
															}
														}
													},
													{
														"nested": {
															"query": {
																"term": {
																	"searchQuery.categoryProperties.values.value": {
																		"value": "Mavi"
																	}
																}
															},
															"path": "searchQuery.categoryProperties.values"
														}
													}]
												}
											}]
										}
									},
									"path": "searchQuery.categoryProperties"
								}
							}]
						}
					},
					"path": "searchQuery"
				}
			}]
		}
	}
}

```

**RESPONSE**

```
{
	"took": 32,
	"timed_out": false,
	"_shards": {
		"total": 5,
		"successful": 5,
		"failed": 0
	},
	"hits": {
		"total": 1,
		"max_score": null,
		"hits": [{
			"_index": "favoritesearchsearchmodelindex_2",
			"_type": "favoritesearchsearchmodel",
			"_id": "76175",
			"_score": null,
			"_source": {
				"searchQuery": {
					"categoryProperties": [{
						"key": "maxkm",
						"values": [{
							"value": "1200000"
						}],
						"intValue": 1200000
					},
					{
						"key": "color",
						"values": [{
							"value": "Beyaz"
						}]
					},
					{
						"key": "gear",
						"values": [{
							"value": "Otomatik"
						}]
					},
					{
						"key": "category",
						"values": [{
							"value": "26553"
						}],
						"intValue": 26553
					}],
					"hasPhoto": false,
					"currency": 0,
					"city": [],
					"category": [{
						"id": 26553
					}]
				}
			}
		}]
	}
}

```

**MAPPING**

```
{
	"favoritesearchsearchmodelindex_2": {
		"mappings": {
			"favoritesearchsearchmodel": {
				"properties": {
					"searchQuery": {
						"type": "nested",
						"properties": {
							"categoryProperties": {
								"type": "nested",
								"properties": {
									"intValue": {
										"type": "integer"
									},
									"key": {
										"type": "keyword"
									},
									"values": {
										"type": "nested",
										"properties": {
											"value": {
												"type": "keyword"
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
}
```

---

<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:** [September 25, 2017, 12:17pm UTC](https://discuss.elastic.co/t/keyword-query-does-not-work/101687/2 "2017-09-25T12:17:57Z")

</div>

Not a comment on why this is going wrong but more of a general observation - you may have gone overboard with the use of "nested". If you have an array of objects and they only have one property (like your "values" example) then there is no benefit to mapping as `nested` instead of the simpler`object` type. The use of nested is required to preserve associations between multiple fields in the same object by using additional Lucene documents to hold the related values together.  
This is an old slide deck [1] that outlines the use case for nested - the implementation details differ a little in elasticsearch but the rationale for when you need nested docs is the same and it should give an indication of the overhead they add to disk storage and query complexity.

[1] [https://www.slideshare.net/MarkHarwood/proposal-for-nested-document-support-in-lucene](https://www.slideshare.net/MarkHarwood/proposal-for-nested-document-support-in-lucene)

---

<div class="post-metadata">

**Author:** ![Ibrahim\_ozgon](https://avatars.discourse-cdn.com/v4/letter/i/ac91a4/32.png) [@Ibrahim\_ozgon](https://discuss.elastic.co/u/Ibrahim_ozgon)\
**Post date:** [September 25, 2017, 12:31pm UTC](https://discuss.elastic.co/t/keyword-query-does-not-work/101687/3 "2017-09-25T12:31:57Z")

</div>

Thank you for your response Mark. I didn't know that. I will change them to `object` asap.

---

<div class="post-metadata">

**Author:** ![Ibrahim\_ozgon](https://avatars.discourse-cdn.com/v4/letter/i/ac91a4/32.png) [@Ibrahim\_ozgon](https://discuss.elastic.co/u/Ibrahim_ozgon)\
**Post date:** [September 26, 2017, 11:34am UTC](https://discuss.elastic.co/t/keyword-query-does-not-work/101687/4 "2017-09-26T11:34:39Z")

</div>

Thank you again @Mark_Harwood.  
According to your response, I changed my mapping.  
**New Mapping**

```
{
	"favoritesearchsearchmodelindex_2": {
		"mappings": {
			"favoritesearchsearchmodel": {
				"properties": {
					"searchQuery": {
						"type": "nested",
						"properties": {
							"categoryProperties": {
								"properties": {
									"key": {
										"type": "keyword"
									},
									"numberValue": {
										"type": "double"
									},
									"values": {
										"properties": {
											"value": {
												"type": "keyword"
											}
										}
									}
								}
							},
							"keyList": {
								"properties": {
									"value": {
										"type": "keyword"
									}
								}
							}
						}
					}
				}
			}
		}
	}
}

```

After changing mapping i realized that;  
I'm searching for `searchQuery.categoryProperties.key` is not `color`. I have an array and if one of the key is not `color`its ok for search, but not for me.  
I created a `keyList` array and put all grouped keys of `searchQuery.categoryProperties.key` to `keyList`object.  
Now I'm searching for `keyList`first. It gives me the correct response. This resolved my problem.

**Here is the correct REQUEST**

```
{
	"query": {
		"bool": {
			"must": [{
				"nested": {
					"query": {
						"bool": {
							"filter": [{
								"bool": {
									"should": [{
										"bool": {
											"must_not": [{
												"term": {
													"searchQuery.keyList.value": {
														"value": "color"
													}
												}
											}]
										}
									},
									{
										"bool": {
											"must": [{
												"term": {
													"searchQuery.categoryProperties.key": {
														"value": "color"
													}
												}
											},
											{
												"term": {
													"searchQuery.categoryProperties.values.value": {
														"value": "Mavi"
													}
												}
											}]
										}
									}]
								}
							}]
						}
					},
					"path": "searchQuery"
				}
			}]
		}
	}
}

```

With @Mark_Harwood help, i don't need a lot of nested queries!

---

<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:** [October 24, 2017, 11:35am UTC](https://discuss.elastic.co/t/keyword-query-does-not-work/101687/5 "2017-10-24T11:35:31Z")

</div>

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