user7154067
user7154067

Reputation: 21

Solr returning docs on empty query - How to stop?

<!-- Search -->
<div class="searchresults">
    <div>
        <div class="searchresultsinput">
            @Html.TextBox("query-box", Model.Search.FreeSearch)
            <input type="image" id="search-btn" class="search-btn" onclick="ExecuteSearch(); return false;" src='@Html.IncludeCDNImagePathWithTimeStamp("Buttons/search-now.png")' />
        </div>
        <div class="searchresultscounts">
            @{
                if (Model.TotalCount > 0)
                {
                    if (Model.Search.LastItemIndex > Model.TotalCount)
                    {
                        <div>Showing @(Model.Search.FirstItemIndex + 1) - @Model.TotalCount of @Model.TotalCount documents.</div>
                    }
                    else
                    {
                        <div>Showing @(Model.Search.FirstItemIndex + 1) - @Model.Search.LastItemIndex of @Model.TotalCount documents.</div>
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(Model.DidYouMean))
                    {
                        <span>Did you mean </span><strong><em><a href="<%= Url.ForQuery(Model.DidYouMean) %>"><%= Model.DidYouMean%></a></em></strong>
                    }
                    else
                    {
                        <div>No results found. Please try a different search term.</div>
                    }
                }
            }

        </div>

I have three Solr Instances on Internal QA Environment and Two on Production. All of them are almost configuration wise identical. The problem is Solr is returning search results even when nothing is typed in the search box field? [Search Button on the Website]

Can anyone please help as to what I need to do at Solr level to stop this from happening. I wanna be able to display a message stating "You Search Enter 0 Results" if somebody tries to hit the search button with an empty query. Thanks.

Upvotes: 1

Views: 195

Answers (2)

Marek Musielak
Marek Musielak

Reputation: 27142

It's not Solr which is returning docs on empty query. I'm quite sure you're sending some query to Solr like "templateid: someId" or even something more.

What you need to do is to check in your frontend if user typed anything in the box (or in the backend but before the call to Solr is even called) and display the correct message to the user.

However without seeing the code you're using to populate search result list, no one will be able to give you exact code you need.


EDIT

Your html contains onclick="ExecuteSearch(); return false;".

Change your ExecuteSearch function and add a check if the query-box input value is not empty. If it's empty, don't send request to Solr.

Upvotes: 2

Alessandro Hoss
Alessandro Hoss

Reputation: 395

If your business rule is to show 0 results when nothing is typed, you should handle this behavior in the view or server layer, before your server hits Solr, and don't hit it at all.

Upvotes: 2

Related Questions