Recently a es sorting problem encountered in the development, said about the project requirements, an image analytic results can have a lot of color, such as color such as white, pink, red, and their proportion, we put all the pictures in the gallery are parsed, each image can save up to five the mass-tone attune of color and their proportion in the es, Users can select the color image they want by filtering the color mass and descending by proportion. Here, ES needs a field to store an array of mass information. The data structure of this field is roughly as follows:

Color information: [{” color “:” white “, “share” : “0.123456”} {” color “:” black “, “share” : “0.223456”} {” color “:” blue “, “share” : “0.323456”}]

Mapping index structure of ES

The most important thing here is to define the field type to be “type”: “nested”

PUT index_Name {

“mappings”: {

“doc”: {

“properties”: {

“huesInfo”: {

“type”: “nested”,

“include_in_parent”: true,

“properties”: {

“hues”: {

“type”: “keyword”

},

“ratio”: {

“type”: “double”

}

}

}

}

}

}

}

Insert data

Test by simulating several pieces of data

POST index_Name/doc/1 {“huesInfo”:[{“hues”:”WhiteColors”, “ratio”:0.123456},{“hues”:”RedColors”, “Thewire”, 0.323456}, {” hues “:” BlueColors “, “thewire” : 0.423456}]}

3. Es query

Select the specified hue and order the selected hue proportion from highest to lowest score

For example, if the main color of the es is white, here is an example of the ES syntax:

GET index_Name/ _search {

“query”: {

“bool”: {

“must”: [{

“term”: {

“huesInfo.hues”: {

“value”: “WhiteColors”

}

}

}]

}

},

“sort”: {

“huesInfo.ratio”: {

“order”: “desc”,

“mode”: “max”,

“nested_path”: “huesInfo”,

“nested_filter”: {

“match”: {

“huesInfo.hues”: {

“query”: “WhiteColors”

}

}

}

}

}

}

In the actual development, SpringBoot integrated ES tool class can use the following NestedQueryBuilder API to achieve