大佬教程收集整理的这篇文章主要介绍了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个查询:
books
标题中带有“ book”一词的所有内容,然后返回authors
。authors
带有“ jon doe”字样的内容,然后返回books
。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().get@R_535_10586@lHits() + " 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().get@R_535_10586@lHits() + " 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().get@R_535_10586@lHits() + " results found for query 3.");
System.out.println(searchResponse3.toString());
System.out.println();
}
}
以上是大佬教程为你收集整理的Elastic search父子数据搜索Java API全部内容,希望文章能够帮你解决Elastic search父子数据搜索Java API所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。