Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get total index records for datatables #283

Open
HawtDogFlvrWtr opened this issue Mar 9, 2022 · 5 comments
Open

get total index records for datatables #283

HawtDogFlvrWtr opened this issue Mar 9, 2022 · 5 comments
Labels

Comments

@HawtDogFlvrWtr
Copy link

I know this repo is all but dead, but i was curious if anyone figured out how to get the total count of records in index. Using the elastic libraries without cake, i just left query empty, specified the index and pulled count. That doesn't seem to work with this library wrapper for elastic.

@ndm2
Copy link
Contributor

ndm2 commented Mar 9, 2022

There's not much going on, but it's certainly not yet necrotic... anyhow, you can count using the regular query builder count API:

$totalDocuments = $this->Comments->find()->count();

If you need to refresh in before hand:

$this->Comments->getConnection()->getIndex($this->Comments->getName())->refresh();

@HawtDogFlvrWtr
Copy link
Author

Thanks for responding. I only said that because I was searching closed issues for the answer to this and noticed most were marked stale by the bot. Meant no disrespect at all.

I tried $totalDocuments = $this->Comments->find()->count(); but it only returned the total window size (10k default) instead of the total document count (which in my case is somewhere around 14 million). I'll give it a refresh before attempting to pull the total when I get back to work tomorrow and let you know what happens.

@ndm2
Copy link
Contributor

ndm2 commented Mar 9, 2022

That's the default limit that Elasticsearch imposes, AFAIK it applies to the _search endpoint that is used for counting here, as well as to the alternative _count endpoint.

As of ES 7 the track_total_hits parameter can be set to true (max integer) or an integer that sets the total hits limit, but it seems there's no API here yet that would allow to set that parameter for counting. Until it is implemented, you could do what's being done under the hood, and manually add the parameter to it, like:

$type = $this->Comments
    ->getConnection()
    ->getIndex($this->Comments->getName())
    ->getType($this->Comments->getType());

$query = $this->Comments
    ->find()
    ->limit(0)
    ->compileQuery()
    ->setParam('track_total_hits', true);

$totalDocuments = $type->search($query)->getTotalHits();

@andrii-pukhalevych
Copy link
Contributor

I recommend inheriting class Index extends \Cake\ElasticSearch\Index in your project.
And inheriting class Query extends \Cake\ElasticSearch\Query.

Then you can override method Index::query to return an instance of the inherited query, where you can write things like this:

class Query extends \Cake\ElasticSearch\Query {

	public function compileQuery() {
		$this->_elasticQuery->setTrackTotalHits();

		return parent::compileQuery();
	}
}

@github-actions
Copy link

This issue is stale because it has been open for 120 days with no activity. Remove the stale label or comment or this will be closed in 15 days

@github-actions github-actions bot added the stale label Mar 16, 2023
@othercorey othercorey added pinned and removed stale labels Mar 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants