# Zero search results searching private messages for specific keyword

**URL:** https://meta.discourse.org/t/zero-search-results-searching-private-messages-for-specific-keyword/48429
**Category:** Bug
**Created:** [10 augustus 2016 om 03:24 UTC](https://meta.discourse.org/t/zero-search-results-searching-private-messages-for-specific-keyword/48429 "2016-08-10T03:24:41Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![DeanMarkTaylor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/deanmarktaylor/32/102462_2.png) [@DeanMarkTaylor](https://meta.discourse.org/u/DeanMarkTaylor)
#### Post date: [10 augustus 2016 om 03:24 UTC](https://meta.discourse.org/t/zero-search-results-searching-private-messages-for-specific-keyword/48429/1 "2016-08-10T03:24:41Z")

</div>

As an admin attempting to search private messages for a discussion on “natwest”…

Using the search `in:private natwest` returns zero results.

Running the following query returns 9 results:

```sql
SELECT COUNT(*) FROM posts p
LEFT JOIN topics t on t.id = p.topic_id
WHERE t.archetype = 'private_message'
AND p.raw ILIKE '% natwest %'

```

Checking the actual posts they do contain text like “… its a natwest account …” and “… I pick up the guarantee on the Natwest overdraft …”

When using the search `in:private car` I see results as expected.

Is there a reason why I might not be seeing search results for the “natwest” keyword?

Currently running `Discourse 1.7.0.beta1 - 823a699d41974affe42adbd562cc9fbfd5a56ef1`.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [10 augustus 2016 om 03:41 UTC](https://meta.discourse.org/t/zero-search-results-searching-private-messages-for-specific-keyword/48429/2 "2016-08-10T03:41:02Z")

</div>

A question for @sam, what is the repro? That specific word? Permissions weird?

---

<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: [10 augustus 2016 om 03:46 UTC](https://meta.discourse.org/t/zero-search-results-searching-private-messages-for-specific-keyword/48429/3 "2016-08-10T03:46:58Z")

</div>

Have a look at the query that is running via mini profiler

---

<div class="post-metadata">

### Author: ![DeanMarkTaylor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/deanmarktaylor/32/102462_2.png) [@DeanMarkTaylor](https://meta.discourse.org/u/DeanMarkTaylor)
#### Post date: [10 augustus 2016 om 03:51 UTC](https://meta.discourse.org/t/zero-search-results-searching-private-messages-for-specific-keyword/48429/4 "2016-08-10T03:51:41Z")

</div>

Thanks for the pointer I’ll take a look in mini profiler tomorrow and get some more detail

---

<div class="post-metadata">

### Author: ![DeanMarkTaylor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/deanmarktaylor/32/102462_2.png) [@DeanMarkTaylor](https://meta.discourse.org/u/DeanMarkTaylor)
#### Post date: [10 augustus 2016 om 18:45 UTC](https://meta.discourse.org/t/zero-search-results-searching-private-messages-for-specific-keyword/48429/5 "2016-08-10T18:45:25Z")

</div>

The actual query for “natwest” shown in mini profiler is as follows:

```SQL
SELECT "posts".* FROM "posts" JOIN (SELECT *, row_number() over() row_number FROM (SELECT topics.id, min(post_number) post_number FROM "posts" INNER JOIN "post_search_data" ON "post_search_data"."post_id" = "posts"."id" INNER JOIN "topics" ON "topics"."id" = "posts"."topic_id" AND ("topics"."deleted_at" IS NULL) LEFT JOIN categories ON categories.id = topics.category_id WHERE ("posts"."deleted_at" IS NULL) AND "posts"."post_type" IN (1, 2, 3, 4) AND "topics"."deleted_at" IS NULL AND (topics.visible) AND (topics.archetype = 'private_message') AND (post_search_data.search_data @@ TO_TSQUERY('english', '''natwest'':*')) AND (topics.id IN (SELECT topic_id
                                               FROM topic_allowed_users
                                               WHERE user_id = 1
                                               UNION ALL
                                               SELECT tg.topic_id
                                               FROM topic_allowed_groups tg
                                               JOIN group_users gu ON gu.user_id = 1 AND
                                                                        gu.group_id = tg.group_id)) AND ((categories.id IS NULL) OR (NOT categories.read_restricted) OR (categories.id IN (2,4,35,38,40,42,46,47,48,49,52,56,57,58,59,60,61,62,63,64,65,66,67,68,72,73,75,79,80,81,82,83,84,85,86,87,89,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,113,114,115,116,118,119))) GROUP BY topics.id ORDER BY TS_RANK_CD(TO_TSVECTOR('english', topics.title), TO_TSQUERY('english', '''natwest'':*')) DESC, SUM(TS_RANK_CD(post_search_data.search_data, TO_TSQUERY('english', '''natwest'':*'))) DESC, topics.bumped_at DESC LIMIT 50) xxx) x ON x.id = posts.topic_id AND x.post_number = posts.post_number WHERE ("posts"."deleted_at" IS NULL) ORDER BY row_number   

```

Completing a side-by-side comparison the only differences between “car” and “natwest” queries only that keyword changes.

Editing the query further removing the `ORDER BY` statement (confirming it still returns no results)…

… and then removing `AND (post_search_data.search_data @@ TO_TSQUERY('english', '''natwest'':*'))` then returns results.

A general search of public content (i.e. not limiting via `in:private`) for “natwest” returns results.

Is this helpful to you?

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [12 augustus 2016 om 22:50 UTC](https://meta.discourse.org/t/zero-search-results-searching-private-messages-for-specific-keyword/48429/6 "2016-08-12T22:50:37Z")

</div>

> [@DeanMarkTaylor](#):
>
> ) OR (categories.id IN (2,4,35,38,40,42

Somehow I don’t think that should be in there if we’re searching private mesages.

---

<div class="post-metadata">

### Author: ![DeanMarkTaylor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/deanmarktaylor/32/102462_2.png) [@DeanMarkTaylor](https://meta.discourse.org/u/DeanMarkTaylor)
#### Post date: [12 augustus 2016 om 22:54 UTC](https://meta.discourse.org/t/zero-search-results-searching-private-messages-for-specific-keyword/48429/7 "2016-08-12T22:54:48Z")

</div>

Although this isn’t the problem as removing the entire `AND` statement makes no difference to getting the query to return results:

```SQL
AND (
				(categories.id IS NULL)
				OR (NOT categories.read_restricted)
				OR (
					categories.id IN (
						2
						,4
						,35
						,38
						,40
						,42
                                                ...
						)
					)
				)

```

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [8 april 2017 om 05:10 UTC](https://meta.discourse.org/t/zero-search-results-searching-private-messages-for-specific-keyword/48429/8 "2017-04-08T05:10:54Z")

</div>

Are you still seeing this on latest?

---

<div class="post-metadata">

### Author: ![DeanMarkTaylor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/deanmarktaylor/32/102462_2.png) [@DeanMarkTaylor](https://meta.discourse.org/u/DeanMarkTaylor)
#### Post date: [18 april 2017 om 18:01 UTC](https://meta.discourse.org/t/zero-search-results-searching-private-messages-for-specific-keyword/48429/9 "2017-04-18T18:01:56Z")

</div>

TLDR: You can close this bug report.

(thanks for your support)

* * *

## Search UI Improved

In the time since the original report the definition of “`in:private`” has changed (became more clear)…

… the new “Advanced Search” UI for search makes it clear to the user that the “`in:private`” search parameter will only search “my messages” opposed to “private messages” which previously didn’t indicate the search was limited to only my private messages.

## Original Problem

At the time of this original report I was attempting to follow up on tracking an abusive person communicating privately under with multiple usernames - so it was import to use search to search across all private messages for many keywords to track this users activity.

## Temporary Solution

I ended up creating custom queries via the [Data Explorer](https://meta.discourse.org/t/32566?silent=true) plugin to solve my problem.

> [@Discourse Data Explorer](https://meta.discourse.org/t/data-explorer-plugin/32566):
>
> discourse2Summary Discourse [Data Explorer](https://meta.discourse.org/t/32566?silent=true) allows you to make SQL queries against your live database, allowing for up-to-the-minute stats reporting.open_bookInstall Guide This plugin is bundled with Discourse core. There is no need to install the plugin separately.information_source If you’re looking for examples or support for any custom queries, you can find lots of topics in our #Data & reporting category under the #sql-query tag. If there’s not one to suit your pa…

## Possible Future Feature Request

I can see that an admin having the ability to directly search “all private messages” including those they are not a part of as an important admin tool for specific cases.

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [18 april 2017 om 18:15 UTC](https://meta.discourse.org/t/zero-search-results-searching-private-messages-for-specific-keyword/48429/10 "2017-04-18T18:15:55Z")

</div>


