Metadata total_hits FTS not available in N1QL

Parameter total_hits not returned by the query

SELECT SEARCH_META().total_hits AS meta
FROM Import AS import USE INDEX(index_table USING FTS)
WHERE import.type = 'bill'
    AND SEARCH(import, {"indexName": "index_table", "size": 100, "score": "none", "from": 0, "query": {"conjuncts": [{"inclusive_start": true, "inclusive_end": true, "field": "actual_arrival_date", "start": "2018-01-01", "end": "2020-01-31"}, {"field": "containers.loads.description_text", "match_phrase": "house"}]}, "sort": ["actual_arrival_date"]})

I need the total number of records that match the search in the index. The above I pages but does not return the total_hits

Hi @nelsonxx1,
The metadata extracted and stored from an FTS search, is done on a per document basis. Furthermore,

By default, the metadata includes the score and ID of the search result. It may also include other metadata requested by advanced search options, such as the location of the search terms or an explanation of the search results.

So, I don’t think if you will be able to get the total number of hits from the metadata alone. Also, there are only a few specific parameters that can be accessed from the SEARCH_META return value, here is an example and explanation about the return values:


@thejas is right.

  • N1QL queries only interpret the “hits” part of the search response obtained from the search service.
  • SEARCH_META() will only contain document details …
    • id … which is the document key
    • score … which is the tf-idf relevancy score for the document based on the search criteria (only if score != “none” in the search request)
    • fields … if requested for in the search request

If you wish to obtain the total hits, try …

SELECT count(*) ...