GET bliad7/_search
{
"query": {"range": {
"prices.price": {
"gte": 500,
"lte": 600
}
}}
}
mapping
"Price" : {
"type" : "nested",
"properties" : {
"Price" : {
"type" : "double"
}
}
},
GET bliad7/_search
{
"query": {"range": {
"prices.price": {
"gte": 500,
"lte": 600
}
}}
}
mapping
"Price" : {
"type" : "nested",
"properties" : {
"Price" : {
"type" : "double"
}
}
},
You need to use a nested query when searching on nested fields. For example (note that you are using different case in your provided example for mapping and querying):
mapping:
PUT example
{
"settings": {
"index": {
"number_of_shards": 1,
"number_of_replicas": 0
}
},
"mappings": {
"dynamic": "strict",
"properties": {
"Prices": {
"type": "nested",
"properties": {
"Price": {
"type": "double"
}
}
}
}
}
}
Query:
GET example/_search
{
"query": {
"nested": {
"path": "Prices",
"query": {
"range": {
"Prices.Price": {
"gte": 500,
"lte": 600
}
}
}
}
}
}
i don't understand what i am mapping in different way than you
What exactly yo don't understand? the important part from your case is the following:
"properties": {
"Prices": {
"type": "nested",
"properties": {
"Price": {
"type": "double"
}
}
}
"Price" : {
"type" : "nested",
"properties" : {
"Price" : {
"type" : "double"
}
}
},
and i have the same mapping
too.
GET bliad7/_search
{
"query": {
"nested": {
"path": "Price",
"query": {
"range": {
"Price.Price": {
"gte": 0,
"lte": 10
}
}
}
}
}
}
this query returns nothing
but there are products which are in this range...
This works for me:
PUT example
{
"settings": {
"index": {
"number_of_shards": 1,
"number_of_replicas": 0
}
},
"mappings": {
"dynamic": "strict",
"properties": {
"Price": {
"type": "nested",
"properties": {
"Price": {
"type": "double"
}
}
}
}
}
}
POST example/_doc
{
"Price" : [
{"Price" : 10},
{"Price" : 30}
]
}
POST example/_doc
{
"Price" : [
{"Price" : 40},
{"Price" : 50}
]
}
GET example/_search
{
"query": {
"nested": {
"path": "Price",
"query": {
"range": {
"Price.Price": {
"gte": 10,
"lte": 30
}
}
}
}
}
}
and I get a hit as expected:
{
"took" : 371,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "example",
"_type" : "_doc",
"_id" : "JvL212wBubu-7AafcvuH",
"_score" : 1.0,
"_source" : {
"Price" : [
{
"Price" : 10
},
{
"Price" : 30
}
]
}
}
]
}
}
Not sure what is the issue with your environment. Could you try to run this commands in your system and check if it works for you?
{
"error": {
"root_cause": [
{
"type": "query_shard_exception",
"reason": "failed to create query: {\n "nested" : {\n "query" : {\n "range" : {\n "prices.price" : {\n "from" : 500,\n "to" : 600,\n "include_lower" : true,\n "include_upper" : true,\n "boost" : 1.0\n }\n }\n },\n "path" : "prices",\n "ignore_unmapped" : false,\n "score_mode" : "avg",\n "boost" : 1.0\n }\n}",
"index_uuid": "-q0nMbfoRN6Jbhr_5vNkew",
"index": "bliad9"
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "bliad9",
"node": "l6WS2f86S-20-_39aVzTaA",
"reason": {
"type": "query_shard_exception",
"reason": "failed to create query: {\n "nested" : {\n "query" : {\n "range" : {\n "prices.price" : {\n "from" : 500,\n "to" : 600,\n "include_lower" : true,\n "include_upper" : true,\n "boost" : 1.0\n }\n }\n },\n "path" : "prices",\n "ignore_unmapped" : false,\n "score_mode" : "avg",\n "boost" : 1.0\n }\n}",
"index_uuid": "-q0nMbfoRN6Jbhr_5vNkew",
"index": "bliad9",
"caused_by": {
"type": "illegal_state_exception",
"reason": "[nested] nested object under path [prices] is not of nested type"
}
}
}
]
},
"status": 400
}
okey this gave me the error but not sure what it is..
There is something strange I the error. It is not the same query I posted.
are you kidding me:smiley: i did what you said and it is not doing and u are saying that it is not the same query and there is no difference
Not really, I post the statements again I would like you to run, not the name of the attributes (Price
, singular and the first letter uppercase):
PUT example
{
"settings": {
"index": {
"number_of_shards": 1,
"number_of_replicas": 0
}
},
"mappings": {
"dynamic": "strict",
"properties": {
"Price": {
"type": "nested",
"properties": {
"Price": {
"type": "double"
}
}
}
}
}
}
POST example/_doc
{
"Price" : [
{"Price" : 10},
{"Price" : 30}
]
}
POST example/_doc
{
"Price" : [
{"Price" : 40},
{"Price" : 50}
]
}
GET example/_search
{
"query": {
"nested": {
"path": "Price",
"query": {
"range": {
"Price.Price": {
"gte": 10,
"lte": 30
}
}
}
}
}
}
what i found is really interesting..
"Price" : {
"type" : "nested",
"properties" : {
"Price" : {
"type" : "double"
}
}
},
"prices" : {
"properties" : {
"price" : {
"type" : "float"
}
}
},
it did 2 mapping while migration... one for float one for double
The issue here is that your original mapping is not defined as strict. your original mapping was defined as:
"Price": {
"type": "nested",
"properties": {
"Price": {
"type": "double"
}
}
}
But then you try to insert the following data:
POST example/_doc
{
"prices" : [
{"price" : 10},
{"price" : 30}
]
}
Elasticsearch did not find any matching definition on your mapping and therefore created a new field in the mapping:
"prices" : {
"properties" : {
"price" : {
"type" : "float"
}
}
i am going to do migration with dynamic strict... will it work?
At least it will tell you when the migration is trying to ingest data with an unexpected format. Generally speaking if your application is working with a fixed mapping I would always recommend using strict mapping.
okey i will try it.. thank you for help...
You may have summarized your mapping to show just the price field, but if you haven't note there's no advantage to using the nested
type for single-field objects - only disadvantages.
I REMOVED NESTED AND I HAVE QUERY LIKE THIS
{
"query":
{
"range": {
"prices.price": {
"gte":500,
"lte": 1000
}
}
}
}
BUT IT GETS WRONG DATA
"prices" : [
{
"price" : 13012.0
},
{
"price" : 70867.0
},
{
"price" : 4984.0
},
{
"price" : 44111.0
},
{
"price" : 67594.0
},
{
"price" : 26644.0
},
{
"price" : 42066.0
},
{
"price" : 699.0
},
{
"price" : 72687.0
}
],
© 2020. All Rights Reserved - Elasticsearch
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant logo are trademarks of the Apache Software Foundation in the United States and/or other countries.