mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 11:23:25 +08:00
Include the search_log_id
in search results
This commit is contained in:
parent
d0c5205a52
commit
21e02d6969
|
@ -12,6 +12,9 @@ class SearchLog < ActiveRecord::Base
|
|||
|
||||
def self.log(term:, search_type:, ip_address:, user_id:nil)
|
||||
|
||||
search_type = search_types[search_type]
|
||||
return [:error] unless search_type.present? && ip_address.present?
|
||||
|
||||
update_sql = <<~SQL
|
||||
UPDATE search_logs
|
||||
SET term = :term,
|
||||
|
@ -35,7 +38,7 @@ class SearchLog < ActiveRecord::Base
|
|||
if rows.cmd_tuples == 0
|
||||
result = create(
|
||||
term: term,
|
||||
search_type: search_types[search_type],
|
||||
search_type: search_type,
|
||||
ip_address: ip_address,
|
||||
user_id: user_id
|
||||
)
|
||||
|
|
|
@ -2,5 +2,10 @@ class GroupedSearchResultSerializer < ApplicationSerializer
|
|||
has_many :posts, serializer: SearchPostSerializer
|
||||
has_many :users, serializer: SearchResultUserSerializer
|
||||
has_many :categories, serializer: BasicCategorySerializer
|
||||
attributes :more_posts, :more_users, :more_categories, :term
|
||||
attributes :more_posts, :more_users, :more_categories, :term, :search_log_id
|
||||
|
||||
def search_log_id
|
||||
object.search_log_id
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -182,7 +182,13 @@ class Search
|
|||
@limit = Search.per_filter
|
||||
end
|
||||
|
||||
@results = GroupedSearchResults.new(@opts[:type_filter], clean_term, @search_context, @include_blurbs, @blurb_length)
|
||||
@results = GroupedSearchResults.new(
|
||||
@opts[:type_filter],
|
||||
clean_term,
|
||||
@search_context,
|
||||
@include_blurbs,
|
||||
@blurb_length
|
||||
)
|
||||
end
|
||||
|
||||
def valid?
|
||||
|
@ -197,12 +203,13 @@ class Search
|
|||
def execute
|
||||
|
||||
if SiteSetting.log_search_queries?
|
||||
SearchLog.log(
|
||||
status, search_log_id = SearchLog.log(
|
||||
term: @term,
|
||||
search_type: @opts[:search_type],
|
||||
ip_address: @opts[:ip_address],
|
||||
user_id: @opts[:user_id]
|
||||
)
|
||||
@results.search_log_id = search_log_id unless status == :error
|
||||
end
|
||||
|
||||
unless @filters.present?
|
||||
|
|
|
@ -9,10 +9,20 @@ class Search
|
|||
extend ActionView::Helpers::TextHelper
|
||||
end
|
||||
|
||||
attr_reader :type_filter,
|
||||
:posts, :categories, :users,
|
||||
:more_posts, :more_categories, :more_users,
|
||||
:term, :search_context, :include_blurbs
|
||||
attr_reader(
|
||||
:type_filter,
|
||||
:posts,
|
||||
:categories,
|
||||
:users,
|
||||
:more_posts,
|
||||
:more_categories,
|
||||
:more_users,
|
||||
:term,
|
||||
:search_context,
|
||||
:include_blurbs
|
||||
)
|
||||
|
||||
attr_accessor :search_log_id
|
||||
|
||||
def initialize(type_filter, term, search_context, include_blurbs, blurb_length)
|
||||
@type_filter = type_filter
|
||||
|
|
|
@ -790,4 +790,16 @@ describe Search do
|
|||
end
|
||||
end
|
||||
|
||||
context "search_log_id" do
|
||||
it "returns an id when the search succeeds" do
|
||||
s = Search.new(
|
||||
'indiana jones',
|
||||
search_type: :header,
|
||||
ip_address: '127.0.0.1'
|
||||
)
|
||||
results = s.execute
|
||||
expect(results.search_log_id).to be_present
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -57,8 +57,17 @@ describe SearchController do
|
|||
it "logs the search term" do
|
||||
SiteSetting.log_search_queries = true
|
||||
xhr :get, :query, term: 'wookie'
|
||||
|
||||
expect(response).to be_success
|
||||
expect(SearchLog.where(term: 'wookie')).to be_present
|
||||
|
||||
json = JSON.parse(response.body)
|
||||
search_log_id = json['grouped_search_result']['search_log_id']
|
||||
expect(search_log_id).to be_present
|
||||
|
||||
log = SearchLog.where(id: search_log_id).first
|
||||
expect(log).to be_present
|
||||
expect(log.term).to eq('wookie')
|
||||
end
|
||||
|
||||
it "doesn't log when disabled" do
|
||||
|
|
|
@ -4,6 +4,26 @@ RSpec.describe SearchLog, type: :model do
|
|||
|
||||
describe ".log" do
|
||||
|
||||
context "invalid arguments" do
|
||||
it "no search type returns error" do
|
||||
status, _ = SearchLog.log(
|
||||
term: 'bounty hunter',
|
||||
search_type: :missing,
|
||||
ip_address: '127.0.0.1'
|
||||
)
|
||||
expect(status).to eq(:error)
|
||||
end
|
||||
|
||||
it "no IP returns error" do
|
||||
status, _ = SearchLog.log(
|
||||
term: 'bounty hunter',
|
||||
search_type: :header,
|
||||
ip_address: nil
|
||||
)
|
||||
expect(status).to eq(:error)
|
||||
end
|
||||
end
|
||||
|
||||
context "when anonymous" do
|
||||
it "logs and updates the search" do
|
||||
Timecop.freeze do
|
||||
|
|
Loading…
Reference in New Issue
Block a user