es模糊查询与精准查询混用、距离排序、返回距离

es模糊查询与精准查询混用

QueryBuilder qb1 = QueryBuilders.matchPhraseQuery("XXXXXX1", "查询短语");
QueryBuilder qb2 = QueryBuilders.termQuery("XXXXXX2",1);
QueryBuilder qb3 = QueryBuilders.boolQuery().must(qb1).must(qb2);

对应的JSON语句

{
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase": {
            "XXXXXX1": {
              "query": "小店",
              "slop": 0,
              "boost": 1
            }
          }
        },
        {
          "term": {
            "XXXXXX2": {
              "value": 1,
              "boost": 1
            }
          }
        }
      ],
      "disable_coord": false,
      "adjust_pure_negative": true,
      "boost": 1
    }
  },
  "from": 0,
  "size": 5000,
  "sort": [],
  "aggs": {}
}

es根据指定的坐标距离排序

List sortBuilders = Lists.newArrayList();
//按照距离(Latitude, Longitudel)坐标的距离生序排列,返回距离以米为单位
sortBuilders.add(SortBuilders.geoDistanceSort(s.getValue(), Latitude, Longitudel)
                            .unit(DistanceUnit.METERS).geoDistance(GeoDistance.ARC).order(SortOrder.ASC));
//返回值中获取距离
Object[] sortRes = hit.getSortValues();
String distance = (Double) sortRes[i];//如果sortBuilders中有多个排序字段,i为距离排序在sortBuilders中的位置,否则为0

你可能感兴趣的:(es模糊查询与精准查询混用、距离排序、返回距离)