Combine field on indexing

Does couchbase support combining fields for indexing? I am looking to do something similar to:

I know i can create a new field in my model, but it would be really nice if I can specify a way to index a field that is compose with the value of other two or more fields.
Thanks!

@monti.mauro Unfortunately, with couchbase there’s no direct query that offers merging contents from multiple fields and searching from the resultant data as if they belonged to a single field.

However there is a way this can be achieved by an index definition change, here’s how with an example -

Let’s say you have the 3 fields (from the example in the elastic documentation) whose results you want to combine …

  • “title”
  • “abstract”
  • “body”

Here’s what you’ll need to do while defining the index:

  • you will have the option to enter a searchable as for each of these fields.
  • For your current use case, you can set the searchable as for all 3 fields to a common name:
    • title will be searchable as fieldGroup
    • abstract will be searchable as fieldGroup
    • body will be searchable as fieldGroup

Your index mapping would then look like …

...
      "default_mapping": {
        "default_analyzer": "standard",
        "dynamic": false,
        "enabled": true,
        "properties": {
          "title": {
            "dynamic": false,
            "enabled": true,
            "fields": [
              {
                "index": true,
                "name": "fieldGroup",
                "store": true,
                "type": "text"
              }
            ]
          },
          "abstract": {
            "dynamic": false,
            "enabled": true,
            "fields": [
              {
                "index": true,
                "name": "fieldGroup",
                "store": true,
                "type": "text"
              }
            ]
          },
         "body": {
            "dynamic": false,
            "enabled": true,
            "fields": [
              {
                "index": true,
                "name": "fieldGroup",
                "store": true,
                "type": "text"
              }
            ]
          }
        }
      }
...

Now you search request would simply be …

{
  "query": {
    "match": "database systems",
    "field": "fieldGroup",
    "operator": "and"
  }
}

Hope this works for you.

1 Like

Thanks @abhinav! that worked for my use case…

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.