I have a documents in collections like:
{
_id:"....",
resourceId: "375749d7-a828-4a7b-acc0-77539fbcb7b4" // string type
name: "bla random word"
}
with index:
{
"mappings": {
"dynamic": false,
"fields": {
"name": {
"type": "autocomplete"
},
"resourceId": {
"type": "string"
}
}
}
}
I have a golang api to find it
I want to search a term that contains exact in name OR in resourceId
for example if I find a partial name like “bla” returns documents with this name, if I find some words contains all of the words like “bla random”
In another hand, if I find by an id like “375749d7-a828-4a7b-acc0-77539fbcb7b4” or partially “375749d7” returns the documents that resourceId contains this term
I’m not sure that index is ok and I try to use this code
mustClauses := bson.A{
bson.D{
{Key: "$or", Value: bson.A{
bson.D{
{Key: "autocomplete", Value: bson.D{
{Key: "path", Value: "name"},
{Key: "query", Value: searchTerm},
}},
},
bson.D{
{Key: "regex", Value: bson.D{
{Key: "path", Value: "resourceId"},
{Key: "query", Value: searchTerm},
{Key: "allowAnalyzedField", Value: true},
}},
},
}},
},
}
search := bson.D{
{
Key: "$search", Value: bson.M{
"index": index,
"compound": bson.M{
"must": mustClauses,
},
},
},
}
but must cannot contains $or, should contains autocomple, regex, etc
In another hand I try to use should but returns inconsistent documents (if you search “bla random” return documents that only contains “bla”, etc)
Any knows how to implement this?