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

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

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":

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:

{
  "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"
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}

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

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

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?

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