# Effectively logging search queries

**URL:** <https://meta.discourse.org/t/effectively-logging-search-queries/63838>\
**Category:** Feature\
**Created:** [Junho 2, 2017, 8:29pm UTC](https://meta.discourse.org/t/effectively-logging-search-queries/63838 "2017-06-02T20:29:35Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)\
**Post date:** [Junho 2, 2017, 8:29pm UTC](https://meta.discourse.org/t/effectively-logging-search-queries/63838/1 "2017-06-02T20:29:36Z")

</div>

For 1.9 we plan to add a search log.

Unfortunately, the “log every search the server makes” approach is incorrect as we perform searches as people are typing which will result in a massively noisy log.

### Proposal

- Create a new table

term, user\_id (nullable), ip\_address, created\_at, clicked\_topic\_id (nullable), source\_id (either header or fullpage)

- Log on server with the following algorithm on search

```plaintext
UPDATE term
SET term = :new_term
        created_at: :now
WHERE created_at < 5.seconds.ago AND
             position(term in :new_term) = 0 AND
             (user_id = :user_id OR ip_address = :ip_address)

term: new_term,
now: Time.zone.no,
ip_address: request.ip 

If update touches zero rows, then insert a **new** search log row

```

Or, in English

- Update existing search log row IF:

- On click on search result (in either full page search or header) update the `clicked_topic_id`, (have search results return log id, then update it based on log id + user match + in last 10 minutes)

### Limiting log size

So the log does not grow forever there should be a site setting for maximum rows to store. Default should be about a million.

A weekly job can delete oldest rows or something.

Thoughts? Feedback?

---

_[View the full topic](https://meta.discourse.org/t/effectively-logging-search-queries/63838)._
