FTS + N1QL Conjuncts and Disjuncts

I’m trying to use N1QL + FTS to identify this:

SELECT meta().id FROM default
WHERE type = ‘contact’
AND environmentId = xyz
AND isActive = true
AND (primaryPhone = 123 OR secondaryPhone = 123 OR …)

I’m not able to get conjucts + disjuncts working correctly and this is all I can find and it features precisely one example with only conjuncts: https://blog.couchbase.com/n1ql-and-search-how-to-leverage-fts-index-in-n1ql-query/

To illlustrate the problem:
SELECT meta(co).id FROM app co
WHERE co.type = ‘contact’
AND SEARCH(co, {“conjuncts”: [
{“field”: “envId”, “match”: “123”},
{“field”: “active”, “bool”: true}]
}, {“index”: “correct-index”})

gives me all the contacts (so far so good)
SELECT meta(co).id FROM app co
WHERE co.type = ‘contact’
AND SEARCH(co, {“conjuncts”: [
{“field”: “envId”, “match”: “123”},
{“field”: “active”, “bool”: true}],
{“field”: “pPhone”, “match”:“278” } // this is new!!!
}, {“index”: “correct-index”})

gives me one contact (so far so good)

now I move the latter into a disjunct:
SELECT meta(co).id FROM app co
WHERE co.type = ‘contact’
AND SEARCH(co,
{
“conjuncts”: [{“field”: “envId”, “match”: “123”}, {“field”: “isActive”, “bool”: true}],
“disjuncts”: [{“field”: “pPhone”, “match”:“278” }]

}, {“index”: “contact-index”})

gives me ALL THE CONTACTS which is not right. My understanding is the default is ALL the conjucts AND (any of the disjuncts)

Am I getting the syntax wrong? Missing something else?

@naftali would you share your FTS index definition here?

Thanks @naftali.
Your index definition looks good, but your query syntax is incorrect.
Note that the disjuncts has to be a child query of the conjuncts.

For your index definition, this would be the right query -

{
 "conjuncts": [
   {
     "field": "environmentId",
     "match": "123"
   },
   {
     "field": "isActive",
     "bool": true
   },
   {
     "disjuncts": [
       {
         "field": "primaryPhone",
         "match": "278"
       }
     ]
   }
 ]
}
1 Like