Wednesday 2 December 2015

Elasticsearch: Java : match query

Match query is used for full text search.

For example, following match query return all documents, that contains word religious in description field.

QueryBuilder builder = QueryBuilders.matchQuery("description","religious");
Following are the model classes and Main class to demonstrate simple application. 
Please go through following link for utility classes.
package com.self_learn.model;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@EqualsAndHashCode()
@ToString
public class Author {
 @Getter @Setter private String firstName;
 @Getter @Setter private String lastName;
}

package com.self_learn.model;

import java.util.ArrayList;
import java.util.List;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@EqualsAndHashCode()
@ToString
public class Book {
 @Getter @Setter private int pages;
 @Getter @Setter private String title;
 @Getter @Setter private String description;
 @Getter @Setter private String ISBN;
 @Getter @Setter private String publisher;
 @Getter @Setter private Author author = new Author();
 @Getter @Setter private double price;
 @Getter @Setter private List<String> tags = new ArrayList<>();
}

package com.self_learn.test;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutionException;

import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

import com.self_learn.model.Book;
import com.self_learn.model.BulkObject;
import com.self_learn.util.BulkUtil;
import com.self_learn.util.SearchUtil;
import com.self_learn.util.TransportClientUtil;

public class Main {
 private static String clusterName = "my_cluster_1";
 private static String _index = "books";
 private static String _type = "philosophy";

 private static Book bk1, bk2, bk3, bk4, bk5, bk6, bk7;

 private static void populateData() {
  bk1 = new Book();
  bk2 = new Book();
  bk3 = new Book();
  bk4 = new Book();
  bk5 = new Book();
  bk6 = new Book();
  bk7 = new Book();

  bk1.getAuthor().setFirstName("Osho");
  bk1.getAuthor().setLastName("Chandra Mohan Jain");
  bk1.setDescription("These techniques will not mention any religious ritual. No temple is needed, you are quite enough of a temple yourself. You are the lab; the whole experiment is to go on within you. This is not religion, this is science. No belief is needed. Only a daringness to experiment is enough; courage to experiment is enough.");
  bk1.setISBN("0312180586");
  bk1.setPages(1152);
  bk1.setPrice(1500.0);
  bk1.setPublisher("St. Martins Griffin");
  bk1.setTitle("The Book of Secrets");
  bk1.getTags().add("Spiritual");
  bk1.getTags().add("Philosophy");
  bk1.getTags().add("secrets");

  bk2.getAuthor().setFirstName("Osho");
  bk2.getAuthor().setLastName("Chandra Mohan Jain");
  bk2.setDescription("In the beginning there is not much difference between the coward and the courageous person. The only difference is, the coward listens to his fears and follows them, and the courageous person puts them aside and goes ahead. The courageous person goes into the unknown in spite of all the fears.");
  bk2.setISBN("0312205171");
  bk2.setPages(208);
  bk2.setPrice(350.0);
  bk2.setPublisher("St. Martins Griffin");
  bk2.setTitle("Courage: The Joy of Living Dangerously");
  bk2.getTags().add("Spiritual");
  bk2.getTags().add("Philosophy");
  bk2.getTags().add("courage");
  bk2.getTags().add("Living joyfully");

  bk3.getAuthor().setFirstName("Mitch");
  bk3.getAuthor().setLastName("Albom");
  bk3.setDescription("While sleeping near a sycamore tree in the sacristy of an abandoned church, Santiago, a shepherd boy, has a recurring dream about a child who tells him that he will find a hidden treasure if he travels to the Egyptian pyramids. An old woman tells Santiago that this dream is prophetic and that he must follow its instructions. Santiago is uncertain, however, since he enjoys the life of a shepherd.");
  bk3.setISBN("0061122416");
  bk3.setPages(197);
  bk3.setPrice(380.0);
  bk3.setPublisher("HarperOne");
  bk3.setTitle("The Alchemist");
  bk3.getTags().add("literature");
  bk3.getTags().add("fiction");
  bk3.getTags().add("religion");
  bk3.getTags().add("spirituality");

  bk4.getAuthor().setFirstName("Osho");
  bk4.getAuthor().setLastName("Chandra Mohan Jain");
  bk4.setDescription("I am not a Buddhist, but a Christian. I started reading, just to see what it was about and read the whole thing. The beginning is a very relaxing read and had some highlightable/clipable quotes. I do have different religious beliefs, this book offered some areas for further prayer and contemplation but I did not find these areas offensive so much as a beneficial exposure to a different perspective");
  bk4.setISBN("1938755928");
  bk4.setPages(152);
  bk4.setPrice(400.0);
  bk4.setPublisher("Osho Media International");
  bk4.setTitle("Emotions: Freedom from Anger, Jealousy and Fear");
  bk4.getTags().add("Spiritual");
  bk4.getTags().add("Philosophy");
  bk4.getTags().add("emotions");
  bk4.getTags().add("freedom");
  bk4.getTags().add("angry");
  bk4.getTags().add("Jealousy");
  bk4.getTags().add("Fear");

  bk5.getAuthor().setFirstName("Mitch");
  bk5.getAuthor().setLastName("Albom");
  bk5.setDescription("tells the story of a young womans transformation from despairing would-be suicide to affirmed and then affirming survivor. This book offers an archetypal story of hope, portraying a situation in which joy, freedom, integrity and truth all remain possible under the most challenging and limiting of circumstances. In doing so, the narrative thematically explores the nature of insanity, the importance of living a genuine life, and the threats to individual identity imposed by closed communities and the rules under which they function.");
  bk5.setISBN("5955000836");
  bk5.setPages(240);
  bk5.setPrice(420.0);
  bk5.setPublisher("Harper Perennial");
  bk5.setTitle("Veronika Decides to Die: A Novel of Redemption");
  bk5.getTags().add("literature");
  bk5.getTags().add("fiction");

  bk6.getAuthor().setFirstName("Plato");
  bk6.getAuthor().setLastName("Plato");
  bk6.setDescription("Platos most famous work and one of the most important books ever written on the subject of philosophy and political theory");
  bk6.setISBN("1420931695");
  bk6.setPages(220);
  bk6.setPrice(342);
  bk6.setPublisher("Digireads.com");
  bk6.setTitle("The Republic");
  bk6.getTags().add("life");
  bk6.getTags().add("philosophy");
  bk6.getTags().add("conversations");

  bk7.getAuthor().setFirstName("Mitch");
  bk7.getAuthor().setLastName("Albom");
  bk7.setDescription("Maybe it was a grandparent, or a teacher, or a colleague. Someone older, patient and wise, who understood you when you were young and searching, helped you see the world as a more profound place, gave you sound advice to help you make your way through it.");
  bk7.setISBN("076790592X");
  bk7.setPages(224);
  bk7.setPrice(172.0);
  bk7.setPublisher("Broadway Books");
  bk7.setTitle("Tuesdays with Morrie: An Old Man, a Young Man, and Lifes Greatest Lesson");
  bk7.getTags().add("life");
  bk7.getTags().add("philosophy");
  bk7.getTags().add("mankind");
  bk7.getTags().add("conversations");
 }

 public static void main(String args[]) throws IOException,
   InterruptedException, ExecutionException {

  populateData();
  /* Get client instance for cluster */
  Client client = TransportClientUtil.getLocalTransportClient(
    clusterName, 9300);

  Set<BulkObject> bulkReq = new HashSet<>();

  bulkReq.add(BulkUtil.getIndexObject(_index, _type, "1", bk1));
  bulkReq.add(BulkUtil.getIndexObject(_index, _type, "2", bk2));
  bulkReq.add(BulkUtil.getIndexObject(_index, _type, "3", bk3));
  bulkReq.add(BulkUtil.getIndexObject(_index, _type, "4", bk4));
  bulkReq.add(BulkUtil.getIndexObject(_index, _type, "5", bk5));
  bulkReq.add(BulkUtil.getIndexObject(_index, _type, "6", bk6));
  bulkReq.add(BulkUtil.getIndexObject(_index, _type, "7", bk7));

  BulkUtil.execute(client, bulkReq);

  QueryBuilder builder = QueryBuilders.matchQuery("description",
    "religious");
  SearchResponse response = SearchUtil.getDocuments(client, builder,
    _index, _type);
  System.out.println(response);

  client.close();
 }
}


Run Main.java, you will get following output.

Sep 10, 2015 10:02:44 PM org.elasticsearch.common.MacAddressProvider getSecureMungedAddress
WARNING: Unable to get a valid mac address, will use a dummy address
Sep 10, 2015 10:02:44 PM org.elasticsearch.plugins.PluginsService <init>
INFO: [Mary Walker] loaded [], sites []
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 0.2116434,
    "hits" : [ {
      "_index" : "books",
      "_type" : "philosophy",
      "_id" : "1",
      "_score" : 0.2116434,
      "_source":{"pages":1152,"title":"The Book of Secrets","description":"These techniques will not mention any religious ritual. No temple is needed, you are quite enough of a temple yourself. You are the lab; the whole experiment is to go on within you. This is not religion, this is science. No belief is needed. Only a daringness to experiment is enough; courage to experiment is enough.","ISBN":"0312180586","publisher":"St. Martins Griffin","author":{"firstName":"Osho","lastName":"Chandra Mohan Jain"},"price":1500.0,"tags":["Spiritual","Philosophy","secrets"]}
    }, {
      "_index" : "books",
      "_type" : "philosophy",
      "_id" : "4",
      "_score" : 0.109375,
      "_source":{"pages":152,"title":"Emotions: Freedom from Anger, Jealousy and Fear","description":"I am not a Buddhist, but a Christian. I started reading, just to see what it was about and read the whole thing. The beginning is a very relaxing read and had some highlightable/clipable quotes. I do have different religious beliefs, this book offered some areas for further prayer and contemplation but I did not find these areas offensive so much as a beneficial exposure to a different perspective","ISBN":"1938755928","publisher":"Osho Media International","author":{"firstName":"Osho","lastName":"Chandra Mohan Jain"},"price":400.0,"tags":["Spiritual","Philosophy","emotions","freedom","angry","Jealousy","Fear"]}
    } ]
  }
}

Following are some of the match queries for your reference

1. Get all the documents that match all terms "young transformation Egyptian"
QueryBuilder builder = QueryBuilders.matchQuery("description","young transformation Egyptian").operator(MatchQueryBuilder.Operator.AND);


Above statement generates following json document.
{
  "match" : {
    "description" : {
      "query" : "young transformation Egyptian",
      "type" : "boolean",
      "operator" : "AND"
    }
  }
}

3. Get all documents, that match atleast 2 terms in “young transformation Egyptian”.
QueryBuilder builder = QueryBuilders.matchQuery("description","young transformation Egyptian").minimumShouldMatch("2");


Above statement generates following json document.


{
  "match" : {
    "description" : {
      "query" : "young transformation Egyptian",
      "type" : "boolean",
      "minimum_should_match" : "2"
    }
  }
}





Previous                                                 Next                                                 Home

No comments:

Post a Comment