# How do I get the highest matching set of data in a multi-value field?

**URL:** <https://discuss.elastic.co/t/how-do-i-get-the-highest-matching-set-of-data-in-a-multi-value-field/252141>\
**Category:** Elasticsearch\
**Created:** [October 15, 2020, 7:14am UTC](https://discuss.elastic.co/t/how-do-i-get-the-highest-matching-set-of-data-in-a-multi-value-field/252141 "2020-10-15T07:14:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![yanglingqiang](https://avatars.discourse-cdn.com/v4/letter/y/8e7dd6/32.png) [@yanglingqiang](https://discuss.elastic.co/u/yanglingqiang)\
**Post date:** [October 15, 2020, 7:14am UTC](https://discuss.elastic.co/t/how-do-i-get-the-highest-matching-set-of-data-in-a-multi-value-field/252141/1 "2020-10-15T07:14:07Z")

</div>

my index mapping

```
"mapping":{
	"test":{
	    "descript": {
			"properties": {
				"prop": {
					"type": "long"
				},
				"value": {
					"analyzer": "english",
					"type": "text"
				}
			}
		},
		"id":{
			"type":"long"
		}
	}
}

```

my index data

```
{
	"id": 1,
	"descript": [
		{
			"prop": 15,
			"value": "led light"
		},
		{
			"prop": 10,
			"value": "led lamp"
		}
	]
}
{
	"id": 2,
	"descript": [
		{
			"prop": 5,
			"value": "lamp"
		},
		{
			"prop": 10,
			"value": "light"
		}
	]
}

```

my query

```
GET test/_search
{
  "query": {
    "match": {
      "descript.value": "led"
    }
  }
}

```

This query will match id 1 and id 2. But I want the descript field from \_source to look something like this

```
"_source" : {
	"id": 1,
	"descript": [
		{
			"prop": 15,
			"value": "led light"
		}
	]
}

```

I want to take only the highest match or the highest value of `prop` in the matched multi-value field `descript`

I'm hoping for some help from the gurus.

thinks

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 15, 2020, 9:56am UTC](https://discuss.elastic.co/t/how-do-i-get-the-highest-matching-set-of-data-in-a-multi-value-field/252141/2 "2020-10-15T09:56:04Z")

</div>

I can't think of an easy way for doing this with the current document structure you are using.  
I'd probably solve that problem on client side by parsing the resultset and only taking into account the highest value for each table.

In which case I'd use the following approach to be sure I'm only getting the right "sub-hits":

```auto
DELETE test 
PUT test
{
  "mappings": {
    "properties": {
      "descript": {
        "type": "nested",
        "properties": {
          "prop": {
            "type": "long"
          },
          "value": {
            "type": "text"
          }
        }
      }
    }
  }
}
PUT /test/_doc/1
{
  "descript": [
    {
      "prop": 15,
      "value": "led light"
    },
    {
      "prop": 10,
      "value": "led lamp"
    },
    {
      "prop": 5,
      "value": "lamp"
    }
  ]
}
PUT /test/_doc/2
{
  "descript": [
    {
      "prop": 5,
      "value": "lamp"
    },
    {
      "prop": 10,
      "value": "light"
    }
  ]
}
GET test/_search
{
  "_source": false,
  "query": {
    "nested": {
      "path": "descript",
      "query": {
        "match": {
          "descript.value": "led"
        }
      },
      "inner_hits": { }
    }
  }
}

```

That gives:

```auto
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.5176139,
    "hits" : [
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.5176139,
        "inner_hits" : {
          "descript" : {
            "hits" : {
              "total" : {
                "value" : 2,
                "relation" : "eq"
              },
              "max_score" : 0.5176139,
              "hits" : [
                {
                  "_index" : "test",
                  "_type" : "_doc",
                  "_id" : "1",
                  "_nested" : {
                    "field" : "descript",
                    "offset" : 0
                  },
                  "_score" : 0.5176139,
                  "_source" : {
                    "prop" : 15,
                    "value" : "led light"
                  }
                },
                {
                  "_index" : "test",
                  "_type" : "_doc",
                  "_id" : "1",
                  "_nested" : {
                    "field" : "descript",
                    "offset" : 1
                  },
                  "_score" : 0.5176139,
                  "_source" : {
                    "prop" : 10,
                    "value" : "led lamp"
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}

```

---

<div class="post-metadata">

**Author:** ![yanglingqiang](https://avatars.discourse-cdn.com/v4/letter/y/8e7dd6/32.png) [@yanglingqiang](https://discuss.elastic.co/u/yanglingqiang)\
**Post date:** [October 16, 2020, 8:47am UTC](https://discuss.elastic.co/t/how-do-i-get-the-highest-matching-set-of-data-in-a-multi-value-field/252141/4 "2020-10-16T08:47:27Z")

</div>

Thank you, that's exactly what I wanted! Thank you very much.

---

<div class="post-metadata">

**Author:** ![yanglingqiang](https://avatars.discourse-cdn.com/v4/letter/y/8e7dd6/32.png) [@yanglingqiang](https://discuss.elastic.co/u/yanglingqiang)\
**Post date:** [October 30, 2020, 3:10am UTC](https://discuss.elastic.co/t/how-do-i-get-the-highest-matching-set-of-data-in-a-multi-value-field/252141/5 "2020-10-30T03:10:38Z")

</div>

Why can't the prop fields be sorted from smallest to largest when they are all decimal points?

```auto
DELETE test 

PUT test
{
  "mappings": {
    "properties": {
      "descript": {
        "type": "nested",
        "properties": {
          "prop": {
            "type": "long"
          },
          "value": {
            "ignore_above": 256,
            "type": "keyword"
          },
          "last_trade_date": {
            "type": "date"
          },
          "count": {
            "type": "long"
          }
        }
      },
      "name": {
        "analyzer": "standard",
        "type": "text"
      }
    }
  }
}

PUT /test/_doc/1
{
  "descript": [
    {
      "prop": 4.2,
      "value": "led",
      "last_trade_date": "2020-07-31T00:00:00",
      "count": 29
    }
  ],
  "name": "led a"
}
PUT /test/_doc/2
{
  "descript": [
    {
      "prop": 4.5,
      "value": "led",
      "last_trade_date": "2020-07-31T00:00:00",
      "count": 29
    }
  ],
  "name": "led b"
}
PUT /test/_doc/3
{
  "descript": [
    {
      "prop": 2,
      "value": "led",
      "last_trade_date": "2020-07-31T00:00:00",
      "count": 29
    }
  ],
  "name": "led c"
}

GET test/_search
{
  "sort": [
    {
      "descript.prop": {
        "order": "desc",
        "mode": "max",
        "nested": {
          "path": "descript",
          "filter": {
            "term": {
              "descript.value": "led"
            }
          }
        }
      }
    }
  ],
  "from": 0,
  "size": 20,
  "track_total_hits": true,
  "_source": [
    "name"
  ],
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "descript",
            "query": {
              "bool": {
                "filter": {
                  "term": {
                    "descript.value": "led"
                  }
                }
              }
            },
            "inner_hits": {
              "size": 1
            }
          }
        }
      ]
    }
  }
}

```

Why does Document 1 rank higher than Document 2?

---

<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:** [November 27, 2020, 3:10am UTC](https://discuss.elastic.co/t/how-do-i-get-the-highest-matching-set-of-data-in-a-multi-value-field/252141/6 "2020-11-27T03:10:42Z")

</div>

Closed on November 27, 2020, 3:10am UTC

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