I have a smart contract which contains some article data stored inside a struct, which is mapped with an id which uniquely identifies it with a mapping, like this:
struct Foo {
string a;
string b;
}
mapping (uint64 => Foo) public articles;
(Obviously the above code is simplied, but the structure is the same, although the real struct has 7 variables instead of 2).
Now I have to be able to search using any of the 7 attributes. The blockchain is going to be private, so gas usage is not a concern.
My first solution was to add a search method to the smart contract and, within it, traverse the entire mapping and check which items satisfy the condition. Obviously this is O(n) and it was taking around 8 seconds for 2000 elements, which although isn't bad, I hope for a better solution.
Then I thought about having 7 more mappings which would act as search indexes, mapping each attribute value to the index of the article that satisfies it, such as:
mapping (string => uint64) public searchIndex;
(Note that right now it isn't required to check substrings, only equality)
And finally, I was thinking about if it would be good to do keep these indexes on Elastic Search rather than on the blockchain itself, although I would prefer a pure blockchain-based solution if possible.
So, what of these options (of any other better you know) do you think is more adequate?
Thank you.