Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sunspot Rails difference between :text an :string type fields

As everyone is aware in your searchable model you can define an index of type :text as well as :string, for example:

class Post < ActiveRecord::Base
  searchable do
   string :title
   text :title, :body    
end

I tried searching for the basic differences between a text field type and string field type and was able to get a basic understanding like:

  1. Text field types are tokenized , and this makes doing full text search within them very fast.
  2. You cannot use order_by i.e sorting on a text filed , and if you need sorting on that field you need to define it as :string.

So what i am looking for here is all the CONCEPTUAL as well as USAGE differences between a text field type and string field type so that i can weigh my opinions while defining a field as string or text or both.

Note: I am not saying that you provide all the differences in a single answer, one difference per answer will also do and but please make sure the difference you are giving is not given already.

like image 641
Sahil Dhankhar Avatar asked Aug 12 '13 11:08

Sahil Dhankhar


1 Answers

Text Fields

When text fields are indexed, they are broken up into their constituent words and then processed using a definable set of filters (with Sunspot’s default Solr installation, they’re just lower-cased). This process is known as tokenization, and it’s what allow text fields to be searched using fulltext matching. You can read more about tokenization and the available filter options on the Solr wiki http://wiki.apache.org/solr/AnalyzersTokenizersTokenFilters.

String Fields

String fields store string data. How is this different from text fields? A text field is tokenized, which is to say it’s broken up into its constituent words; that’s how fulltext search works. String fields, on the other hand, are just indexed as-is: the indexed data is exactly that string, from beginning to end.

Docs

https://github.com/sunspot/sunspot/wiki/Setting-up-classes-for-search-and-indexing

like image 142
rmagnum2002 Avatar answered Nov 13 '22 09:11

rmagnum2002