Full text search by regex key

Hi, I am trying to create a full text search index based on a regex on a key. I can understand from documentation how the regex expression works.
Our Ids are like this:
event:email:action:input:61ae2ecf-ec6c-4eca-ac56-ac6fd9f38ec7:engine-facing-node-2
I want to get all events that start with “event:email:action:input:” and assign it to the mapping object that we created “email”

What should go on the “Doc ID with regex:” ?
event:(.):action:input:.

So it maps to email using the () ?

Thanks

Unfortunately when we built this capability, we opted for a simple approach, so we do not support capture groups. The portion of the key that matches the entire regular expression will be used for the resulting type. This makes it hard to extract portions of the key, particularly in the middle or end and not include the surrounding context in the type name.

One option, if all documents of type email start with event is to use the longer pattern ‘event:email’ as your pattern (and that then is the result type).

Another option is to use a pattern like

\bemail\b

The \b will match word boundaries (which : satisfies). This is more brittle, and can match email showing up in other parts of the key if that is possible (you would have to examine your key naming convention closely).

That will be what i need to match, so the regex will be “event:email:action:input” and the object mapping that i need should be called also “event:email:action:input”, would that be correct?

Using that as the regex will work, but will only match documents with that exact text. I’d suggest adding the ^ character at the start to ensure it is anchored to the start of the key. And type name used in the FTS mapping will also need to be the full “event:email:action:input”.

Will try it out, thanks for your help.

Hi,
I have created a field name employeeDetails which has concatenated information
firstName:middleName:lastName
would this regex work for searching ‘firstName:*:lastName’ in case the user don’t specify the middleName while searching

@Sachin,

Yes. It should work. If in case it is not working for your so far, it is due to the analysers set for the field. Note that regex query is a non analytic query, so you need to either set matching analysers(keyword) for your text field Or make similar changes (lower/upper) to the query text to match the indexed contents).
ref - https://docs.couchbase.com/server/current/fts/fts-query-types.html#non-analytic-queries

Cheers!

Thank @sreeks for being so quick. But I think I did not put my question correctly.
I have a large document and it has an embedded array object, for eg -
{
id : …
name : …
“territory”: [
{
“country”: “GB”,
“selected”: false
},
{
“country”: “US”,
“selected”: true
}
]
}

I am using fts to select for a document that has the country as GB and selected as true. The query is returning me a false positive. Just wanted to understand whether this can be handled by fts indexes.

Also, I came across this link which has a comment by @Gabriel_P1 saying that it can be handled by setting the correct index -

I could not find any concrete examples of how to search for the required combinations in an array of objects in couchbase.
A solution which I was thinking of is to flatten the object and store it in a field
for instance :
{
id : …
name : …
“territory”: [
{
“country”: “GB”,
“selected”: false
"territoryFlattened ": GB, false
},
{
“country”: “US”,
“selected”: true
"territoryFlattened ": US, true
}
]
}
while creating the query, the user could use a query on the flattened field.
And to use regex if he does not provide values for either of the attributes, so the query would be like the search for the match ‘"territoryFlattened ": GB, *’ or ‘"territoryFlattened ": *, false’
What are your thoughts on it, does it look workable?
I would be able to leverage fts completely without moving to GSI or having to write complex queries
Thanks in advance !!

Indeed,
It looks like a plausible approach.
And I assume you are making the territoryFlattened as a text type field containing values like
“Field1-text, Field2-text,…FieldN-text”.

Cheers!

1 Like