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.
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.
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?
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:
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.
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?
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.
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?
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.