栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Elastic search父子数据搜索Java API

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Elastic search父子数据搜索Java API

首先使用

parent/child
映射设置索引。在下面的映射中,我还添加了一个未标记的字段,
categories
以便您可以对该字段执行过滤器查询。(对于创建索引和文档,我使用的是JSONAPI而不是JavaAPI,因为这不是问题的一部分。)

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

创建一些

author
文档:

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

创建一些

book
文档,指定请求之间
book
和之间的关系
author

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"}

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

  1. 搜索
    books
    标题中带有“ book”一词的所有内容,然后返回
    authors
  2. 搜索名称中所有
    authors
    带有“ jon doe”字样的内容,然后返回
    books
  3. 搜索
    books
    “ jane smith”写的且类型为Fiction的内容。

您可以从命令行运行该类,也可以将其导入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 {    //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();  }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/390443.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号