elasticsearch 颜色搜索

第一步:创建颜色搜索结构

{ "color" : {
    "type": "nested",
    "properties" : {
      "h" : { "type" : "integer" },
      "s" : { "type" : "integer" },
      "v" : { "type" : "integer" } } } }

第二步:组建搜索语句

{ "filter": {
    "and": [
      { "nested": {
          "path": "color",
          "filter": {
            "and": [
              { "range": {
                  "ratio": { //取颜色比例区间
                    "gte": 80,
                    "lte": 100 } } },
              { "range": {
                  "h": { //H 值区间
                    "gte": 180,
                    "lte": 200 } } },
              { "range": {
                  "s": { //S 值区间
                    "gte": 95,
                    "lte": 100 } } },
              { "range": {
                  "v": { //V 值区间
                    "gte": 45,
                    "lte": 55 } } } ] } } } ] } }

第三步:自定义评分使颜色最近的现实在前面

{ "function_score": {
    "boost_mode": "replace",
    "query": {
      "nested": {
        "path": "color",
        "query": {
          "function_score": {
            "score_mode": "multiply",
            "functions": [
              { "exp": {
                  "h": {
                    "origin": 190,
                    "offset": 2,
                    "scale": 4 } } },
              { "exp": {
                  "s": {
                    "origin": 100,
                    "offset": 4,
                    "scale": 8 } } },
              { "exp": {
                  "v": {
                    "origin": 50,
                    "offset": 4,
                    "scale": 8 } } },
              { "linear": {
                  "ratio": {
                    "origin": 100,
                    "offset": 5,
                    "scale": 10 } } } ] } },
        "score_mode": "sum" } },
    "functions": [
        { "script_score": { "script": "_score" } } ] } }

 

//H 值区间
posted @ 2020-10-01 23:38  likecs  阅读(461)  评论(0编辑  收藏  举报