Get stored nested field


(lukasSirhal) #1

Hello,

because i don't find any helpfull answear. I have to ask you how can i get store nested field from search.
I have this mapping

{
"spm_1.1": {
	"mappings": {
	  "order": {
		"properties": {
		  "dateCreated": {
			"type": "date",
			"store": true,
			"format": "dd.MM.yyyy HH:mm:ss"
		  },
		  "note": {
			"type": "text",
			"analyzer": "lowerCaseCzechAnalyzer"
		  },
		  "offerId": {
			"type": "keyword",
			"store": true
		  },
		  "priority": {
			"type": "short",
			"store": true
		  },
		  "serviceItem": {
			"type": "nested",
			"properties": {
			  "active": {
				"type": "keyword",
				"store": true
			  },
			  "akce": {
				"type": "keyword",
				"store": true
			  },
			  "basicServiceAttribut": {
				"type": "keyword",
				"store": true
			  },
			  "crossId": {
				"type": "keyword",
				"store": true
			  }
			}
		  }
			"type": "text",
			"fields": {
			  "keyword": {
				"type": "keyword",
				"ignore_above": 256
			  }
			}
		  }
		}
	}
}
 }

and when i do this search :

{
  "query" : {
    "bool" : {
      "must" : {
        "term" : {
          "primaryKey" : 38823
        }
      }
    }
  },
  "stored_fields" : [
    "priority",
    "serviceItem.tariffSpaceAttribut"
  ]
}

I gen only priority store field :

"hits": {
"total": 1,
"max_score": 1,
"hits": [
  {
    "_index": "spm_1.1",
    "_type": "order",
    "_id": "38823",
    "_score": 1,
    "fields": {
      "priority": [
        3
      ]
    }
  }
]
}

Can you give me any advice?


(lukasSirhal) #2

Hello I'm sorry, i used wrong nested stored fields tariffSpaceAttribut, so someone should be confused. But if i used serviceItem.akce or serviceItem.active i had same result. No nested fields in returned field array


(Alexander Reelsen) #3

Hey,

please provide valid JSON examples so others can reproduce your examples.

Why do you want to access stored fields. Would source filtering already be helpful in your case?

--Alex


(lukasSirhal) #4

Because i don't need whole document but only few fields for display.
I repaired mappings :

{
"spm_1.1": {
	"mappings": {
		"order": {
			"properties": {
				"dateCreated": {
					"type": "date",
					"store": true,
					"format": "dd.MM.yyyy HH:mm:ss"
				},
				"note": {
					"type": "text",
					"analyzer": "lowerCaseCzechAnalyzer"
				},
				"offerId": {
					"type": "keyword",
					"store": true
				},
				"priority": {
					"type": "short",
					"store": true
				},
				"serviceItem": {
					"type": "nested",
					"properties": {
						"active": {
							"type": "keyword",
							"store": true
						},
						"akce": {
							"type": "keyword",
							"store": true
						},
						"basicServiceAttribut": {
							"type": "keyword",
							"store": true
						},
						"crossId": {
							"type": "keyword",
							"store": true
						}
					}
				}
			}
		}
	}
}
} 

I need fields in response because we have prepaired our ES java libraly for search and display since 1.7.4

Thanks for help


(Alexander Reelsen) #5

Hey,

again, please provide a possibility to fully reproduce your example. Now your analyzer lowerCaseCzechAnalyzer is missing. This is costing others a lot of time, even if they want to help.

Take this example:

PUT index1

PUT index1/order/_mapping
{
  "order": {
    "properties": {
      "dateCreated": {
        "type": "date",
        "store": true,
        "format": "dd.MM.yyyy HH:mm:ss"
      },
      "note": {
        "type": "text"
      },
      "serviceItem": {
        "type": "nested",
        "properties": {
          "active": {
            "type": "keyword",
            "store": true
          },
          "crossId": {
            "type": "keyword",
            "store": true
          }
        }
      }
    }
  }
}

PUT index1/order/1
{
  "note" : "my note",
  "serviceItem" : [
    { "crossId": "a" ,"active": true },
    { "crossId": "b" ,"active": false },
    { "crossId": "c" ,"active": true },
    { "crossId": "d" ,"active": false },
    { "crossId": "e" ,"active": true }
  ]
}

GET index1/order/_search
{
  "_source": "serviceItem.active"
}

the output of the search is:

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "index1",
        "_type": "order",
        "_id": "1",
        "_score": 1,
        "_source": {
          "serviceItem": [
            {
              "active": true
            },
            {
              "active": false
            },
            {
              "active": true
            },
            {
              "active": false
            },
            {
              "active": true
            }
          ]
        }
      }
    ]
  }
}

and you can see that the source is filtered for the active field.

--Alex


(lukasSirhal) #6

Sorry i forgot on analyzer this is just part of my whole index.

I tried retrieve fields in _source and it's work fine but why doesn't work stored_field in nested object?
Maybe I don't fully underestund stored_fields idea :frowning:

GET index1/order/_search
{
  "_source": "serviceItem.active",
  "stored_fields" : ["serviceItem.active"]
} 

and output should look like this:

  {
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "hits": {
    "total": 9819,
    "max_score": 1,
    "hits": [
      {
        "_index": "spm_1.1",
        "_type": "order",
        "_id": "39066",
        "_score": 1,
        "_source": {
          "serviceItem": [
            {
              "active": "PROJECT"
            }
          ]
        },
        "fields" : {
           "serviceItem.active" : [
              "PROJECT"
             ]
        }
      }, .....

(Alexander Reelsen) #7

Hey,

just to quote from the docs:

The stored_fields parameter is about fields that are explicitly marked as stored in the mapping, which is off by default and generally not recommended. Use source filtering instead to select subsets of the original source document to be returned.

The missing detail here is the case, that nested documents are not part of the lucene document, but are mini documents appended at the end of that document when being stored. Retrieving stored fields from nested documents does not work. IIRC there is an option include_in_parent for nested documents, which also indexed those documents in the parent which would allow this - but that would require some testing.

--Alex


(lukasSirhal) #8

Ok so using _source is recomended instead stored_field

i don't want to use **include_in_parent ** because it's deprecated in 5.1.1 version.

There is a lot of changes in new version against 1.7.4, I must study it carefully, Alex thanks a lot you are realy help me.


(system) #9

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