弹性搜索父子数据搜索Java API [英] Elastic Search Parent-Child Data Search Java API

查看:112
本文介绍了弹性搜索父子数据搜索Java API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的弹性搜索。



弹性搜索中的数据位于父子模型中。我想使用java api执行此数据的搜索。 p>

父类型包含作者详细信息和子类型包含图书名称,图书出版商,图书类别等图书详细信息。



在对子细节进行搜索时,我还需要获取父细节,反之亦然。有时搜索条件将是父类型和子类。例如搜索由 author1 编写的图书,然后键入 Fiction



如何在java中实现?我已经提到弹性搜索文档,但无法获得解决方案



请帮助

解决方案

首先使用父/子映射设置索引。在下面的映射中,我还为类别添加了一个未打开的字段,以便您可以对该字段执行过滤器查询。 (为了创建索引和文档,我使用JSON API而不是Java API,因为这不是问题的一部分。)

  POST / test 
{
mappings:{
book:{
_parent:{
type:author
$ bproperty:{
category:{
type:string,
fields:{
raw :{
type:string,
index:not_analyzed
}
}
}
}
}
}
}

创建一些作者文件:

  POST / test / author / 1 
{
name :jon doe
}


POST / test / author / 2
{
name:jane smith
}

创建一些书籍文件,指定作者之间的关系在请求中。

  POST / test / book / 12?parent = 1 
{
name:fictional book,
category:Fiction,
publisher:publisher1
}

POST / test / book / 16?parent = 2
{
name:历史书,
category:历史,
publisher:publisher2
}

POST / test / book / 20?parent = 2
{
name:第二个虚构的书,
category:Fiction,
publisher:publisher2
}

下面的Java类执行3个查询:


  1. 搜索标题中有书的所有书籍
    返回作者

  2. 搜索所有作者在名称中使用jon doe,
    返回书籍

  3. 搜索jane smith编写的书籍,类型为Fiction。

您可以运行c lass从命令行,或导入到Eclipse并右键单击该类并选择'运行为> Java应用程序'。 (您需要在类路径中使用Elasticsearch库。)

  import java.util.concurrent.ExecutionException; 

import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.HasChildQueryBuilder;
import org.elasticsearch.index.query.HasParentQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermFilterBuilder;

public class ParentChildQueryExample {

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

//设置传输客户端用于与ES集群进行通信。也可以使用客户端节点进行设置。
设置设置= ImmutableSettings.settingsBuilder()
.put(cluster.name,elasticsearch)。build();
客户端客户端=新的TransportClient(设置)
.addTransportAddress(新的InetSocketTransportAddress(
localhost,
9300));

//创建searchRequestBuilder对象。
SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(client).setIndices(test);

//查询1.在标题中搜索所有具有术语书的书,并返回作者。
HasChildQueryBuilder bookNameQuery = QueryBuilders.hasChildQuery(book,QueryBuilders.matchQuery(name,book));
System.out.println(Exectuing Query 1);
SearchResponse searchResponse1 = searchRequestBuilder.setQuery(bookNameQuery).execute()。actionGet();
System.out.println(有+ searchResponse1.getHits()。getTotalHits()+查询1的结果);
System.out.println(searchResponse1.toString());
System.out.println();

//查询2.在名称中搜索所有具有术语jon doe的作者,并返回书。
HasParentQueryBuilder authorNameQuery = QueryBuilders.hasParentQuery(author,QueryBuilders.matchQuery(name,jon doe));
System.out.println(Exectuing Query 2);
SearchResponse searchResponse2 = searchRequestBuilder.setQuery(authorNameQuery).execute()。actionGet();
System.out.println(有+ searchResponse2.getHits()。getTotalHits()+查询2的结果);
System.out.println(searchResponse2.toString());
System.out.println();

//查询3.搜索由jane smith编写的书籍,并输入小说。
TermFilterBuilder termFilter = FilterBuilders.termFilter(category.raw,Fiction);
HasParentQueryBuilder authorNameQuery2 = QueryBuilders.hasParentQuery(author,QueryBuilders.matchQuery(name,jane smith));
SearchResponse searchResponse3 = searchRequestBuilder.setQuery(QueryBuilders.filteredQuery(authorNameQuery2,termFilter))。execute()。actionGet();
System.out.println(有+ searchResponse3.getHits()。getTotalHits()+查询3的结果。);
System.out.println(searchResponse3.toString());
System.out.println();
}
}


I am new to ELastic Search.

Data in Elastic search is in Parent-Child Model.I want to perform search in this data using java api.

parent type contains author details and child type contains book details like book name,book publisher, book category.

While performing a search on child details,I need to get the parent details also and vice versa. Sometimes search conditions will be on parent type as well as child. eg search for books written by author1 and type Fiction.

How can i implement this in java? I have referred the elastic search documentation but not able to get a solution

Please help

解决方案

First set up your index with the parent/child mapping. In the mapping below I have also added a untokenized field for categories so you can execute filter queries on that field. (For creating the index and documents I'm using the JSON API not the Java API as that was not part of the question.)

POST /test
{
    "mappings": {
        "book": {
            "_parent": {
                "type": "author"
            },
            "properties":{
                "category":{
                    "type":"string",
                    "fields":{
                        "raw":{
                            "type":"string",
                            "index": "not_analyzed"
                        }
                    }
                }
            }
        }
    }
}

Create some author documents:

POST /test/author/1
{
    "name": "jon doe"
}


POST /test/author/2
{
    "name": "jane smith"
}

Create some book documents, specifying the relationship between book and author in the request.

POST /test/book/12?parent=1
{
    "name": "fictional book",
    "category": "Fiction",
    "publisher": "publisher1"
}

POST /test/book/16?parent=2
{
    "name": "book of history",
    "category": "historical",
    "publisher": "publisher2"
}

POST /test/book/20?parent=2
{
    "name": "second fictional book",
    "category": "Fiction",
    "publisher": "publisher2"
}

The Java class below executes 3 queries:

  1. Search on all books that have the term 'book' in the title and return the authors.
  2. Search on all authors that have the terms 'jon doe' in the name and return the books.
  3. Search for books written by 'jane smith' and that are of type Fiction.

You can run the class from the command line, or import into Eclipse and right click on the class and select 'Run As > Java Application'. (You'll need to have the Elasticsearch library in the classpath.)

import java.util.concurrent.ExecutionException;

import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.HasChildQueryBuilder;
import org.elasticsearch.index.query.HasParentQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermFilterBuilder;

public class ParentChildQueryExample {

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

    //Set the Transport client which is used to communicate with your ES cluster. It is also possible to set this up using the Client Node.
    Settings settings = ImmutableSettings.settingsBuilder()
        .put("cluster.name", "elasticsearch").build();
    Client client = new TransportClient(settings)
        .addTransportAddress(new InetSocketTransportAddress(
        "localhost",
        9300));

    //create the searchRequestBuilder object.
    SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(client).setIndices("test");

    //Query 1. Search on all books that have the term 'book' in the title and return the 'authors'.
    HasChildQueryBuilder bookNameQuery = QueryBuilders.hasChildQuery("book", QueryBuilders.matchQuery("name", "book"));
    System.out.println("Exectuing Query 1");
    SearchResponse searchResponse1 = searchRequestBuilder.setQuery(bookNameQuery).execute().actionGet();
    System.out.println("There were " + searchResponse1.getHits().getTotalHits()  + " results found for Query 1.");
    System.out.println(searchResponse1.toString());
    System.out.println();

    //Query 2. Search on all authors that have the terms 'jon doe' in the name and return the 'books'.
    HasParentQueryBuilder authorNameQuery = QueryBuilders.hasParentQuery("author", QueryBuilders.matchQuery("name", "jon doe"));
    System.out.println("Exectuing Query 2");
    SearchResponse searchResponse2 = searchRequestBuilder.setQuery(authorNameQuery).execute().actionGet();
    System.out.println("There were " + searchResponse2.getHits().getTotalHits()  + " results found for Query 2.");
    System.out.println(searchResponse2.toString());
    System.out.println();

    //Query 3. Search for books written by 'jane smith' and type Fiction.
    TermFilterBuilder termFilter = FilterBuilders.termFilter("category.raw", "Fiction");
    HasParentQueryBuilder authorNameQuery2 = QueryBuilders.hasParentQuery("author", QueryBuilders.matchQuery("name", "jane smith"));
    SearchResponse searchResponse3 = searchRequestBuilder.setQuery(QueryBuilders.filteredQuery(authorNameQuery2, termFilter)).execute().actionGet();
    System.out.println("There were " + searchResponse3.getHits().getTotalHits()  + " results found for Query 3.");
    System.out.println(searchResponse3.toString());
    System.out.println();
  }
}

这篇关于弹性搜索父子数据搜索Java API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆