How to construct Compound query in Nodejs SDK?

Full Text Search (SearchQuery)
version: Coucbase 6.0.3
environment: AWS Lambda Nodejs 10.x
SDK Version: 2.6

My objective is to return all document containing “te” anywhere in the name field (“must”)
AND for which the country field is either ‘US’ or ‘XX’ - the code for “undetermined” through the Nodejs SDK.

My primary question is how to construct the request below via the Nodejs SDK 2.6 in FTS that achieves the query structure below. Secondly, in what way is the query below deficient in that it is not choosing only ‘XX’ OR ‘US’

and not finding the documentation how to do so correctly. I have tried a variety of combinations but each throw errors. All other simple queries work as expected via the SDK.

I have baselined this request via http through port 8094 in the cluster and the query returns documents matching the name correctly, although the country is not filtered correctly.(separate issue from the main issue?).

"query": {
        "must": {
            "conjuncts": [
                {
                    "match": "TE",
                    "field": "name",
                    "prefix_length": 0,
                    "fuzziness": 0
                }
            ]
        },
        "should": {
            "disjuncts": [
                {
                    "match": "US",
                    "field": "country",
                    "prefix_length": 0,
                    "fuzziness": 0
                },
                {
                    "match": "XX",
                    "field": "country",
                    "prefix_length": 0,
                    "fuzziness": 0
                }
            ],
            "min": 0
        }
    }

}

Hey @jgcoding,

The solution should be something along the lines of:

function build_fts_query (options) {
  options.isSimilarQuery = SearchQuery.new(‘entity_name_fulltext’, 
    SearchQuery.boolean().must(
      SearchQuery.conjuncts(
        SearchQuery.match(options.companyName || options.for_name).field(‘name’),
        SearchQuery.match(options.country).field(‘country’))));
  return options;
}

Cheers, Brett

Thank you @brett19,

I have made a number of edits to the question as I worked through a number of solutions and I sought accurately explain what I was seeking. In the end. I added what I hope is a more straightforward request along with the object I assume would be necessary represented in the SDK.

To summarize, I need to search all documents matching the value in “options.name” AND the country matching the value in options.country OR ‘XX’

The solution provided successfully progressed constructing the must but I assume I still need a OR or something in a “should”?

After I posted this question, I succeeded in getting exactly what I wanted the query object below via postman but it does not translate somehow when actually piped through the SDK.

"query": {
    "conjuncts": [
        {
            "match": "TE",
            "field": "name"
        },
        {
            "disjuncts": [
                {
                    "match": "mm",
                    "field": "country"
                },
                {
                    "match": "XX",
                    "field": "country"
                }
            ]
        }
    ]
}

I updated my question with a second part

@brett19
My timing is less than fortunate.

Please refer to my follow up to your original solution. Look forward to your reply.

Thanks for helping, it is very much appreciated

Hey @jgcoding,

You’re query text should look something like this in Node.js:

SearchQuery.conjuncts(
  SearchQuery.match('TE').field(‘name’),
  SearchQuery.disjuncts(
    SearchQuery.match('MM').field(‘country’),
    SearchQuery.match('XX').field(‘country’)))

Cheers, Brett

1 Like

@brett19 - that worked! Thank you!

I had some other issues I was creating I discovered during implementation of your suggestion I worked through. Now, I am getting the same set of results from the SDK for the same query as I was directly through http API in postman.

I appreciate your help very much!

Have a great day. You have made mine.

Jay

Although it wasn’t necessary for this solution, I am still curious how the query object below should be constructed in the nodejs SDK:

"query": {
        "must": {
            "conjuncts": [
                {
                    "match": "TE",
                    "field": "name",
                    "prefix_length": 0,
                    "fuzziness": 0
                }
            ]
        },
        "should": {
            "disjuncts": [
                {
                    "match": "US",
                    "field": "country",
                    "prefix_length": 0,
                    "fuzziness": 0
                },
                {
                    "match": "XX",
                    "field": "country",
                    "prefix_length": 0,
                    "fuzziness": 0
                }
            ],
            "min": 0
        }
    }

}