如何在elasticsearch中执行高度范围搜索

knpiaxh1  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(1)|浏览(258)

如何在elasticsearch中表示高度和高度范围,以便更容易进行范围搜索

Height.java: int feet, int inches;
HeightRange.java: Height from, Height to

我想搜索在一定范围内的用户(比如5英尺-6英尺)

wgeznvg7

wgeznvg71#

如果我能很好地理解您的问题,您可以使用如下范围查询。我做了一个本地测试,如下所示,我摄取了以下数据:

"hits" : [
  {
    "_index" : "height-index-array",
    "_type" : "_doc",
    "_id" : "OfCHdXUB1QlsTOLdRJgd",
    "_score" : 1.0,
    "_source" : {
      "user" : "user1",
      "height" : {
        "feet" : 5,
        "inch" : 8
      }
    }
  },
  {
    "_index" : "height-index-array",
    "_type" : "_doc",
    "_id" : "CfCJdXUB1QlsTOLdEZxS",
    "_score" : 1.0,
    "_source" : {
      "user" : "user2",
      "height" : {
        "feet" : 7,
        "inch" : 9
      }
    }
  },
  {
    "_index" : "height-index-array",
    "_type" : "_doc",
    "_id" : "CvCJdXUB1QlsTOLdEpx5",
    "_score" : 1.0,
    "_source" : {
      "user" : "user3",
      "height" : {
        "feet" : 5,
        "inch" : 6
      }
    }
  },
  {
    "_index" : "height-index-array",
    "_type" : "_doc",
    "_id" : "C_CJdXUB1QlsTOLdE5yk",
    "_score" : 1.0,
    "_source" : {
      "user" : "user4",
      "height" : {
        "feet" : 5,
        "inch" : 8
      }
    }
  },
  {
    "_index" : "height-index-array",
    "_type" : "_doc",
    "_id" : "T_CJdXUB1QlsTOLdFZwx",
    "_score" : 1.0,
    "_source" : {
      "user" : "user5",
      "height" : {
        "feet" : 2,
        "inch" : 3
      }
    }
  }
]

我用来查询 heightfeet 在5和6之间:

"query": {
    "range": {
    "height.feet": {
        "gte": 5,
        "lte": 6
    }
    }
}

这个 gte 相当于 greater than or equal to 以及 lte 相当于 less than or equal to .
结果是:

"hits" : {
    "total" : {
    "value" : 3,
    "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
    {
        "_index" : "height-index-array",
        "_type" : "_doc",
        "_id" : "OfCHdXUB1QlsTOLdRJgd",
        "_score" : 1.0,
        "_source" : {
        "user" : "user1",
        "height" : {
            "feet" : 5,
            "inch" : 8
        }
        }
    },
    {
        "_index" : "height-index-array",
        "_type" : "_doc",
        "_id" : "CvCJdXUB1QlsTOLdEpx5",
        "_score" : 1.0,
        "_source" : {
        "user" : "user3",
        "height" : {
            "feet" : 5,
            "inch" : 6
        }
        }
    },
    {
        "_index" : "height-index-array",
        "_type" : "_doc",
        "_id" : "C_CJdXUB1QlsTOLdE5yk",
        "_score" : 1.0,
        "_source" : {
        "user" : "user4",
        "height" : {
            "feet" : 5,
            "inch" : 8
        }
        }
    }
    ]
}

如果您有任何问题,请告诉我,我很乐意为您提供帮助:)
根据您的要求,如果您需要合并这两个指标,您可以使用bool查询:

"query": {
    "bool": {
    "must": [
        {
        "range": {
            "height.feet": {
            "gte": 5,
            "lte": 6
            }
        }
        },{
        "range": {
            "height.inch": {
            "gte": 6,
            "lte": 8
            }
        }
        }
    ]
    }
}

答案是:

"hits" : [
  {
    "_index" : "height-index-array",
    "_type" : "_doc",
    "_id" : "OfCHdXUB1QlsTOLdRJgd",
    "_score" : 2.0,
    "_source" : {
      "user" : "user1",
      "height" : {
        "feet" : 5,
        "inch" : 8
      }
    }
  },
  {
    "_index" : "height-index-array",
    "_type" : "_doc",
    "_id" : "CvCJdXUB1QlsTOLdEpx5",
    "_score" : 2.0,
    "_source" : {
      "user" : "user3",
      "height" : {
        "feet" : 5,
        "inch" : 6
      }
    }
  },
  {
    "_index" : "height-index-array",
    "_type" : "_doc",
    "_id" : "C_CJdXUB1QlsTOLdE5yk",
    "_score" : 2.0,
    "_source" : {
      "user" : "user4",
      "height" : {
        "feet" : 5,
        "inch" : 8
      }
    }
  }
]

链接:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.htmlhttps://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html

相关问题