라이브러리
[PHP] SolrDocument::current - 현재 필드를 검색합니다.
PHP에서 SolrDocument::current() 메서드
SolrDocument::current() 메서드는 SolrDocument 객체의 현재 항목을 반환합니다. SolrDocument 객체는 Solr에서 검색한 문서를 나타내는 객체입니다. 이 메서드는 Iterator 인터페이스를 구현한 객체에서 사용할 수 있습니다.
예제
#hostingforum.kr
php
// SolrClient 객체를 생성합니다.
$client = new SolrClient('http://localhost:8983/solr');
// 검색을 수행합니다.
$query = new SolrQuery();
$query->setQuery('키워드');
$query->setStart(0);
$query->setRows(10);
$result = $client->query($query);
// 결과를 처리합니다.
foreach ($result->getResponse()->getResponse()->getDocuments() as $document) {
// 현재 항목을 반환합니다.
$currentDocument = $document->current();
echo "ID: " . $currentDocument['id'] . "
";
echo "제목: " . $currentDocument['title'] . "
";
echo "내용: " . $currentDocument['content'] . "
";
}
Iterator 인터페이스
SolrDocument 객체는 Iterator 인터페이스를 구현한 객체이므로, foreach 문을 사용하여 문서를 처리할 수 있습니다. Iterator 인터페이스는 다음 메서드를 구현해야 합니다.
* `current()`: 현재 항목을 반환합니다.
* `key()`: 현재 항목의 키를 반환합니다.
* `next()`: 다음 항목으로 이동합니다.
* `rewind()`: 처음 항목으로 이동합니다.
* `valid()`: 현재 항목이 유효한지 여부를 반환합니다.
예제 (Iterator 인터페이스)
#hostingforum.kr
php
// SolrDocument 객체를 생성합니다.
$document = new SolrDocument();
// Iterator 인터페이스를 구현한 객체를 생성합니다.
$iterator = new Iterator($document);
// Iterator 인터페이스를 구현한 객체를 사용하여 문서를 처리합니다.
foreach ($iterator as $key => $value) {
echo "키: $key
";
echo "값: $value
";
}
// Iterator 인터페이스를 구현한 객체를 사용하여 문서를 처리합니다.
$iterator->rewind();
while ($iterator->valid()) {
echo "키: " . $iterator->key() . "
";
echo "값: " . $iterator->current() . "
";
$iterator->next();
}
참고
* SolrClient 클래스: [https://solr.apache.org/api/7_10_3/](https://solr.apache.org/api/7_10_3/)
* SolrQuery 클래스: [https://solr.apache.org/api/7_10_3/](https://solr.apache.org/api/7_10_3/)
* SolrDocument 클래스: [https://solr.apache.org/api/7_10_3/](https://solr.apache.org/api/7_10_3/)
* Iterator 인터페이스: [https://www.php.net/manual/kr/class.iterator.php](https://www.php.net/manual/kr/class.iterator.php)
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.