Wednesday, 2 December 2015

Elasticsearch: Java: Prefix query

“prefix” query matches documents that have fields containing terms with a specified prefix.
Add mappings to type “titles” like below.
PUT /books/_mappings/titles
{
  "titles" : {
    "properties" : {
      "title" :{
        "type" : "string",
        "index": "not_analyzed"
      }
    }
  } 
}


Insert below data into type “titles”.
PUT /_bulk
{"create" : {"_index": "books", "_type": "titles", "_id": "1" }}
{"id" : 1, "title" : "Courage: The Joy of Living Dangerously"}
{"create" : {"_index": "books", "_type": "titles", "_id": "2" }}
{"id" : 2, "title" : "Creativity: Unleashing the Forces Within"}
{"create" : {"_index": "books", "_type": "titles", "_id": "3" }}
{"id" : 3, "title" : "Joy: The Happiness That Comes from Within"}
{"create" : {"_index": "books", "_type": "titles", "_id": "4" }}
{"id" : 4, "title" : "Freedom: The Courage to Be Yourself "}
{"create" : {"_index": "books", "_type": "titles", "_id": "5" }}
{"id" : 5, "title" : "The Book of Secrets"}
{"create" : {"_index": "books", "_type": "titles", "_id": "6" }}
{"id" : 6, "title" : "Love, Freedom, and Aloneness: The Koan of Relationships"}
{"create" : {"_index": "books", "_type": "titles", "_id": "7" }}
{"id" : 7, "title" : "Life, Love, Laughter: Celebrating Your Existence"}


Get all books whose title starts with ‘L’.
QueryBuilder builder = QueryBuilders.prefixQuery("title", "L");

Above statement is used to get all the documents where title starts with ‘L’.


Following is the complete working application. Please go through following link, to get all utility classes.


package com.self_learn.test;

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

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.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 = "titles";

  public static void main(String args[]) throws IOException,
      InterruptedException, ExecutionException {
    Client client = TransportClientUtil.getLocalTransportClient(
        clusterName, 9300);

    QueryBuilder builder = QueryBuilders.prefixQuery("title", "L");

    System.out.println(builder);

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

    client.close();
  }
}


Above application generates following output.


Sep 14, 2015 9:49:53 AM org.elasticsearch.plugins.PluginsService <init>
INFO: [Meanstreak] loaded [], sites []
{
  "prefix" : {
    "title" : "L"
  }
}
{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "books",
      "_type" : "titles",
      "_id" : "6",
      "_score" : 1.0,
      "_source":{"id" : 6, "title" : "Love, Freedom, and Aloneness: The Koan of Relationships"}
    }, {
      "_index" : "books",
      "_type" : "titles",
      "_id" : "7",
      "_score" : 1.0,
      "_source":{"id" : 7, "title" : "Life, Love, Laughter: Celebrating Your Existence"}
    } ]
  }
}






Previous                                                 Next                                                 Home

No comments:

Post a Comment