Understand more about full-text search for Rails developer

If you’re a Rails developer, then you might have came across to use `pg_search` gem. It’s allow you to do full-text search in a Rails app very easily. All you need to do is to add the gem to your Gemfile and do the bundle install then include the module into a Rails model that you want to search against.

After all that you tested out the search functionality and all worked very nice then you decided to improve the performance by adding full-text search functionality. For example you have the following code

pg_search_scope :search_by_name, against: :name, using: {
  tsearch: {
    tsvector_column: "tsvector_name_tsearch",
    dictionary: "english",
    prefix: true
  }
}

Now you’re happy with the performance boost. But you’re also realize that the search doesn’t return all result as before. For example: you have an article with the following name `Business landing page` and the search `landi` or `landin` doesn’t return any result at all but `landing` return the correct result. So why is that ?

If you’re notice a little bit, you can see that in the code above we’re using dictionary is english as the way to do the index. It means that when you have the following content `Business landing page` then here is the list of tokens that Postgresql will do the index


"Business landing page" =>
  ["business",
   "businesses",
   "land",
   "lands",
   "landed"
   "landing",
   "page",
   "paged",
   "pages",
   "paging"
  ]

`landi` or `landin` are not the correct English words and they never got indexed so that why they never got return.

More  about dictionaries

  • Simple dictionary: The simple dictionary template operates by converting the input token to lower case and checking it against a file of stop words. If it is found in the file then an empty array is returned, causing the token to be discarded. If not, the lower-cased form of the word is returned as the normalized lexeme.
  • Synonym dictionary: This dictionary template is used to create dictionaries that replace a word with a synonym.
  • Thesaurus dictionary: A thesaurus dictionary is a collection of words that includes information about the relationships of words and phrases
  • Ispell dictionary: The Ispell dictionary template supports morphological dictionaries, which can normalize many different linguistic forms of a word into the same lexeme. For example, an English Ispell dictionary can match all declensions and conjugations of the search term bank, e.g., banking, banked,banks, banks', and bank's.

 

#References

Understand more about full-text search for Rails developer

Replace S3 with DigitalOcean as asset server for Spree commerce

In this post I will show steps need to setup DigitalOcean as asset server for Spree projects. So before going into any detail, I want to explain a bit the reason I did it. S3 was great for me. It’s simple to setup and use. Until one day my bill for Amazon S3 go up to ~7k USD/month. I was freak out. And the reason was, one of our dev guys have use aws key for testing purpose and he was not careful enough. And the rest of the story you already know what was going on. Then I decided to setup the asset server on DigitalOcean with only 5$/month and I got 20Gb of SSD storage. Which I think pretty good.

Ok. So here is what you need to make it happen.

First, add the following gem to your Gemfile `paperclip-storage-ftp` at https://github.com/xing/paperclip-storage-ftp. Second, you gonna need to do the config for Spree. You can add a file called `spree_ftp.rb` under `initializers` folder

attachment_config = {
  storage: :ftp,
  path: "/home/your_project_path/files/:attachment/:id/:style/:filename",
  url: "http://X.X.X.X/:attachment/:id/:style/:filename",
  styles: {
    mini: "48x48>",
    small: "100x100>",
    product: "240x240>",
    large: "600x600>"
  },
  default_style: "product",
    ftp_servers: [
      {
        host: "X.X.X.X",
        user: "your-ftp-user-name",
        password: "your-ftp-password"
      }
    ],
  ftp_connect_timeout: 5,
  ftp_ignore_failing_connections: true,
  ftp_keep_empty_directories: true
}

attachment_config.each do |key, value|
  Spree::Image.attachment_definitions[:attachment][key.to_sym] = value
end

That is all for Spree part. Now for DigitalOcean, you gonna need 2 things.

  • 1 is a nginx server – to serve the static content only (png, jpg,..)
  • 2 ftp server – for your Spree project upload asset to

Setup an nginx server on DigitalOcean quite easy and straight forward. One thing to keep in mind that you need to config Nginx to serve only static content (nothing else) and only authorize public users to access to the asset folder which you defined in the Spree config above.

For FTP server setup, you can follow great tutorial here https://www.digitalocean.com/community/tutorials/how-to-set-up-vsftpd-on-ubuntu-12-04 – This can be easily setup within 15 – 30 minutes. Then one last thing you need to do the config for FTP server also. By edit the following file `/etc/vsftpd.conf`. Here is my config as an example

Screen Shot 2016-03-15 at 9.15.46 PM

That’s all. Now I can upload all of my assets into DigitalOcean. The result was pretty great for me. Nginx handle static images very fast. I happy with it and now I gonna move all images from S3 to DigitalOcean.

Replace S3 with DigitalOcean as asset server for Spree commerce

Great text editor for Golang

You might think this is another blog post about Vim, Emacs, Atom, or Sublime. No, it’s not. All of those editor are super great. I have used all of them, and still using Atom for all of my Rails projects, and using Vim for all Linux related tasks. I’m happy with them. However when it come to Golang, I kind of feel not comfortable very much with those tools. I don’t need all of those configs for other stuff from Atom or Vim. Then I dis some research and found this amazing little small editor but still powerful, and it’s Textadept. http://foicica.com/textadept

Textadept is an text editor written in about 200o lines of Lua. And it’s super fast!!!

Here is a quick screenshot of how it look with my Golang code:

Screen Shot 2016-03-14 at 11.17.27 PM

Here is the reasons why I love it so much

  1. It’s very fast
  2. It support Mac/Linux/Window
  3. It’s light weight
  4. It got all handy shortcuts from other editors (Atom/Sublime) which still allow me coding very fast. Search, multi-cursor edit, moving cursors around, …
  5. It support Golang out of the box. I don’t need to do any config at all and it just work. Stuff like syntax highlight, go format … (similar to go-plus for Atom). And it’s even got very handy shortcut like: `command+R` -> then it gonna build and run the go program for me. So I don’t have to go to terminal and type `go run main.go` anymore.      Screen Shot 2016-03-14 at 11.29.49 PM
  6. It’s configurable. Like Vim got .vimrc – Textadept got it’s init file also where you can do all kind of customization that fit your need. Stuff like custom functions, themes…

Textadept is exact text editor I want. It’s small, simple, but still powerful. I highly recommend you give it a try.

Great text editor for Golang