0

I'd like to store in SOLR some items with addresses (City, State, ...) and I'd like to change how similarity is computed. The thing is that when comparing for example city I'm only interested if they are same and not if those strings are similar. Is there a way how to that? Is it through the custom similarity? If so, can somebody please point me to how it can be done in Solr 6.2? Thank you very much.

1 Answers1

0

If you're only interested if something matches exactly, use a StrField (a StrField is case sensitive, so the case has to match as well). As you're only getting exact matches, the scoring will be the same for all documents.

The only time you need to implement a custom similarity class is if you want to score documents in a different way than what the built in similarities (or function queries) allow.

Matching exactly would be a regular query: city:Frankfurt. As long as the field is a StrField, only documents with exactly Frankfurt in that field will be returned (and unless you've added an index time boost for one of them, they'll all score identical).

Also, if you're sorting by a field (such as city), any score calculation will be thrown out.

MatsLindh
  • 49,529
  • 4
  • 53
  • 84
  • ah, ok. Thank you very much. StrField looks to be the way. What about state a country? If I have some number values for state (state = 100) and country (country = 50), it also does not mean that state 100 is similar to 101. – Peter Krejzl Sep 13 '16 at 19:05
  • That's the same. If you want, you can index those values as an integer field, as that will be useful if you're going to sort by the field (so that 10 sorts after 2 - if you're not going to sort, a StrField will still work). Solr doesn't really do "similar" that way (unless you ask for fuzzy search), as there has to be deterministic processing involved to convert a value into something different. – MatsLindh Sep 13 '16 at 19:25
  • And if you need case insensitive matches, you can use a [TextField with a KeywordTokenizer and a LowercaseFilter](https://stackoverflow.com/questions/2053214/how-to-create-a-case-insensitive-copy-of-a-string-field-in-solr). – MatsLindh Sep 13 '16 at 19:50