Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement search like Stack Overflow

I have implemented the full text search using Sphinx and Thinking Sphinx. I want to add column wise search. Some thing like:-(taking an example of Stack Overflow)

Suppose you want to see actvities related to you, just type:

  user:me

Then it will return a result with all the questions and answers related to piemesons.

If you type

 votes:15

then it will return a result with all the questions tagged with having more than 15 votes.

And if you type

  user:me votes:15

then it will return all the questions and answers belonging to you with more than 15 votes.

How can I implement this thing?

Right now my search results are based upon full text search. How can these kinds of features be included?

Any options avaliable in Sphinx or Solr or any other search engines?

like image 743
Mohit Jain Avatar asked Jun 30 '10 13:06

Mohit Jain


People also ask

What is a stack overflow search?

Stack Overflow is a question and answer website for professional and enthusiast programmers. It is the flagship site of the Stack Exchange Network. It was created in 2008 by Jeff Atwood and Joel Spolsky. It features questions and answers on a wide range of topics in computer programming.

How do I search users on stackoverflow?

If a user wants to search his questions, the user can use this query user: mine or user: me, or in case the user wants to search for another user's questions, he can use the query user: id and this will return all the questions that a specific user has asked.

Does stack overflow have an API?

Teams API: A First Request WalkthroughThe Stack Exchange API permits access to data within private Teams. Only Stack Overflow Business accounts have read and write access to the API.

What is stack overflow error?

A stack overflow is a type of buffer overflow error that occurs when a computer program tries to use more memory space in the call stack than has been allocated to that stack.


1 Answers

:with option in thinking sphinx.

First of all, you have to define those attributes in your index definition (check out attributes section here).

has views_count, :as => :views, :type => :integer
has user.id, :as => :user, :type => :integer

Then you could search for posts like this:

Post.search '', :with => {:views => 12..maxint, :user => User.first.id}

(I am not sure if there is any more elegant possibility of giving open ranges, but 12..max_int should be enough)

Two important things:

  1. if you want to count related objects (e.g. responses), you should use counter cache
  2. if your "user" is polymorphic association, I recommend "CRC32(CONCAT(user_type, user_id))" instead of user.id
like image 191
skalee Avatar answered Oct 05 '22 13:10

skalee