Query for search including join on different bucket performing very slow

Hi Team,
I am using a below query to get the search results, using join on two different buckets but it is taking near about 29 seconds for returning the result, sometimes it is taking this much time but returning the empty result-set.

SELECT history.*, 
cms.pageId, cms.ticketNumber, cms.effectiveTime,
      cms.expiryTime, cms.title, cms.pageType, cms.type, cms.name, cms.pageGroup,
      cms.supersededTime
      FROM `bucket1` AS history
      JOIN `bucket2` AS cms
      ON KEYS TOSTRING(history.instanceRef)
       WHERE history.type="history"
       AND (SEARCH(cms, {
      "match": "pool",
      "field": "name",
      "analyzer": "standard"
    }) OR SEARCH(cms, {
      "match": "pool",
      "field": "title",
      "analyzer": "standard"
    }) OR SEARCH(cms, {
      "match": "pool",
      "field": "pageName",
      "analyzer": "standard"
    })OR SEARCH(cms, {
      "match": "pool",
      "field": "ticketNumber",
      "analyzer": "standard"
    }))
ORDER BY history.timestamp DESC
      LIMIT 20
      OFFSET 0

I am using the index as CREATE INDEX adv_history_order_by_timestamp ON bucket1(type,timestamp DESC)

I am not sure how to add and use indexes on different bucket here (ex: bucket2) or is it possible to do it.
Can anyone suggest how I can improve the performance of this query.

You should combine all SEARCH() into one disjunct cc @abhinav

Can you explain more about that how to implement that.

I have tried the below query, it is still taking the 20sec.

SELECT history.*, cms.pageId, cms.ticketNumber, cms.effectiveTime,
      cms.expiryTime, cms.title, cms.pageType, cms.type, cms.name, cms.pageGroup,
      cms.supersededTime
      FROM bucket1  AS history
      JOIN bucket2 AS cms
      ON KEYS TOSTRING(history.instanceRef)
       WHERE history.type="history" AND
       SEARCH(cms,
    {
       "should": {
                   "disjuncts": [
                       { "disjuncts": [
                            {"field":"name", "match": "pool", "boost": 1.4},
                            {"field":"name", "match": "pool", "fuzziness":1, "boost": 1.4}
                         ]
                       },
                       { "disjuncts": [
                              {"field":"title", "match": "pool"},
                              { "field":"title", "match": "pool", "fuzziness":1}
                         ]
                       },
                       { "disjuncts": [
                              {"field":"pageName", "match": "pool"},
                              { "field":"pageName", "match": "pool", "fuzziness":1}
                         ]
                       },
                       { "disjuncts": [
                              {"field":"ticketNumber", "match": "pool"},
                              { "field":"ticketNumber", "match": "pool", "fuzziness":1}
                         ]
                       }
                  ]
       }
    }
)
      ORDER BY history.timestamp DESC
      LIMIT 20
      OFFSET 0

Run the query in Query Workbench and post profile stats (Plan Text Tab)

Here is the output of Plan text tab after query execution, 

{
  "#operator": "Sequence",
  "#stats": {
    "#phaseSwitches": 1,
    "execTime": "2.563µs"
  },
  "~children": [
    {
      "#operator": "Authorize",
      "#stats": {
        "#phaseSwitches": 3,
        "execTime": "14.785µs",
        "servTime": "9.944767ms"
      },
      "privileges": {
        "List": [
          {
            "Target": "default:bucket1",
            "Priv": 7
          },
          {
            "Target": "default:bucket2",
            "Priv": 7
          }
        ]
      },
      "~child": {
        "#operator": "Sequence",
        "#stats": {
          "#phaseSwitches": 1,
          "execTime": "5.231µs"
        },
        "~children": [
          {
            "#operator": "Sequence",
            "#stats": {
              "#phaseSwitches": 2,
              "execTime": "346.355µs",
              "kernTime": "132ns",
              "state": "running"
            },
            "~children": [
              {
                "#operator": "IndexScan3",
                "#stats": {
                  "#itemsOut": 231828,
                  "#phaseSwitches": 927315,
                  "execTime": "634.092059ms",
                  "kernTime": "34.972425874s",
                  "servTime": "88.448801ms"
                },
                "as": "history",
                "index": "adv_history_order_by_timestamp",
                "index_id": "d76f6f85464e77b3",
                "index_order": [
                  {
                    "keypos": 0
                  },
                  {
                    "desc": true,
                    "keypos": 1
                  }
                ],
                "index_projection": {
                  "primary_key": true
                },
                "keyspace": "bucket1",
                "namespace": "default",
                "spans": [
                  {
                    "exact": true,
                    "range": [
                      {
                        "high": "\"history\"",
                        "inclusion": 3,
                        "low": "\"history\""
                      }
                    ]
                  }
                ],
                "using": "gsi",
                "#time_normal": "00:00.722",
                "#time_absolute": 0.72254086
              },
              {
                "#operator": "Fetch",
                "#stats": {
                  "#itemsIn": 231828,
                  "#itemsOut": 231828,
                  "#phaseSwitches": 956295,
                  "execTime": "744.89263ms",
                  "kernTime": "7.557037407s",
                  "servTime": "27.429220346s"
                },
                "as": "history",
                "keyspace": "bucket1",
                "namespace": "default",
                "#time_normal": "00:28.174",
                "#time_absolute": 28.174112976
              },
              {
                "#operator": "Join",
                "#stats": {
                  "#itemsIn": 231828,
                  "#itemsOut": 6294,
                  "#phaseSwitches": 505227,
                  "execTime": "8.257826693s",
                  "kernTime": "5.788377182s",
                  "servTime": "21.717608222s"
                },
                "as": "cms",
                "keyspace": "bucket2",
                "namespace": "default",
                "on_keys": "to_string((`history`.`instanceRef`))",
                "#time_normal": "00:29.975",
                "#time_absolute": 29.975434915
              },
              {
                "#operator": "Sequence",
                "#stats": {
                  "#phaseSwitches": 2,
                  "execTime": "614.105µs",
                  "kernTime": "318ns",
                  "state": "running"
                },
                "~children": [
                  {
                    "#operator": "Filter",
                    "#stats": {
                      "#itemsIn": 6294,
                      "#itemsOut": 3,
                      "#phaseSwitches": 12597,
                      "execTime": "6.962879063s",
                      "kernTime": "28.801017439s"
                    },
                    "condition": "(((`history`.`type`) = \"history\") and (((search(`cms`, {\"analyzer\": \"standard\", \"field\": \"name\", \"match\": \"pool\"}) or search(`cms`, {\"analyzer\": \"standard\", \"field\": \"title\", \"match\": \"pool\"})) or search(`cms`, {\"analyzer\": \"standard\", \"field\": \"pageName\", \"match\": \"pool\"})) or search(`cms`, {\"analyzer\": \"standard\", \"field\": \"ticketNumber\", \"match\": \"pool\"})))",
                    "#time_normal": "00:06.962",
                    "#time_absolute": 6.962879063
                  },
                  {
                    "#operator": "InitialProject",
                    "#stats": {
                      "#itemsIn": 3,
                      "#itemsOut": 3,
                      "#phaseSwitches": 14,
                      "execTime": "183.278µs",
                      "kernTime": "35.763708788s"
                    },
                    "result_terms": [
                      {
                        "expr": "`history`",
                        "star": true
                      },
                      {
                        "expr": "(`cms`.`pageId`)"
                      },
                      {
                        "expr": "(`cms`.`ticketNumber`)"
                      },
                      {
                        "expr": "(`cms`.`effectiveTime`)"
                      },
                      {
                        "expr": "(`cms`.`expiryTime`)"
                      },
                      {
                        "expr": "(`cms`.`title`)"
                      },
                      {
                        "expr": "(`cms`.`pageType`)"
                      },
                      {
                        "expr": "(`cms`.`type`)"
                      },
                      {
                        "expr": "(`cms`.`name`)"
                      },
                      {
                        "expr": "(`cms`.`pageGroup`)"
                      },
                      {
                        "expr": "(`cms`.`supersededTime`)"
                      }
                    ],
                    "#time_normal": "00:00.000",
                    "#time_absolute": 0.000183278
                  },
                  {
                    "#operator": "FinalProject",
                    "#stats": {
                      "#itemsIn": 3,
                      "#itemsOut": 3,
                      "#phaseSwitches": 7,
                      "execTime": "7.298µs"
                    },
                    "#time_normal": "00:00.000",
                    "#time_absolute": 0.000007298
                  }
                ],
                "#time_normal": "00:00.000",
                "#time_absolute": 0.000614105
              }
            ],
            "#time_normal": "00:00.000",
            "#time_absolute": 0.000346355
          },
          {
            "#operator": "Limit",
            "#stats": {
              "#itemsIn": 3,
              "#itemsOut": 3,
              "#phaseSwitches": 10,
              "execTime": "12.416µs",
              "kernTime": "13.843µs"
            },
            "expr": "20",
            "#time_normal": "00:00.000",
            "#time_absolute": 0.000012416000000000001
          }
        ],
        "#time_normal": "00:00.000",
        "#time_absolute": 0.0000052309999999999996
      },
      "#time_normal": "00:00.009",
      "#time_absolute": 0.009959552
    },
    {
      "#operator": "Stream",
      "#stats": {
        "#itemsIn": 3,
        "#itemsOut": 3,
        "#phaseSwitches": 9,
        "execTime": "148.656µs",
        "kernTime": "35.773782854s"
      },
      "#time_normal": "00:00.000",
      "#time_absolute": 0.000148656
    }
  ],
  "~versions": [
    "6.5.0-N1QL",
    "6.5.1-6299-enterprise"
  ],
  "#time_normal": "00:00.000",
  "#time_absolute": 0.0000025630000000000003
}

LEFT side produced 231,828 items and final result is 3

Switch JOIN order and use ANSI JOIN and that might be faster,
Create right FTS index

CREATE INDEX ix1 ON bucket1(TOSTRING(instanceRef)) WHERE type = "history";

SELECT history.*, cms.pageId, cms.ticketNumber, cms.effectiveTime,
      cms.expiryTime, cms.title, cms.pageType, cms.type, cms.name, cms.pageGroup,
      cms.supersededTime
      FROM bucket2 AS cms
      JOIN bucket1  AS history
      ON META(cms).id = TOSTRING(history.instanceRef)
WHERE history.type = "history" AND
SEARCH(cms,
    {
       "should": {
                   "disjuncts": [
                       { "disjuncts": [
                            {"field":"name", "match": "pool", "boost": 1.4},
                            {"field":"name", "match": "pool", "fuzziness":1, "boost": 1.4}
                         ]
                       },
                       { "disjuncts": [
                              {"field":"title", "match": "pool"},
                              { "field":"title", "match": "pool", "fuzziness":1}
                         ]
                       },
                       { "disjuncts": [
                              {"field":"pageName", "match": "pool"},
                              { "field":"pageName", "match": "pool", "fuzziness":1}
                         ]
                       },
                       { "disjuncts": [
                              {"field":"ticketNumber", "match": "pool"},
                              { "field":"ticketNumber", "match": "pool", "fuzziness":1}
                         ]
                       }
                  ]
       }
    }
  )
ORDER BY history.timestamp DESC
LIMIT 20
OFFSET 0

Thanks for the reply @vsr1, 
I have tried with the given query and suggested index , but still the performance is very slow here, it is taking 33 sec to return the result. I am attaching the plan text tab output here for your reference.

{
  "#operator": "Sequence",
  "#stats": {
    "#phaseSwitches": 1,
    "execTime": "2.427µs"
  },
  "~children": [
    {
      "#operator": "Authorize",
      "#stats": {
        "#phaseSwitches": 3,
        "execTime": "14.935µs",
        "servTime": "17.377463ms"
      },
      "privileges": {
        "List": [
          {
            "Target": "default:bucket2",
            "Priv": 7
          },
          {
            "Target": "default:bucket1",
            "Priv": 7
          }
        ]
      },
      "~child": {
        "#operator": "Sequence",
        "#stats": {
          "#phaseSwitches": 1,
          "execTime": "11.359µs"
        },
        "~children": [
          {
            "#operator": "Sequence",
            "#stats": {
              "#phaseSwitches": 1,
              "execTime": "11.9µs"
            },
            "~children": [
              {
                "#operator": "IndexScan3",
                "#stats": {
                  "#itemsOut": 31135,
                  "#phaseSwitches": 124543,
                  "execTime": "76.378988ms",
                  "kernTime": "32.269867546s",
                  "servTime": "15.676527ms"
                },
                "as": "cms",
                "index": "adv_deleted_page_by_METAid",
                "index_id": "a3563eac9e8d676",
                "index_projection": {
                  "primary_key": true
                },
                "keyspace": "bucket2",
                "namespace": "default",
                "spans": [
                  {
                    "exact": true,
                    "range": [
                      {
                        "inclusion": 0,
                        "low": "null"
                      }
                    ]
                  }
                ],
                "using": "gsi",
                "#time_normal": "00:00.092",
                "#time_absolute": 0.092055515
              },
              {
                "#operator": "Fetch",
                "#stats": {
                  "#itemsIn": 31135,
                  "#itemsOut": 31135,
                  "#phaseSwitches": 128435,
                  "execTime": "218.756632ms",
                  "kernTime": "27.00366432s",
                  "servTime": "5.582049524s"
                },
                "as": "cms",
                "keyspace": "bucket2",
                "namespace": "default",
                "#time_normal": "00:05.800",
                "#time_absolute": 5.800806156
              },
              {
                "#operator": "Sequence",
                "#stats": {
                  "#phaseSwitches": 1,
                  "execTime": "3.053µs"
                },
                "~children": [
                  {
                    "#operator": "NestedLoopJoin",
                    "#stats": {
                      "#itemsIn": 31135,
                      "#itemsOut": 6344,
                      "#phaseSwitches": 212189,
                      "execTime": "238.58238ms",
                      "kernTime": "33.032827543s"
                    },
                    "alias": "history",
                    "on_clause": "((meta(`cms`).`id`) = to_string((`history`.`instanceRef`)))",
                    "~child": {
                      "#operator": "Sequence",
                      "#stats": {
                        "#phaseSwitches": 62270,
                        "execTime": "451.180744ms",
                        "kernTime": "38.649582ms",
                        "state": "running"
                      },
                      "~children": [
                        {
                          "#operator": "IndexScan3",
                          "#stats": {
                            "#itemsOut": 6344,
                            "#phaseSwitches": 118781,
                            "execTime": "211.606272ms",
                            "kernTime": "6.198151ms",
                            "servTime": "29.105165891s"
                          },
                          "as": "history",
                          "index": "adv_instanceRef_type_test",
                          "index_id": "6d55256bc3efb827",
                          "index_projection": {
                            "primary_key": true
                          },
                          "keyspace": "bucket1",
                          "namespace": "default",
                          "nested_loop": true,
                          "spans": [
                            {
                              "exact": true,
                              "range": [
                                {
                                  "high": "(meta(`cms`).`id`)",
                                  "inclusion": 3,
                                  "low": "(meta(`cms`).`id`)"
                                }
                              ]
                            }
                          ],
                          "using": "gsi"
                        },
                        {
                          "#operator": "Fetch",
                          "#stats": {
                            "#itemsIn": 6344,
                            "#itemsOut": 6344,
                            "#phaseSwitches": 123439,
                            "execTime": "206.094303ms",
                            "kernTime": "29.465355864s",
                            "servTime": "3.135639315s"
                          },
                          "as": "history",
                          "keyspace": "bucket1",
                          "namespace": "default",
                          "nested_loop": true
                        }
                      ]
                    },
                    "#time_normal": "00:00.238",
                    "#time_absolute": 0.23858238
                  },
                  {
                    "#operator": "Filter",
                    "#stats": {
                      "#itemsIn": 6344,
                      "#itemsOut": 27,
                      "#phaseSwitches": 12745,
                      "execTime": "2.697790724s",
                      "kernTime": "30.573700111s"
                    },
                    "condition": "(((`history`.`type`) = \"history\") and search(`cms`, {\"should\": {\"disjuncts\": [{\"disjuncts\": [{\"boost\": 1.4, \"field\": \"name\", \"match\": \"pool\"}, {\"boost\": 1.4, \"field\": \"name\", \"fuzziness\": 1, \"match\": \"pool\"}]}, {\"disjuncts\": [{\"field\": \"title\", \"match\": \"pool\"}, {\"field\": \"title\", \"fuzziness\": 1, \"match\": \"pool\"}]}, {\"disjuncts\": [{\"field\": \"pageName\", \"match\": \"pool\"}, {\"field\": \"pageName\", \"fuzziness\": 1, \"match\": \"pool\"}]}, {\"disjuncts\": [{\"field\": \"ticketNumber\", \"match\": \"pool\"}, {\"field\": \"ticketNumber\", \"fuzziness\": 1, \"match\": \"pool\"}]}]}}))",
                    "#time_normal": "00:02.697",
                    "#time_absolute": 2.697790724
                  },
                  {
                    "#operator": "InitialProject",
                    "#stats": {
                      "#itemsIn": 27,
                      "#itemsOut": 27,
                      "#phaseSwitches": 111,
                      "execTime": "916.61µs",
                      "kernTime": "33.270590214s"
                    },
                    "result_terms": [
                      {
                        "expr": "`history`",
                        "star": true
                      },
                      {
                        "expr": "(`cms`.`pageId`)"
                      },
                      {
                        "expr": "(`cms`.`ticketNumber`)"
                      },
                      {
                        "expr": "(`cms`.`effectiveTime`)"
                      },
                      {
                        "expr": "(`cms`.`expiryTime`)"
                      },
                      {
                        "expr": "(`cms`.`title`)"
                      },
                      {
                        "expr": "(`cms`.`pageType`)"
                      },
                      {
                        "expr": "(`cms`.`type`)"
                      },
                      {
                        "expr": "(`cms`.`name`)"
                      },
                      {
                        "expr": "(`cms`.`pageGroup`)"
                      },
                      {
                        "expr": "(`cms`.`supersededTime`)"
                      }
                    ],
                    "#time_normal": "00:00.000",
                    "#time_absolute": 0.00091661
                  }
                ],
                "#time_normal": "00:00.000",
                "#time_absolute": 0.000003053
              }
            ],
            "#time_normal": "00:00.000",
            "#time_absolute": 0.0000119
          },
          {
            "#operator": "Order",
            "#stats": {
              "#itemsIn": 27,
              "#itemsOut": 20,
              "#phaseSwitches": 79,
              "execTime": "242.346µs",
              "kernTime": "33.271393468s"
            },
            "limit": "20",
            "sort_terms": [
              {
                "desc": true,
                "expr": "(`history`.`timestamp`)"
              }
            ],
            "#time_normal": "00:00.000",
            "#time_absolute": 0.000242346
          },
          {
            "#operator": "Limit",
            "#stats": {
              "#itemsIn": 20,
              "#itemsOut": 20,
              "#phaseSwitches": 41,
              "execTime": "19.017µs"
            },
            "expr": "20",
            "#time_normal": "00:00.000",
            "#time_absolute": 0.000019017
          },
          {
            "#operator": "FinalProject",
            "#stats": {
              "#itemsIn": 20,
              "#itemsOut": 20,
              "#phaseSwitches": 61,
              "execTime": "37.086µs",
              "kernTime": "896.644µs"
            },
            "#time_normal": "00:00.000",
            "#time_absolute": 0.000037086
          }
        ],
        "#time_normal": "00:00.000",
        "#time_absolute": 0.000011359
      },
      "#time_normal": "00:00.017",
      "#time_absolute": 0.017392398
    },
    {
      "#operator": "Stream",
      "#stats": {
        "#itemsIn": 20,
        "#itemsOut": 20,
        "#phaseSwitches": 43,
        "execTime": "875.848µs",
        "kernTime": "33.289221194s"
      },
      "#time_normal": "00:00.000",
      "#time_absolute": 0.0008758479999999999
    }
  ],
  "~versions": [
    "6.5.0-N1QL",
    "6.5.1-6299-enterprise"
  ],
  "#time_normal": "00:00.000",
  "#time_absolute": 0.0000024270000000000002
}


When I have added a condition to force to use the additional index which I have created earlier 
CREATE INDEX adv_type ON `ecom-tesla-cms`(`type`)
I can see the response time slightly decreases to 25 sec, but still it is slow performance.

Can you suggest better approach here..or any alternative way I can achieve this so that result will return in less time.

Also, I have tried to create FTS index and executing below query,

SELECT history.*, cms.pageId, cms.ticketNumber, cms.effectiveTime,
      cms.expiryTime, cms.title, cms.pageType, cms.type, cms.name, cms.pageGroup,
      cms.supersededTime
      FROM `bucket2` AS cms
      JOIN `bucket1` AS history
      ON META(cms).id =  TOSTRING(history.instanceRef)
WHERE SEARCH(cms, 
        { 
          "query": { 
             "disjuncts": [
                       { "disjuncts": [
                            {"field":"name", "match": "pool", "boost": 1.4},
                            {"field":"name", "match": "pool", "fuzziness":1, "boost": 1.4}
                         ]
                       },
                       { "disjuncts": [
                              {"field":"title", "match": "pool"},
                              { "field":"title", "match": "pool", "fuzziness":1}
                         ]
                       },
                       { "disjuncts": [
                              {"field":"pageName", "match": "pool"},
                              { "field":"pageName", "match": "pool", "fuzziness":1}
                         ]
                       },
                       { "disjuncts": [
                              {"field":"ticketNumber", "match": "pool"},
                              { "field":"ticketNumber", "match": "pool", "fuzziness":1}
                         ]
                       }
                  ]
          },
          "score": "none"
        }, { "index": "fts_test" })
        AND history.type='history'

The index fts_test definition is as below: 

{
  "type": "fulltext-index",
  "name": "fts_test",
  "uuid": "3828d3e70b2091cb",
  "sourceType": "couchbase",
  "sourceName": "bucket2",
  "sourceUUID": "341281c3e28240f8e4ea76e8aeaa952a",
  "planParams": {
    "maxPartitionsPerPIndex": 171,
    "indexPartitions": 6
  },
  "params": {
    "doc_config": {
      "docid_prefix_delim": "",
      "docid_regexp": "",
      "mode": "type_field",
      "type_field": "type"
    },
    "mapping": {
      "analysis": {
        "analyzers": {
          "fts_test_analyzer": {
            "char_filters": [
              "html",
              "zero_width_spaces",
              "asciifolding"
            ],
            "token_filters": [
              "apostrophe",
              "camelCase",
              "to_lower",
              "stop_en"
            ],
            "tokenizer": "unicode",
            "type": "custom"
          }
        }
      },
      "default_analyzer": "standard",
      "default_datetime_parser": "dateTimeOptional",
      "default_field": "_all",
      "default_mapping": {
        "dynamic": true,
        "enabled": false
      },
      "default_type": "_default",
      "docvalues_dynamic": true,
      "index_dynamic": true,
      "store_dynamic": false,
      "type_field": "_type",
      "types": {
        "ContentFragment": {
          "dynamic": true,
          "enabled": true
        },
        "deleted_ContentFragment": {
          "dynamic": true,
          "enabled": true
        },
        "deleted_pagedef": {
          "dynamic": true,
          "enabled": true
        },
        "fragment": {
          "dynamic": true,
          "enabled": true
        },
        "page": {
          "dynamic": true,
          "enabled": true
        },
        "pagedef": {
          "dynamic": true,
          "enabled": true
        }
      }
    },
    "store": {
      "indexType": "scorch"
    }
  },
  "sourceParams": {}
}
 

but I am getting error as 
{
    "code": 5010,
    "msg": "Error evaluating filter. - cause: n1fty: index mapping not found"
  }

Not sure what I am missing here, I am new to the couchbase here, any help is greatly appreciated. Thanks in advance.

You must create FTS index on cms cc @abhinav

I’ll make a couple recommendations here.

Firstly like @vsr1 already pointed out - you’ll need to create a search index. I’ll tune this search index based on an optimization we can do for your search request - I see very many disjunctions which can be brought down to improve the latency of the search request. Since you’re looking for a common search term across 4 fields - I recommend adding them all a composite field (_all).

You’ll need to set up a search index over the four fields you seem to need it for. Here’s how index definition would look for it -

{
  "name": "<indexName>",
  "type": "fulltext-index",
  "params": {
    "mapping": {
      "default_mapping": {
        "enabled": true,
        "dynamic": false,
        "properties": {
          "ticketNumber": {
            "enabled": true,
            "dynamic": false,
            "fields": [
              {
                "name": "ticketNumber",
                "type": "text",
                "index": true,
                "include_in_all": true
              }
            ]
          },
          "pageName": {
            "enabled": true,
            "dynamic": false,
            "fields": [
              {
                "name": "pageName",
                "type": "text",
                "index": true,
                "include_in_all": true
              }
            ]
          },
          "title": {
            "enabled": true,
            "dynamic": false,
            "fields": [
              {
                "name": "title",
                "type": "text",
                "index": true,
                "include_in_all": true
              }
            ]
          },
          "name": {
            "enabled": true,
            "dynamic": false,
            "fields": [
              {
                "name": "name",
                "type": "text",
                "index": true
              }
            ]
          }
        }
      },
      "default_type": "_default",
      "default_analyzer": "standard",
      "default_datetime_parser": "dateTimeOptional",
      "default_field": "_all"
    },
    "store": {
      "indexType": "scorch"
    },
    "doc_config": {
      "mode": "type_field",
      "type_field": "type"
    }
  },
  "sourceType": "couchbase",
  "sourceName": "<bucketName>",
  "sourceUUID": "",
  "sourceParams": {
  },
  "planParams": {
    "indexPartitions": 1
  },
  "uuid": ""
}

And here’s your new N1QL query with the embedded search request …

SELECT history.*, cms.pageId, cms.ticketNumber, cms.effectiveTime,
      cms.expiryTime, cms.title, cms.pageType, cms.type, cms.name, cms.pageGroup,
      cms.supersededTime
      FROM bucket2 AS cms
      JOIN bucket1  AS history
      ON META(cms).id = TOSTRING(history.instanceRef)
WHERE history.type = "history" AND
SEARCH(cms,
{
  "should": {
    "disjuncts": [
      {
        "field": "name", "match": "pool", "fuzziness": 1, "boost": 1.4
      },
      {
        "match": "pool", "fuzziness": 1
      }
    ]
  }
}
)
ORDER BY history.timestamp DESC
LIMIT 20
OFFSET 0

p.s. Not specifying the field in the match sub query above will direct it to the _all (composite) field of the index automatically.

Hi @abhinav , Thanks for the reply and apologies for the delay in response from my side.

I have tried the above query by creating specified index, When I am searching with a single word it is giving me result within and about 300ms but when I am searching with some phrase like "Holiday Decorations" it is taking 3sec and more, for some use cases it is taking near about 10 sec.

I can see FTS index is getting used, but there is other index which I have created for bucket1. instanceRef which is also getting used and taking time.

CREATE INDEX adv_instanceRef_type_test ON `bucket1`(to_string(`instanceRef`)) WHERE (`type` = 'history')

Is there a way we can avoid this index or we can make it better to use less time.

I have observed that when we remove order by clause, query returning result within < 500ms everytime, 
I have tried to add sort at FTS index but my FTS index is on bucket2 and sort clause using the field `timestamp` from bucket1, Do I need to add sorting index at bucket1 ?

I have added plan text tab output for your reference here. Thanks in Advance.


{
  "#operator": "Sequence",
  "#stats": {
    "#phaseSwitches": 1,
    "execTime": "3.811µs"
  },
  "~children": [
    {
      "#operator": "Authorize",
      "#stats": {
        "#phaseSwitches": 3,
        "execTime": "5.097µs",
        "servTime": "1.005928ms"
      },
      "privileges": {
        "List": [
          {
            "Target": "default:bucket2",
            "Priv": 7
          },
          {
            "Target": "default:bucket1",
            "Priv": 7
          }
        ]
      },
      "~child": {
        "#operator": "Sequence",
        "#stats": {
          "#phaseSwitches": 1,
          "execTime": "4.017µs"
        },
        "~children": [
          {
            "#operator": "Sequence",
            "#stats": {
              "#phaseSwitches": 1,
              "execTime": "3.202µs"
            },
            "~children": [
              {
                "#operator": "IntersectScan",
                "#stats": {
                  "#itemsIn": 8361,
                  "#itemsOut": 2176,
                  "#phaseSwitches": 21081,
                  "execTime": "10.427267ms",
                  "kernTime": "916.076636ms"
                },
                "scans": [
                  {
                    "#operator": "IndexScan3",
                    "#stats": {
                      "#itemsOut": 6185,
                      "#phaseSwitches": 24747,
                      "execTime": "11.464916ms",
                      "kernTime": "2.744151ms",
                      "servTime": "4.952858ms"
                    },
                    "as": "cms",
                    "index": "adv_deleted_page_by_METAid",
                    "index_id": "a3563eac9e8d676",
                    "index_projection": {
                      "primary_key": true
                    },
                    "keyspace": "bucket2",
                    "namespace": "default",
                    "spans": [
                      {
                        "exact": true,
                        "range": [
                          {
                            "inclusion": 0,
                            "low": "null"
                          }
                        ]
                      }
                    ],
                    "using": "gsi",
                    "#time_normal": "00:00.016",
                    "#time_absolute": 0.016417774
                  },
                  {
                    "#operator": "IndexFtsSearch",
                    "#stats": {
                      "#itemsOut": 2176,
                      "#phaseSwitches": 8709,
                      "execTime": "5.499497ms",
                      "kernTime": "999.094µs",
                      "servTime": "12.695758ms"
                    },
                    "as": "cms",
                    "index": "fts_test",
                    "index_id": "182d425293971f95",
                    "keyspace": "bucket2",
                    "namespace": "default",
                    "search_info": {
                      "field": "\"\"",
                      "outname": "out",
                      "query": "{\"should\": {\"disjuncts\": [{\"boost\": 1.4, \"field\": \"name\", \"fuzziness\": 1, \"match\": \"Holiday Decorations\"}, {\"fuzziness\": 1, \"match\": \"Holiday Decorations\"}]}}"
                    },
                    "using": "fts",
                    "#time_normal": "00:00.018",
                    "#time_absolute": 0.018195255
                  }
                ],
                "#time_normal": "00:00.010",
                "#time_absolute": 0.010427267
              },
              {
                "#operator": "Fetch",
                "#stats": {
                  "#itemsIn": 2176,
                  "#itemsOut": 2176,
                  "#phaseSwitches": 8979,
                  "execTime": "14.089241ms",
                  "kernTime": "1.103137503s",
                  "servTime": "246.762574ms"
                },
                "as": "cms",
                "keyspace": "bucket2",
                "namespace": "default",
                "#time_normal": "00:00.260",
                "#time_absolute": 0.260851815
              },
              {
                "#operator": "Sequence",
                "#stats": {
                  "#phaseSwitches": 1,
                  "execTime": "2.478µs"
                },
                "~children": [
                  {
                    "#operator": "NestedLoopJoin",
                    "#stats": {
                      "#itemsIn": 2176,
                      "#itemsOut": 235,
                      "#phaseSwitches": 13999,
                      "execTime": "19.762664ms",
                      "kernTime": "1.775580286s"
                    },
                    "alias": "history",
                    "on_clause": "((meta(`cms`).`id`) = to_string((`history`.`instanceRef`)))",
                    "~child": {
                      "#operator": "Sequence",
                      "#stats": {
                        "#phaseSwitches": 4352,
                        "execTime": "1.49920299s",
                        "kernTime": "2.411076ms",
                        "state": "running"
                      },
                      "~children": [
                        {
                          "#operator": "IndexScan3",
                          "#stats": {
                            "#itemsOut": 235,
                            "#phaseSwitches": 7468,
                            "execTime": "12.762301ms",
                            "kernTime": "179.783µs",
                            "servTime": "1.659323118s"
                          },
                          "as": "history",
                          "index": "adv_instanceRef_type_test",
                          "index_id": "6d55256bc3efb827",
                          "index_projection": {
                            "primary_key": true
                          },
                          "keyspace": "bucket1",
                          "namespace": "default",
                          "nested_loop": true,
                          "spans": [
                            {
                              "exact": true,
                              "range": [
                                {
                                  "high": "(meta(`cms`).`id`)",
                                  "inclusion": 3,
                                  "low": "(meta(`cms`).`id`)"
                                }
                              ]
                            }
                          ],
                          "using": "gsi"
                        },
                        {
                          "#operator": "Fetch",
                          "#stats": {
                            "#itemsIn": 235,
                            "#itemsOut": 235,
                            "#phaseSwitches": 7618,
                            "execTime": "11.98597ms",
                            "kernTime": "1.680683878s",
                            "servTime": "56.360769ms"
                          },
                          "as": "history",
                          "keyspace": "bucket1",
                          "namespace": "default",
                          "nested_loop": true
                        }
                      ]
                    },
                    "#time_normal": "00:00.019",
                    "#time_absolute": 0.019762664
                  },
                  {
                    "#operator": "Filter",
                    "#stats": {
                      "#itemsIn": 235,
                      "#itemsOut": 235,
                      "#phaseSwitches": 943,
                      "execTime": "3.191671914s",
                      "kernTime": "47.442478ms"
                    },
                    "condition": "(((`history`.`type`) = \"history\") and search(`cms`, {\"should\": {\"disjuncts\": [{\"boost\": 1.4, \"field\": \"name\", \"fuzziness\": 1, \"match\": \"Holiday Decorations\"}, {\"fuzziness\": 1, \"match\": \"Holiday Decorations\"}]}}))",
                    "#time_normal": "00:03.191",
                    "#time_absolute": 3.191671914
                  },
                  {
                    "#operator": "InitialProject",
                    "#stats": {
                      "#itemsIn": 235,
                      "#itemsOut": 235,
                      "#phaseSwitches": 943,
                      "execTime": "50.864378ms",
                      "kernTime": "3.188343741s"
                    },
                    "result_terms": [
                      {
                        "expr": "`history`",
                        "star": true
                      },
                      {
                        "expr": "(`cms`.`pageId`)"
                      },
                      {
                        "expr": "(`cms`.`ticketNumber`)"
                      },
                      {
                        "expr": "(`cms`.`effectiveTime`)"
                      },
                      {
                        "expr": "(`cms`.`expiryTime`)"
                      },
                      {
                        "expr": "(`cms`.`title`)"
                      },
                      {
                        "expr": "(`cms`.`pageType`)"
                      },
                      {
                        "expr": "(`cms`.`type`)"
                      },
                      {
                        "expr": "(`cms`.`name`)"
                      },
                      {
                        "expr": "(`cms`.`pageGroup`)"
                      },
                      {
                        "expr": "(`cms`.`supersededTime`)"
                      }
                    ],
                    "#time_normal": "00:00.050",
                    "#time_absolute": 0.050864378
                  }
                ],
                "#time_normal": "00:00.000",
                "#time_absolute": 0.0000024780000000000002
              }
            ],
            "#time_normal": "00:00.000",
            "#time_absolute": 0.000003202
          },
          {
            "#operator": "Order",
            "#stats": {
              "#itemsIn": 235,
              "#itemsOut": 20,
              "#phaseSwitches": 495,
              "execTime": "1.975579ms",
              "kernTime": "3.237364675s"
            },
            "limit": "20",
            "sort_terms": [
              {
                "desc": true,
                "expr": "(`history`.`timestamp`)"
              }
            ],
            "#time_normal": "00:00.001",
            "#time_absolute": 0.001975579
          },
          {
            "#operator": "Limit",
            "#stats": {
              "#itemsIn": 20,
              "#itemsOut": 20,
              "#phaseSwitches": 41,
              "execTime": "13.827µs"
            },
            "expr": "20",
            "#time_normal": "00:00.000",
            "#time_absolute": 0.000013827
          },
          {
            "#operator": "FinalProject",
            "#stats": {
              "#itemsIn": 20,
              "#itemsOut": 20,
              "#phaseSwitches": 61,
              "execTime": "43.262µs",
              "kernTime": "21.896899ms"
            },
            "#time_normal": "00:00.000",
            "#time_absolute": 0.000043262
          }
        ],
        "#time_normal": "00:00.000",
        "#time_absolute": 0.000004017000000000001
      },
      "#time_normal": "00:00.001",
      "#time_absolute": 0.0010110249999999998
    },
    {
      "#operator": "Stream",
      "#stats": {
        "#itemsIn": 20,
        "#itemsOut": 20,
        "#phaseSwitches": 43,
        "execTime": "21.888316ms",
        "kernTime": "3.240525726s"
      },
      "#time_normal": "00:00.021",
      "#time_absolute": 0.021888315999999998
    }
  ],
  "~versions": [
    "6.5.0-N1QL",
    "6.5.1-6299-enterprise"
  ],
  "#time_normal": "00:00.000",
  "#time_absolute": 0.000003811
}