Keval
Keval

Reputation: 1859

query document which has particular word at last index of that field

I have a field with TextField data type

I want to search if it has particular word at last index of that field,

For example.

my field is title with value Elligator Red Lace Men's Running Sports Shoes

Now I want to search for documents with which has Shoes as last word in the title.

Upvotes: 0

Views: 173

Answers (2)

Alexandre Rafalovitch
Alexandre Rafalovitch

Reputation: 9789

You can use RegEx CharFilter to basically add a boundary marker at the end (so, replace $ with [[END]]). $ here is a regular expression marker for the end of line, just to be clear.

Then, after tokenization, you replace any token that does not have that marker with empty string, using RegEx TokenFilter (Token, not Char now). Then, you have another one of those to just strip off your boundary marker. You end up with a bunch of empty tokens (which I believe are ignored) and your single last-word token. I'd recommend doing that in a copyField and use that as a boost.

You do need to be careful that your input stream actually has your last token right at the end before you add the boundary marker to it and that your boundary marker text is not something that tokenizer will break at. So, you may need to preprocess your string with another Char Filter or even on a client.

Upvotes: 1

bigboy
bigboy

Reputation: 96

Have you tried search by SOLR regex ? (supported by SOLR 4.0+.)

q=title:/.*Shoes/

For this, title field must by of type StringField, so it is not tokenized.

Upvotes: 1

Related Questions