How to search for substrings in any of the fields in the document?

How to search for substrings in any of the fields in the document?
Example:

[
{ name: "first product", description: "first description"},
{name: "second", description: "some products"}
{name: "third", description: "some text"}
]

Search string: product
It should match below documents:

{ name: "first product", description: "first description"},
{name: "second", description: "some products"}

Hi @Divya_N_A1 - Welcome to the community :slight_smile:

Please see the below $search examples based on the sample documents and search term you have provided.

$search and it’s output with use of compound and text:

{
  $search: {
     "index": "default",
     "compound": {
       "should": [
       {"text": {"query": "product", "path": "name", "fuzzy": {"maxEdits": 1}}},
       {"text": {"query": "product", "path": "description", "fuzzy": {"maxEdits": 1}}}
       ]
     }
  }
}
/// Output:
[
  {
    _id: ObjectId("6552dc8181ae04007d21a862"),
    name: 'second',
    description: 'some products'
  },
  {
    _id: ObjectId("6552dc8181ae04007d21a861"),
    name: 'first product',
    description: 'first description'
  }
]

$search and it’s output with queryString:

{
  "$search": {
     "index": "default",
     "queryString": {
        "defaultPath": "name",
        "query": "name:product~1 OR description:product~1" 
     }
  }
}
/// Output:
[
  {
    _id: ObjectId("6552dc8181ae04007d21a862"),
    name: 'second',
    description: 'some products'
  },
  {
    _id: ObjectId("6552dc8181ae04007d21a861"),
    name: 'first product',
    description: 'first description'
  }
]

query string is "product" in both cases.

I have only tested this on my test environment with the 3 sample documents you provided using the search term "product" - If you have cases where these types of queries do not return the expected documents, please advise.

It’s recommended you do any testing of the above examples on a test environment first to ensure it meets your use case and requirements before trying on production.

Regards,
Jason

I am using a search index how to do it with a search index
This is my query
[
{
$search: {
index: “default”,
text: {
query: “pro”,
path: {
wildcard: “*”
}
}
}
}
]

but , it is not working . I tried it.
this is my query.

 const files = await FileModel.aggregate([
                    {
                        $search: {
                            index: "default", // Use the name of the text index
                            compound: {
                                should: [
                                    {
                                        text: {
                                            query: search,
                                            path: {
                                                wildcard: '*'
                                            }
                                        },
                                    }
                                ]
                            },
                        }
                    }
                ]);
            return files;

The search term in this case appears to be "pro" in which you may wish to try using the autocomplete operator. However, can you please provide the index definition in use here?

With the following index definition:

{
  "mappings": {
    "dynamic": true,
    "fields": {
      "description": {
        "type": "autocomplete"
      },
      "name": {
        "type": "autocomplete"
      }
    }
  }
}

And the following query:

[
  {
    '$search': {
      index: 'default',
      compound: {
        should: [
          { autocomplete: { query: 'pro', path: 'description' } },
          { autocomplete: { query: 'pro', path: 'name' } }
        ]
      }
    }
  }
]

I get the below documents returned:

[
  {
    _id: ObjectId("6552dc8181ae04007d21a862"),
    name: 'second',
    description: 'some products'
  },
  {
    _id: ObjectId("6552dc8181ae04007d21a861"),
    name: 'first product',
    description: 'first description'
  }
]

@N171155_GUNTURI_BHAVYASRI

Are you working with @Divya_N_A1 on the same cluster? If not, are you able to create a new topic describing your particular issue?

If so – Plesae provide the index definition in use including index name and the search term you’ve used. I can only see a variable called search but do not know the value of it.

Regards,
Jason

yeah , we are working on the same cluster.
index definition :

{
  "mappings": {
    "dynamic": true
  }
}

and I am using standard analyzer.

but i have many fields . can we do it for all the fields?

I have a table with dynamic document fields, and I want to implement a global search that retrieves documents based on the values in any of the fields. The challenge I’m facing is that when using default search indexes with the path set as {wildcard: "*"}, it seems to only retrieve documents with exact word matches, not considering character matches.

In the provided answer, the suggestion is to specify the field name and description, but my table has dynamic fields, and I don’t have a predefined list of field names. I want the search to encompass all fields in the document without explicitly specifying them. How can I achieve a global search that considers all fields for character matches in a dynamic document structure?

Understood @Divya_N_A1 - It would be important to include these details in the original post as the solution I had provided was based off the search term and example documents you provided with all sample documents having the same 2 fields, "name" and "description".

In saying so, you can possibly consider the examples on the How to Run Partial Match Atlas Search Queries documentation to see if those example queries suit your use case and requirements. However, since you’ve also noted you plan on using a {"wildcard": "*"} path for all your fields, you should consider a performance impact with these types of searches. There is also a very similar post here which I believe somewhat matches the “contains” notion across all fields.

The Tune Atlas Search Performance pages may be of use to go over as well.