How to find count of inner hits?


(oguz) #1

Hi all,

is it possible to get the count of all inner hits in a paginated query? If not, is there any alternative solution you can direct me to? I want to get the count of all items (segment.item) in the inner hits on the below sample mapping:

"mappings": {
	"booking": {
		"properties": {
			"booking_number": {
				"type": "text",
				"analyzer": "ngram_analyzer",
				"search_analyzer": "my_search_analyzer",
				"fields": {
					"raw": {
						"type": "keyword"
					}
				}
			},
			"segment": {
				"type": "nested",
				"properties": {
					"start_date": {
						"type": "date"
					},
					"end_date": {
						"type": "date"
					},
					"cancel_timestamp": {
						"type": "date"
					},
					"service": {
						"type": "keyword"
					},
					"departure_station": {
						"type": "keyword"
					},
					"arrival_station": {
						"type": "keyword"
					},
					"multi_segment": {
						"type": "boolean"
					},
					"item": {
						"type": "nested",
						"properties": {
							"item_id": {
								"type": "integer"
							},
							"inventory_class": {
								"type": "keyword"
							},
							"carriage_number": {
								"type": "keyword"
							},
							"product_code": {
								"type": "keyword"
							},
							"product_type": {
								"type": "keyword"
							},
							"ticket_number": {
								"type": "keyword"
							},
							"cancel_timestamp": {
								"type": "date"
							},
							"expiry_timestamp": {
								"type": "date"
							},
							"validation": {
								"type": "nested",
								"properties": {
									"type": {
										"type": "keyword"
									}
								}
							}
						}
					}
				}
			}
		}
	}
}

my query:

curl -XGET 'localhost: 9200/bookings/booking/_search?pretty' -d '{
	"size": 5,
	"timeout": "60s",
	"query": {
		"bool": {
			"filter": [{
				"nested": {
					"query": {
						"bool": {
							"filter": [{
								"nested": {
									"query": {
										"bool": {
											"must": [{
												"exists": {
													"field": "segment.item.ticket_number",
													"boost": 1.0
												}
											},
											{
												"terms": {
													"segment.item.product_type": ["ST",
													"SO",
													"TO"],
													"boost": 1.0
												}
											}],
											"must_not": [{
												"exists": {
													"field": "segment.item.expiry_timestamp",
													"boost": 1.0
												}
											}],
											"adjust_pure_negative": true,
											"boost": 1.0
										}
									},
									"path": "segment.item",
									"ignore_unmapped": false,
									"score_mode": "sum",
									"boost": 1.0,
									"inner_hits": {
										"name": "segment.item",
										"ignore_unmapped": false,
										"from": 0,
										"size": 20,
										"version": false,
										"explain": false,
										"track_scores": false
									}
								}
							},
							{
								"range": {
									"segment.start_date": {
										"from": null,
										"to": "2017-10-19",
										"include_lower": true,
										"include_upper": true,
										"boost": 1.0
									}
								}
							},
							{
								"range": {
									"segment.end_date": {
										"from": "2017-10-19",
										"to": null,
										"include_lower": true,
										"include_upper": true,
										"boost": 1.0
									}
								}
							}],
							"adjust_pure_negative": true,
							"boost": 1.0
						}
					},
					"path": "segment",
					"ignore_unmapped": false,
					"score_mode": "sum",
					"boost": 1.0,
					"inner_hits": {
						"name": "segment",
						"ignore_unmapped": false,
						"from": 0,
						"size": 10,
						"version": false,
						"explain": false,
						"track_scores": false,
						"_source": false
					}
				}
			}],
			"adjust_pure_negative": true,
			"boost": 1.0
		}
	},
	"_source": false
}

(oguz) #2

And part of sample output:

{
  "took" : 35,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 43,
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "bookings",
        "_type" : "booking",
        "_id" : "AWBqF7CaV4SPSy5IHUz9",
        "_score" : 0.0,
        "inner_hits" : {
          "segment" : {
            "hits" : {
              "total" : 1,
              "max_score" : 0.0,
              "hits" : [
                {
                  "_nested" : {
                    "field" : "segment",
                    "offset" : 0
                  },
                  "_score" : 0.0,
                  "inner_hits" : {
                    "segment.item" : {
                      "hits" : {
                        "total" : 1,
                        "max_score" : 1.1799169,
                        "hits" : [
                          {
                            "_nested" : {
                              "field" : "segment",
                              "offset" : 0,
                              "_nested" : {
                                "field" : "item",
                                "offset" : 0
                              }
                            },
                            "_score" : 1.1799169,
                            "_source" : {
                              "cancel_timestamp" : "2017-10-18T10:31:25.086",
                              "product_type" : "ST",
                              "item_id" : 1565994,
                              "carriage_number" : "3",
                              "product_code" : "14091521ST",
                              "ticket_number" : "345727",
                              "inventory_class" : "O5"
                            }
                          }
                        ]
                      }
                    }
                  }
                }
              ]
            }
          }
        }
      },
      {
        "_index" : "bookings",
        "_type" : "booking",
        "_id" : "AWBqF7CaV4SPSy5IHU0W",
        "_score" : 0.0,
        "inner_hits" : {
          "segment" : {
            "hits" : {
              "total" : 1,
              "max_score" : 0.0,
              "hits" : [
                {
                  "_nested" : {
                    "field" : "segment",
                    "offset" : 0
                  },
                  "_score" : 0.0,
                  "inner_hits" : {
                    "segment.item" : {
                      "hits" : {
                        "total" : 1,
                        "max_score" : 1.1799169,
                        "hits" : [
                          {
                            "_nested" : {
                              "field" : "segment",
                              "offset" : 0,
                              "_nested" : {
                                "field" : "item",
                                "offset" : 0
                              }
                            },
                            "_score" : 1.1799169,
                            "_source" : {
                              "cancel_timestamp" : "2017-10-18T13:38:52.066",
                              "product_type" : "ST",
                              "item_id" : 1566020,
                              "carriage_number" : "3",
                              "product_code" : "14091521ST",
                              "ticket_number" : "205184",
                              "inventory_class" : "O5"
                            }
                          }
                        ]
                      }
                    }
                  }
                }
              ]
            }
          }
        }
      },
...

(system) #3

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