Search via API results


(Paul Apostolos) #1

We are using the search API to allow our main site users to also search the Discourse forum.

I dug through the code for search and I can’t see any way to return a snippet, blurb, section (or whatever is the best term) of the topic to include in our search results page.

Also, is the ordering of the search results based on relative importance with respect to the search term?


(Bill Ayakatubby) #2

There isn’t one. Note how even Discourse’s search only returns titles.


(Paul Apostolos) #3

Barring that…Is there a way to pass a list of topics (not individual ids but multiple ids) to the api and return json of those topics? Then I could just parse them on my end.

I see in the topics controller there is a method called bulk that accepts a parameter called topic_ids. That looks promising, but I can’t figure out how to call it.


(Régis Hanol) #4

I’m afraid that method won’t help you. It’s used to apply bulk operations on topics (ie. change category/close/change notification level/reset read)

I don’t think so. PR welcome though :wink:


(Roberto_Pezzali) #5

I’m using the search with API too…

The problem is that the search return only a result if there is an exact match.

Discourse.client.search “Samsung Galaxy Tab” return 0 results if I have a topic like:

“Samsung Galaxy is good?”

@zogstrip is there any ‘magic’ to have the same results of the forum search?


(Régis Hanol) #6

I’m not sure I understand. When you’re using the discourse_api, you’re actually talking to the same API that Discourse is using. So results should be the same.


(Roberto_Pezzali) #7

Mmm, maybe the problem is in the Discourse search. I don’t know if is related to italian and Postgres but I have an issue.

I have a product database and I’m trying to attach to my product page the related posts.
Every query return me 0 results and so I try to investigate.

This is my query string: “Samsung Galaxy Tab 2 10.1

In Discourse I have three topics:

"Ora proviamo con Galaxy Tab 2 10.1"
Samsung Galaxy Tab 2 quale prendere?
Galaxy Tab, parliamone

Now, API (and Discourse too) return me no results!!!

With query string: “Galaxy Tab 2 10.1” it return me only the first topic!

You need the exact match to return a results.
Is this correct?


(Régis Hanol) #8

Yes it is.

If you look at the search.rb file, you will find this line:

query = Post.sanitize(all_terms.map {|t| "#{PG::Connection.escape_string(t)}:*"}.join(" & "))

Which join all the search terms using the & (AND) boolean operator.


(Roberto_Pezzali) #9

Mmh, maybe the search must be improved…

I will try to reduce the string I pass to the query


(Paul Apostolos) #10

I did it. I got my hands dirty and made the exact modification I needed.

This was my first foray into ruby, so I followed the instructions here Discourse as Your First Rails App and it worked.

Now, hopefully, my first PR ever will be accepted (crosses fingers).


(Aaron Boushley) #11

@paully21 Did you end up doing this as a change to discourse directly? Or did you create a plugin that did what you need?


(Paul Apostolos) #12

I did it and the change was committed to Discourse.

You’ll need to add include_blurbs=true to the search api call.

Here is a Gist of a method in C# showing how to do the search via the API.


(Aaron Boushley) #13

@paully21 Glad to see this landed in the real project!

For people who find this thread in the future, you’re looking for the include_blurbs=true parameter. You can call the search API like this: /search.json?term=things&include_blurbs=true Where “things” is your search term.


(Daniela) #14