Chapter 5. Searching

At this point, you have Solr running and some data indexed, and you're finally ready to put Solr to the test. Searching with Solr is arguably the most fun aspect of working with it, because it's quick and easy to do. While searching your data, you will learn more about its nature than before. It is also a source of interesting puzzles to solve when you troubleshoot why a search didn't find a document, or conversely, why it did, or even why a document wasn't scored sufficiently high.

In this chapter, you are going to learn about the following topics:

  • Request handlers
  • Query parameters
  • Solr's query syntax
  • The DisMax query parser – part 1
  • Filtering
  • Sorting
  • Joins
  • Geospatial

The subject of searching will progress into the next chapter for debugging queries, relevancy (that is, scoring) matters, function queries—an advanced capability used commonly in relevancy but also used in sorting and filtering—and geospatial search.

Tip

In a hurry?

This chapter has a lot of key information on searching. That said, if you're in a hurry, you can skim/skip query parsers, local-params, and the query syntax—you'll use DisMax instead. And you can skip DisMax's "min-should-match" too. Read about geospatial if it's applicable.

Your first search – a walk-through

We've got a lot of data indexed, and now it's time to actually use Solr for what it is intended to do—searching, also known as querying. When your application interacts with Solr, it will more than likely use HTTP, either directly via common APIs or indirectly through one of Solr's client APIs. However, as we demonstrate Solr's capabilities in this chapter, we'll use Solr's web-based admin interface. In Chapter 1, Quick Starting Solr, we covered the basics of Solr's admin interface. To use the admin query interface, click on the mbartists core link in the left navigation column, and then click Query.

You will see a window as shown in the following screenshot, after clicking on the Query link:

Your first search – a walk-through

The URL is http://localhost:8983/solr/#/mbartists/query. This form has a subset of the options you might specify to run a search. Let's do a quick search. In the q box, we'll leave the default of *:* (an asterisk, colon, and then another asterisk). Admittedly, that is cryptic if you've never seen it before, but it basically means "match anything in any field", which is to say, it matches all documents. Much more about the query syntax will be discussed soon enough.

Click on the Execute Query button and you'll see XML output appear in the main content area. Now click on the URL at the top of this area to view the full response. You should now see something like this:

<?xml version="1.0" encoding="UTF-8"?>
<response>
  <lst name="responseHeader">
    <int name="status">0</int>
    <int name="QTime">0</int>
    <lst name="params">
      <str name="q">*:*</str>
      <str name="wt">xml</str>
    </lst>
  </lst>
  <result name="response" numFound="399182" start="0">
    <doc>
      <str name="type">Artist</str>
      <str name="id">Artist:272344</str>
      <str name="a_name">F.D. Project</str>
      <date name="a_release_date_latest">2004-11-30T00:00:00Z</date>
      <date name="indexedAt">2013-01-24T01:05:29Z</date>
    </doc>
    <doc>
      <str name="type">Artist</str>
      <str name="id">Artist:274969</str>
      <str name="a_name">Tempradura</str>
      <date name="indexedAt">2013-01-24T01:05:29Z</date>
    </doc>
    <doc>
      <str name="type">Artist</str>
      <str name="id">Artist:143163</str>
      <str name="a_name">Future Pilot A.K.A. vs. Two Lone Swordsmen</str>
      <date name="indexedAt">2013-01-24T01:05:29Z</date>
    </doc>
    <!--… 7 more documents omitted for brevity -->
  </result>
</response>

Tip

Browser note

You can use any browser, but Firefox has worked best for every Solr release. Solr 4.1 broke Safari and IE support, which was fixed in Solr 4.2.

A note on response format types

In Chapter 1, Quick Starting Solr, we mentioned that XML is not the only response type supported by Solr. The types supported are XML, JSON, Python, Ruby, PHP, and CSV. The parameter responsible for controlling the response type is named wt—the response writer. This parameter accepts the previously mentioned formats as lowercased values. For example, to specify JSON, the wt param would be wt=json. Solr's admin query interface provides control of this parameter as a select box. Changing this select box to json and clicking Execute Query again would result in a JSON response similar to this:

{
  "responseHeader": {
    "status": 0,
    "QTime": 38,
    "params": {
      "indent": "true",
      "q": "*:*",
      "wt": "json"
    }
  },
  "response": {     
    "numFound": 399182,
    "start": 0,
    "docs": [
      {"type": "Artist",
      "id": "Artist:272344",
      "a_name": "F.D. Project",
      "a_release_date_latest": "2004-11-30T00:00:00Z",
      "indexedAt": "2013-01-24T01:05:29Z"},
….

As you have probably noticed, the response structure is the same as the XML format. The keys and values are also the same. The main difference between the JSON and XML is that the data type information is not present in the JSON response. This is a limitation present in all of the non-XML formats supported by Solr.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.32.116