Get OR based results of $search and $match

Hello, I want to get combined result from a collection by querying two fields. One field uses search based indexing for its querying process while the other field can be normally find using match or find operation.
But I want to get OR results of both the field searches. How can I perform this?
Pasting a sample code for your reference.

 GDSchema.aggregate([
                {
                    $search: {
                        index: 'index_company_name',
                        text: {
                            path: 'CompanyName',
                            query: req.query.s,
                            fuzzy: {
                                maxEdits: 2,
                                maxExpansions: 20,
                            },
                        },
                    },
                },
                {
                    $match: { phoneticCode: 'B234' },
                },
                },
            ]).

Hey Ashutosh,

Can you provide sample documents and expected output? I think based off your description, the compound operator may help noting that the should option maps to the OR boolean operator.

Regards,
Jason

GSchema.aggregate([
                {
                    $search: {
                        index: 'index_company_name',
                        text: {
                            path: 'CompanyName',
                            query: req.query.s,
                            fuzzy: {
                                maxEdits: 2,
                                maxExpansions: 20,
                            },
                        },
                    },
                },
                {
                    $match: { phoneticCode: 'B234' },
                },
                },
            ]).

You have sent your $search aggregation again. As per my previous post, are you able to send sample documents you’re using this against and your expected output?

Regards,
Jason

{
  "_id": {
    "$oid": "61e0517cd9c4b533cc0bcf41"
  },
  "CompanyName": "BigML",
  "phoneticCode": "B254"
}

This is the sample document containing two fields which are companyName and its Soundex algorithm based string in the field “phoneticCode”.
Basically I want to implement OR based results of Fuzzy search (on companyName) and PhoneticSearch(on Phonetic code), because I want to get combined search results of query which has similar spelling as well as similar sound. How can I achive this use case with my use case? Is there any other way to do this?

Thanks for that example. I’m not sure of the expected output based off a single sample document but i’ve created the following sample docs in hopes that the compound operator suits your use case:

DB> db.companyphone.find({},{_id:0})
[
  { CompanyName: 'BigML', phoneticCode: 'B254' },
  { CompanyName: 'nothing', phoneticCode: 'B254' },
  { CompanyName: 'BigML', phoneticCode: 'nothing' }
]

I have the following index definition:

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

The following $search stage was used in my test environment:

{
  '$search': {
    index: 'default',
    compound: {
      should: [
        { text: { query: 'BigML', path: 'CompanyName' } },
        { text: { query: 'B254', path: 'phoneticCode' } }
      ]
    }
  }
}

which resulted in the following documents (I also performed a $project for the search scores for your information):

[
  {
    CompanyName: 'BigML',
    phoneticCode: 'B254',
    score: 0.42727601528167725
  },
  {
    CompanyName: 'nothing',
    phoneticCode: 'B254',
    score: 0.21363800764083862
  },
  {
    CompanyName: 'BigML',
    phoneticCode: 'nothing',
    score: 0.21363800764083862
  }
]

If you believe the compound operator will suit your use case then please alter and test thoroughly. The above example was for demonstration based off 3 sample documents on my test environment.

Hope this helps.

Regards,
Jason

Thanks Jason, This works.
Only doubt I have now is about index name. You have written
index: ‘default’. But I have created index for the field companyName with the name
“index_company_name”, and there is no indexing for second field i.e. “phoneticCode”. Should I replace “default” index with “index_company_name”. But about the other field with no index?

Thanks

1 Like

Glad to hear it works.

The index name I used was just the standard default value. That index name that is specified in my example just indicates that the default index should be used which had the following definition:

/// this is what the "default" index definition is for my test environment
{
  "mappings": {
    "dynamic": true
  }
}

You can define whichever field mappings suit your use case. Again, the above index definition for the "default" was just an example.

Regards,
Jason

I have created indexes for both the fields.
For ,“companyName” field, index is “index_company_name”, and for “phoneticCode” field, index is “index_phonetic_code”.

Can you please help me by updating your above code for both the indexes?
If it is done by index mapping, then please provide the sample code for that too and where to define that mapping?

Thanks

I would refer you to go over the Static and Dynamic mappings documentation. However I think this is beyond the scope of the current topic which was already answered in my previous comment. If you’d like to know more about Static & Dynamic mapping with regard to your use case, I believe it’s best to open a new topic for that question.

Regards,
Jason

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