Executing a standard search

Obviously, the most common action in Elasticsearch is searching. Elastic4s leverages the query DSL, bringing to Scala a type-safe definition for the queries. One of the most common advantages of this functionality is that, as Elasticsearch evolves, in Scala code via elastic4s, you can have deprecation or your compilation breaks, requiring you to update your code.

Getting ready

You need an up-and-running Elasticsearch installation, as we described in the Downloading and installing Elasticsearch recipe in Chapter 2, Downloading and Setup.

A Maven tool or an IDE that supports Scala programming, such as Eclipse (ScalaIDE) or IntelliJ IDEA, with the Scala plugin should be installed.

The code for this recipe can be found in the chapter_15/elastic4s_sample file and the referred class is QueryExample.

How to do it...

To execute a standard query, we will perform the following steps:

  1. We import the classes and implicits required to index and search the data:
            import com.sksamuel.elastic4s.ElasticDsl._ 
            import com.sksamuel.elastic4s.circe._ 
            import com.sksamuel.elastic4s.Indexable 
            import io.circe.generic.auto._ 
    
  2. We create an index and populate it with some data. We use bulk calls for speed up:
            object QueryExample extends App with ElasticSearchClientTrait { 
              val indexName = "myindex" 
              val typeName = "mytype" 
     
              case class Place(id: Int, name: String) 
              case class Cafe(name: String, place: Place) 
     
             implicitly[Indexable[Cafe]] 
     
              ensureIndexMapping(indexName, typeName) 
     
              client.execute { 
                bulk( 
                  indexInto(indexName /  
                  typeName).id("0").source(Cafe("nespresso", Place(20,  
                  "Milan"))), 
                  ...truncated... 
                  indexInto(indexName /     
                  typeName).id("8").source(Cafe("nespresso", Place(23, 
                  "Chicago"))), 
                  indexInto(indexName / 
                  typeName).id("9").source(Cafe("java", Place(89, 
                  "London"))) 
                ) 
              }.await 
              Thread.sleep(2000) 
    
  3. We can use a bool filter for search documents with the name equal to java and place.id greater than or equal to 80:
              val resp = client.execute { 
                search(indexName / typeName).bool(must(termQuery("name", 
                "java"), rangeQuery("place.id").gte(80))) 
              }.await 
    
  4. When we have the response parameter, we need to check its count and we can convert it back to a list of classes:
            println(resp.size) 
            println(resp.to[Cafe].toList) 
    
  5. The result should be similar to this one:
            2 
            List(Cafe(java,Place(80,Chicago)), Cafe(java,Place(89,London))) 
    

How it works...

The Elastic4s query DSL wraps the Elasticsearch one in a more human-readable way.

The search method allows us to define, via DSL, a complex query. The result is a wrapper of the original Java result that provides some helpers to be more productive.

The common methods of the Java result are available at a top level, but they also provide two interesting methods: to and safeTo.

They are able to convert the results in case classes via the implicit conversions available in the scope. In the case of the to[T] method, the result is an iterator of T (in the preceding example, we have the conversion back to a List of Cafe). In the case of safeTo[T], the result is an Either[Throwable, T]; in this way, it's possible to collect the conversion errors/exceptions.

Tip

Using the typeclass in Scala allows you to write a cleaner and eas- t0-understand code and also reduces the errors due to string management in Elasticsearch.

See also

  • The Executing a search recipe in Chapter 5, Search, for more detailed information about executing a query
..................Content has been hidden....................

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