30 Eylül 2022 Cuma

SpringData ElasticSearch ElasticsearchOperations Sınıfı

Giriş
Şu satırı dahil ederiz
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.SearchHits; import org.springframework.data.elasticsearch.core.SearchPage; import org.springframework.data.elasticsearch.core.query.Criteria; import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
search metodu
Örnek
Şöyle yaparız. Burada "org.springframework.data.elasticsearch.core.SearchHits" dönülüyor
@Service
@RequiredArgsConstructor
@Slf4j
public class CityService {

@NonNull
final ElasticsearchOperations esTemplate;

public SearchHits<City> searchHits(String name, String country, String subcountry, 
                                     Pageable pageable) {
CriteriaQuery query = buildSearchQuery(name, country, subcountry);
query.setPageable(pageable);
return esTemplate.search(query, City.class);
}

private CriteriaQuery buildSearchQuery(String name, String country, String subcountry) {
var criteria = new Criteria();
if (nonNull(name)) {
criteria.and(new Criteria("name").contains(name));
}
if (nonNull(country)) {
criteria.and(new Criteria("country").expression(country));
}
if (nonNull(subcountry)) {
criteria.and(new Criteria("subcountry").is(subcountry));
}
return new CriteriaQuery(criteria);
}
}
Kullanmak için şöyle yaparız
@RequestMapping(value = CityController.ROOT_PATH, produces = APPLICATION_JSON_VALUE)
@RequiredArgsConstructor
public class CityController {

  static final String ROOT_PATH = "/api/cities";

  @NonNull
  final CityService service;

  @GetMapping("/search_hits")
  public SearchHits<City> searchHits(@PathParam("name") String name, 
    @PathParam("country") String country,
    @PathParam("subcountry") String subcountry, Pageable pageable) {
    return service.searchHits(name, country, subcountry, pageable);
  }
}
Çıktısı şöyle. Ancak bu çıktıda sayfalama bilgisi yok
{
  "totalHits": 3,
  "totalHitsRelation": "EQUAL_TO",
  "maxScore": "NaN",
  "scrollId": null,
  "searchHits": [
    {
      "index": "city",
      "id": "yqoYEIIB55LQo2aMkOKS",
      "score": "NaN",
      "sortValues": [
        "benešov"
      ],
      "content": {
        "id": "yqoYEIIB55LQo2aMkOKS",
        "name": "Benešov",
        "country": "Czech Republic",
        "subcountry": "Central Bohemia",
        "geonameid": 3079508
      },
      "highlightFields": {},
      "innerHits": {},
      "nestedMetaData": null,
      "routing": null,
      "explanation": null,
      "matchedQueries": []
    },
    ...
  ],
  "aggregations": null,
  "suggest": null,
  "empty": false
}
aggregations alanı
Açıklaması şöyle
Since Spring Data Elasticsearch 5.0.1, the SearchHit instance contains aggregations attribute filled (it was null in the previous version). 

Aynı şeyi Spring ile sayfalama bilgisini de dönerek yapabiliriz. Şöyle yaparız
import static org.springframework.data.elasticsearch.core.SearchHitSupport.searchPageFor;

public SearchPage<City> searchPage(String name, String country, String subcountry, 
                                   Pageable pageable) {
  return searchPageFor(searchHits(name, country, subcountry, pageable), pageable);
}
Bu durumda çıktı şöyle. Ancak bu sefer de content ve searchHits altında gerçek veri iki defa tekrar ediyor
{
  "content": [
    {
      "index": "city",
      "id": "yqoYEIIB55LQo2aMkOKS",
      "score": "NaN",
      "sortValues": [
        "benešov"
      ],
      "content": {
        "id": "yqoYEIIB55LQo2aMkOKS",
        "name": "Benešov",
        "country": "Czech Republic",
        "subcountry": "Central Bohemia",
        "geonameid": 3079508
      },
      "highlightFields": {},
      "innerHits": {},
      "nestedMetaData": null,
      "routing": null,
      "explanation": null,
      "matchedQueries": []
    },
    ...
  ],
  "pageable": {
    "sort": {
      "empty": false,
      "sorted": true,
      "unsorted": false
    },
    "offset": 0,
    "pageNumber": 0,
    "pageSize": 5,
    "unpaged": false,
    "paged": true
  },
  "searchHits": {
    "totalHits": 3,
    "totalHitsRelation": "EQUAL_TO",
    "maxScore": "NaN",
    "scrollId": null,
    "searchHits": [
      {
        "index": "city",
        "id": "yqoYEIIB55LQo2aMkOKS",
        "score": "NaN",
        "sortValues": [
          "benešov"
        ],
        "content": {
          "id": "yqoYEIIB55LQo2aMkOKS",
          "name": "Benešov",
          "country": "Czech Republic",
          "subcountry": "Central Bohemia",
          "geonameid": 3079508
        },
        "highlightFields": {},
        "innerHits": {},
        "nestedMetaData": null,
        "routing": null,
        "explanation": null,
        "matchedQueries": []
      },
      ...
    ],
    "aggregations": null,
    "suggest": null,
    "empty": false
  },
  "totalPages": 1,
  "totalElements": 3,
  "size": 5,
  "number": 0,
  "sort": {
    "empty": false,
    "sorted": true,
    "unsorted": false
  },
  "first": true,
  "last": true,
  "numberOfElements": 3,
  "empty": false
}
Bundan da kurtulmak için şöyle yaparız
import static org.springframework.data.elasticsearch.core.SearchHitSupport.unwrapSearchHits;
@SuppressWarnings("unchecked")
public Page<City> search(String name, String country, String subcountry, Pageable pageable) {
  return (Page<City>) unwrapSearchHits(searchPage(name, country, subcountry, pageable));
}
Bu durumda çıktı şöyle. Her şey content altında
{
  "content": [
    {
      "id": "yqoYEIIB55LQo2aMkOKS",
      "name": "Benešov",
      "country": "Czech Republic",
      "subcountry": "Central Bohemia",
      "geonameid": 3079508
    },
    ...
  ],
  "pageable": {
    "sort": {
      "empty": false,
      "sorted": true,
      "unsorted": false
    },
    "offset": 0,
    "pageNumber": 0,
    "pageSize": 5,
    "unpaged": false,
    "paged": true
  },
  "last": true,
  "totalPages": 1,
  "totalElements": 3,
  "size": 5,
  "number": 0,
  "sort": {
    "empty": false,
    "sorted": true,
    "unsorted": false
  },
  "first": true,
  "numberOfElements": 3,
  "empty": false
}



Hiç yorum yok:

Yorum Gönder