Querying strings...
Amir Ansari
fractallyte at csi.com
Tue Nov 17 10:04:08 UTC 2009
If I have a library of books (say, class 'MyLibrary') which contains
books (in a MagmaCollection), each with its own title and tags
(strings), is it possible to query each of the tags in the string?
Here's the example code:
books := MagmaCollection new.
books addIndex: (MaSearchStringIndex attribute: #tags).
books add: (MyLibrary new
title: 'Le Morte d''Arthur';
tags: 'magic merlin camelot');
add: (MyLibrary new
title: 'The Hobbit';
tags: 'dragon ring magic').
To query the tags, I can use this code:
foundItem := books where: [:each | each tags includesAnyOf: 'magic'
subStrings.]
However, this only finds tags which have 'magic' and its subStrings at
the beginning of the 'tags' string. Ideally, I'd like to be able to
parse the tags string (as a subString?) so that I can query each
SEPARATE word.
For instance, the above query returns only the first item. I can
further refine the search:
foundItem first tags subStrings includesAnyOf: 'camelot' subStrings
...but this isn't a solution, because I need to be able to do this in
the actual query!
It would work if I used separate tags so that each contains only one
word, such as tagOne, tagTwo, etc. But the advantage of the above is
that I can add an arbitrary number of tags to any title, so it would be
ideal to be able to keep the schema as it is.
So is there a way to parse subStrings in the 'where:' method? Am I
overlooking something? Is there a way around this? Thanks for any
help!
Amir
More information about the Magma
mailing list