라이브러리

[PHP] SolrQuery::__construct - 생성자




SolrQuery::__construct

SolrQuery는 Elasticsearch의 PHP 클라이언트인 ElasticsearchClientBuilder를 통해 사용할 수 있습니다. SolrQuery::__construct는 Elasticsearch 쿼리 객체를 생성하는 데 사용되는 생성자입니다.

생성자 인수


SolrQuery::__construct에는 두 가지 인수가 있습니다.

- $index: 인덱스 이름
- $type: 문서 타입 이름 (Elasticsearch 6.x 부터는 사용되지 않음)

예제


아래 예제는 Elasticsearch 쿼리 객체를 생성하는 방법을 보여줍니다.

#hostingforum.kr
php

use ElasticsearchClientBuilder;



// Elasticsearch 클라이언트 빌더 생성

$clientBuilder = ClientBuilder::create();



// Elasticsearch 클라이언트 생성

$client = $clientBuilder->build();



// 인덱스 이름과 문서 타입 이름을 지정하여 쿼리 객체 생성

$query = new ElasticsearchQuery('my_index', 'my_type');



// 쿼리 객체에 쿼리 조건을 추가

$query->setQuery('name:john');



// 쿼리 객체에 필터 조건을 추가

$query->addFilter('age', 30);



// 쿼리 객체에 정렬 조건을 추가

$query->addSort('age', 'asc');



// Elasticsearch 클라이언트를 통해 쿼리 실행

$response = $client->search([

    'index' => 'my_index',

    'type' => 'my_type',

    'body' => $query->toArray(),

]);



// 쿼리 결과 출력

print_r($response);



쿼리 조건 추가


쿼리 조건을 추가하려면 `setQuery` 메서드를 사용합니다. 예를 들어, 이름이 'john'인 문서를 찾으려면 다음과 같이 쿼리 조건을 추가할 수 있습니다.

#hostingforum.kr
php

$query->setQuery('name:john');



필터 조건 추가


필터 조건을 추가하려면 `addFilter` 메서드를 사용합니다. 예를 들어, 나이가 30 이상인 문서를 찾으려면 다음과 같이 필터 조건을 추가할 수 있습니다.

#hostingforum.kr
php

$query->addFilter('age', 30);



정렬 조건 추가


정렬 조건을 추가하려면 `addSort` 메서드를 사용합니다. 예를 들어, 나이를 오름차순으로 정렬하려면 다음과 같이 정렬 조건을 추가할 수 있습니다.

#hostingforum.kr
php

$query->addSort('age', 'asc');



쿼리 실행


쿼리를 실행하려면 Elasticsearch 클라이언트의 `search` 메서드를 사용합니다. 예를 들어, 쿼리 객체를 사용하여 쿼리를 실행하려면 다음과 같이 쿼리 객체를 배열로 변환하고 클라이언트의 `search` 메서드를 호출할 수 있습니다.

#hostingforum.kr
php

$response = $client->search([

    'index' => 'my_index',

    'type' => 'my_type',

    'body' => $query->toArray(),

]);



쿼리 결과 출력


쿼리 결과를 출력하려면 `print_r` 함수를 사용합니다. 예를 들어, 쿼리 결과를 출력하려면 다음과 같이 쿼리 결과를 출력할 수 있습니다.

#hostingforum.kr
php

print_r($response);


  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 8,985건 / 149 페이지

검색

게시물 검색