Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Introduction to the Search API

DZone's Guide to

Introduction to the Search API

The search API is not only a protocol for application communications, it is an XQuery library that combines many search features. Read this intro for more details.

· Integration Zone
Free Resource

Learn how API management supports better integration in Achieving Enterprise Agility with Microservices and API Management, brought to you in partnership with 3scale

In today's blog, I want to briefly introduce the search API and its features. As with other APIs, the search API also a protocol for communication between applications. It is an XQuery library that combines searching, search parsing, search grammar, search term completion, and other search application features into a single API.You can interact with the search API through XQuery, REST, Node.js, and Java, using a variety of query styles. The search:search function takes search terms, parses them into an appropriate cts:query, and returns a response with snippets and URIs for matching nodes in the database. You can get started with the search API with a very simple query:

xquery version "1.0-ml";

import module namespace search = 
  "http://marklogic.com/appservices/search"
  at "/MarkLogic/appservices/search/search.xqy";

search:search("hello world")
=>
<search:response total="1" start="1" page-length="10" xmlns=""
  xmlns:search="http://marklogic.com/appservices/search">
  <search:result index="1" uri="/hello.xml"
    path="doc(&quot;/hello.xml&quot;)" score="136"
    confidence="0.67393" fitness="0.67393">
    <search:snippet>
      <search:match path="doc(&quot;/hello.xml&quot;)/hello">This is 
        where you say "<search:highlight>Hello</search:highlight>
        <search:highlight>World</search:highlight>".
      </search:match>
    </search:snippet>
  </search:result>
  <search:qtext>hello world</search:qtext>
  <search:metrics>
    <search:query-resolution-time>PT0.328S
      </search:query-resolution-time>
    <search:total-time>PT0.352S</search:total-time>
  </search:metrics>
</search:response>

The output is a search:response element, and it contains everything needed to build a search results page. It includes an estimate of the total number of documents that match the search, the URI and XPath for each result, pagination of the search results, a snippet of the results' content, the original query text submitted, and metrics on the response time. You can customize the data returned in each search:result using the result-decorator query option.

Nowadays, there is a competitor for the search API: Streaming API. The slight differences between the two APIs are listed below:

  1. Search goes back in time, and streaming goes forward. When you first decide to collect data on a subject, you have nothing to start with. The search API is best used to fill in the past seven days for your subject matter. This is often called back-filling.

  2. Both the search API and streaming API need login credentials for all requests.

  3. You are only allowed to make a single streaming API OAuth connection for each account that owns the app. No matter how many people log into your site, the app they all log into can only use a single streaming API connection. The search API, on the other hand, allows a separate rate limited bucket of requests for each user who logs into your app.

  4. Data formats are almost the same.

  5. The search API has a fairly rich set of operators that can filter results based on attributes like location of sender, language, and various popularity measurements.

  6. The streaming API can collect all tweets that contain up to 400 keyword phrases, sent by up to 5,000.

That's all for now, I'll catch you in the next session with some more blogs.

Unleash the power of your APIs with future-proof API management - Create your account and start your free trial today, brought to you in partnership with 3scale.

Topics:
api ,search api ,rest api ,xquery ,integration

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}