Wednesday 2 December 2015

Elasticsearch: Java : Boosting Query

The boosting query can be used to effectively demote results that match a given query.
For example, I want to get all the documents, where description contains “religious Buddhist Christian”. I want to give positive boost to the documents that contains word “religious” and negative boost to the documents that contain word “Buddhist Christian”.

QueryBuilder builder = QueryBuilders
 .boostingQuery()
 .positive(QueryBuilders.termQuery("description", "religious"))
 .negative(QueryBuilders.termQuery("description","Buddhist Christian"))
 .negativeBoost(0.2f);

Above query return following json document.

{
  "boosting" : {
    "positive" : {
      "term" : {
        "description" : "religious"
      }
    },
    "negative" : {
      "term" : {
        "description" : "Buddhist Christian"
      }
    },
    "negative_boost" : 0.2
  }
}


Following is the complete working application, Please note that to run following application, you require some model and utility classes, you will get these from following location.
https://self-learning-java-tutorial.blogspot.com/2015/11/utility-classes-for-elastic-search.html

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.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.MatchQueryBuilder;
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();
  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
    .boostingQuery()
    .positive(QueryBuilders.termQuery("description", "religious"))
    .negative(QueryBuilders.termQuery("description","Buddhist Christian"))
    .negativeBoost(0.2f);

  //System.out.println(builder);

  SearchResponse response = SearchUtil.getDocuments(client, builder,
    _index, _type);
  System.out.println(response);

  client.close();
 }
}


Once you ran above application, you will get following output.

Sep 11, 2015 11:11:54 AM org.elasticsearch.plugins.PluginsService <init>
INFO: [Volstagg] 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"]}
    } ]
  }
}







Previous                                                 Next                                                 Home

No comments:

Post a Comment