Couchbase Search java SDK searching on a number

Hello,
I have a question regarding couchbase search java sdk.
In java sdk we have a numericRange() function which allows you to work on a numeric range of values < >= <=, etc … But what i want is to search on exactly that number , for eg: if a field say id has number 123,

when i pass in a query with number 123 it should give me that info. I am able to do that with N1QL but not with couchbase search java sdk .

And if i use n1ql I am unable to find out how much total hits are there if i use pagination, for eg: if i paginate from 1-100 and there are 1000 hits … how would i know the end of the pagination if i dont know there total hits (java search sdk gives me that with searchmetrics)… If i get the total hits i would basically know i need to paginate 10 times .

Please help me with this.

Regards

Try the following. cc @abhinav

{
 "min": 123, "max": 123,
 "inclusive_min": true,
 "inclusive_max": true,
 "field": "id"
}

Search Request JSON Properties | Couchbase Docs

The query that @vsr1 recommends above is accurate.

In full text search, the only way we support point look up over numeric data is in the form of a range query with min and max set to the same value and setting them as inclusive.

total_hits is part of the response returned by the search service, so you should be able to leverage that to paginate as many times as you see fit.

@vsr1 @abhinav thank you for your prompt reply … I will try that out … :slight_smile:

@vsr1 @abhinav I have one more question…
So I am also doing alphabetic sort on a field called name … So if I want to do a A-Z I put I sort by field name and desc as false in sdk… But but when I do that it sorts as Z-A …is Z-A considered ascending and A-Z considered desc… So to get A-Z sort I have to put desc as true

Regards,
Herat

Configures the list of fields (including special fields) which are used for sorting purposes. If empty, the default sorting (descending by score) is used by the server. The list of sort fields can include actual fields (like "firstname" but then they must be stored in the index, configured in the server side mapping). Fields provided first are considered first and in a "tie" case the next sort field is considered. So sorting by "firstname" and then "lastname" will first sort ascending by the firstname and if the names are equal then sort ascending by lastname. Special fields like "_id" and "_score" can also be used. If prefixed with "-" the sort order is set to descending. If no sort is provided, it is equal to sort("-_score"), since the server will sort it by score in descending order.

Parameters:
`sort` - the fields that should take part in the sorting.
Returns:
this SearchQuery for chaining.

Thank you for your reply, @vsr1

I am using couchbase v6.6.1 and java sdk version 3.3.2

The problem is that i am trying to query few things which i am able to do with Search in N1QL and able to retrieve values …

{"conjuncts":[{"field":"type","match":"watchlist"},{"inclusive_max":true,"min":1234,"field":"id","inclusive_min":true,"max":1234}]}"

but by using Search Java sdk , it returns 0 results …

final SearchQuery conjunctsQuery = SearchQuery.conjuncts(queryList.toArray(new SearchQuery[queryList.size()]));

        final SearchOptions options = SearchOptions.searchOptions()
                .limit(Long.valueOf(limit).intValue()) // value 20
                .skip(Long.valueOf(offset).intValue()) // value 0
                .fields("cardId", "thumbnailUrl", "contentType", "watchlistId", "contentName", "id");

Any reasons why?

I was initially trying with couchbase 7.0 in local docker and it worked , when i changed my server version 6.6.1 which we have in our environment , it is not working .

But N1QL works… but the issue is that sorting in N1ql is not working on the field contentName

One thing to note - to effectively support sorting on document fields, you will need to enable “doc values” for the field while configuring your index - contentName in your example.

For your java SDK related question - I will ping @daschl .

@abhinav Thank you for your reply

Here is my index i have enabled docValues

Doesnt seem to work … with query

select * from `mc-user-data` where SEARCH(`data , {"indexName": "fts-data, "sort":[{"by": "field", "field": "contentName", "missing": "last"}], "conjuncts":[{"field":"type","match":"watchlist"},{"inclusive_max":true,"min":1234,"field":"accountId","inclusive_min":true,"max":1234}]})

This sorts

Watch
Dog
Alias

It should infact sort as
Alias
Dog
Watch

Your syntax looks off to me. Try this:

SELECT * FROM `mc-user-data` d
WHERE SEARCH(d, 
{
  "sort": [
    {
      "by": "field",
      "field": "contentName",
      "missing": "last"
    }
  ],
  "query": {
    "conjuncts": [
      {
        "field": "type",
        "match": "watchlist"
      },
      {
        "inclusive_max": true,
        "min": 1234,
        "field": "accountId",
        "inclusive_min": true,
        "max": 1234
      }
    ]
  }
});

Note that I’ve placed the conjuncts within “query”.

Separately, what version of couchbase server are you using?

We had an issue with sorting over fields that was addressed in 6.6.1.
Reference: https://issues.couchbase.com/browse/MB-40730

If you’re on a later version than that, would you share a couple sample documents and your index definition - so I can verify what’s going on.