RestClient操作文档和DSL查询语法

一、 文档操作

1、新增文档

本案例中,hotel为索引库名,61083为文档id
 @Test
    void testAddDocument() throws IOException {
        // 1.根据id查询酒店数据
        Hotel hotel = hotelService.getById(61083L);
        // 2.转换为文档类型
        HotelDoc hotelDoc = new HotelDoc(hotel);
        // 3.将HotelDoc转json
        String json = JSON.toJSONString(hotelDoc);

        // 1.准备Request对象
        IndexRequest request = new IndexRequest("hotel").id(hotelDoc.getId().toString());
        // 2.准备Json文档
        request.source(json, XContentType.JSON);
        // 3.发送请求
        client.index(request, RequestOptions.DEFAULT);
    }

2、查询文档

@Test
    void testGetDocumentById() throws IOException {
        // 1.准备Request
        GetRequest request = new GetRequest("hotel", "61083");
        // 2.发送请求,得到响应
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        // 3.解析响应结果
        String json = response.getSourceAsString();

        HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
        System.out.println(hotelDoc);
    }

3、更新文档

@Test
    void testUpdateDocument() throws IOException {
        // 1.准备Request
        UpdateRequest request = new UpdateRequest("hotel", "61083");
        // 2.准备请求参数
        request.doc(
                "price", "952",
                "starName", "四钻"
        );
        // 3.发送请求
        client.update(request, RequestOptions.DEFAULT);
    }

4、删除文档

 @Test
    void testDeleteDocument() throws IOException {
        // 1.准备Request
        DeleteRequest request = new DeleteRequest("hotel", "61083");
        // 2.发送请求
        client.delete(request, RequestOptions.DEFAULT);
    }

5、批量导入文档

 @Test
    void testBulkRequest() throws IOException {
        // 批量查询酒店数据
        List<Hotel> hotels = hotelService.list();

        // 1.创建Request
        BulkRequest request = new BulkRequest();
        // 2.准备参数,添加多个新增的Request
        for (Hotel hotel : hotels) {
            // 2.1.转换为文档类型HotelDoc
            HotelDoc hotelDoc = new HotelDoc(hotel);
            // 2.2.创建新增文档的Request对象
            request.add(new IndexRequest("hotel")
                    .id(hotelDoc.getId().toString())
                    .source(JSON.toJSONString(hotelDoc), XContentType.JSON));
        }
        // 3.发送请求
        client.bulk(request, RequestOptions.DEFAULT);
    }

二、DSL查询语法

1、 全文检索查询

#全文检索查询
#查询所有
GET /hotel/_search
{
  "query": {
    "match_all": {}
  }
}

#match查询(推荐使用,all里包含fields所有信息,效率较fields高)
GET /hotel/_search
{
  "query": {
    "match": {
      "all": "外滩"
    }
  }
}

#multi_match查询
GET /hotel/_search
{
  "query": {
    "multi_match": {
      "query": "外滩",
      "fields": ["brand","name","business"]
    } 
  }
}

2、 精确查询

#精确查询
#term查询(具体值)
GET /hotel/_search
{
  "query": {
    "term": {
      "city": {
        "value": "上海"
      }
    }
  }
}
#range查询(范围)
GET /hotel/_search
{
  "query": {
    "range": {
      "price": {
        "gte": 100,
        "lte": 300
        
      }
    }
  }
}

3、 地理查询

RestClient操作文档和DSL查询语法_第1张图片
注:一般不用上方地理查询,用下面方法!
RestClient操作文档和DSL查询语法_第2张图片

#distance查询,附近15km范围内
GET /hotel/_search
{
  "query": {
    "geo_distance":{
      "distance":"15km",
      "location":"31.21,121.5"
    }
  }
}

4、 复合查询

算法了解
RestClient操作文档和DSL查询语法_第3张图片
RestClient操作文档和DSL查询语法_第4张图片

4.1、Function Score Query(修改相关性算分)

RestClient操作文档和DSL查询语法_第5张图片
RestClient操作文档和DSL查询语法_第6张图片

# function score 查询,
GET /hotel/_search
{
  "query":{
    "function_score": {
      "query": {
        "match": {
          "all": "外滩"
        }
      },
      "functions": [
        {
          "filter": {
            "term": {
              "brand": "如家"
            }
          },
          "weight": 10
        }
      ],
      "boost_mode": "sum"
    }
  }
}

RestClient操作文档和DSL查询语法_第7张图片

4.2、Boolean Query

RestClient操作文档和DSL查询语法_第8张图片
RestClient操作文档和DSL查询语法_第9张图片

# bool查询,一般关键字搜索放在must里,其他放在must_not或filter
GET /hotel/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "如家"
          }
        }
      ],
      "must_not": [
        {
          "range": {
            "price": {
              "gt": 400
              
            }
          }
        }
      ],
      "filter": [
        {
          "geo_distance": {
            "distance": "10km",
            
            "location": {
              "lat": 31.21,
              "lon": 121.5
            }
          }
        }
      ]
    }
  }
}

你可能感兴趣的:(张仲景大药房公司工作总结,elasticsearch,RestClient)