/ * *
* System environment: centos 7.2 under VM12
* Current version: ElasticSearch-2.4.0.tar. gz
* /
QueryBuilder is a query interface provided in ES, which can be set to its parameters to check the use of erasure training
package com.wenbronk.javaes;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map.Entry;
import org.elasticsearch.action.ListenableActionFuture;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.IndicesQueryBuilder;
import org.elasticsearch.index.query.NestedQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.index.query.SpanFirstQueryBuilder;
import org.elasticsearch.index.query.WildcardQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.junit.Before;
import org.junit.Test;
/** * Java operation query API *@author231 * * /
public class JavaESQuery {
private TransportClient client;
@Before
public void testBefore(a) {
Settings settings = Settings.settingsBuilder().put("cluster.name"."wenbronk_escluster").build();
client = TransportClient.builder().settings(settings).build()
.addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress("192.168.50.37".9300)));
System.out.println("success to connect escluster");
}
/** * use get to query */
@Test
public void testGet(a) {
GetRequestBuilder requestBuilder = client.prepareGet("twitter"."tweet"."1");
GetResponse response = requestBuilder.execute().actionGet();
GetResponse getResponse = requestBuilder.get();
ListenableActionFuture<GetResponse> execute = requestBuilder.execute();
System.out.println(response.getSourceAsString());
}
TermQuery ("key", obj) exactly matches termsQuery("key", obj1, obj2..) Multi value matching at one time * matchQuery("key", Obj) single match, field not supported wildcards, prefix with advanced features * multiMatchQuery("text", "field1", "field2"..) ; Match multiple fields, field has wildcard line * matchAllQuery(); Matches all files */
@Test
public void testQueryBuilder(a) {
// QueryBuilder queryBuilder = QueryBuilders.termQuery("user", "kimchy");QueryBUilder queryBuilder = QueryBuilders.termQuery("user"."kimchy"."wenbronk"."vini");
QueryBuilders.termsQuery("user".new ArrayList<String>().add("kimchy"));
// QueryBuilder queryBuilder = QueryBuilders.matchQuery("user", "kimchy");
// QueryBuilder queryBuilder = QueryBuilders.multiMatchQuery("kimchy", "user", "message", "gender");
QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
searchFunction(queryBuilder);
}
Must (QueryBuilders): AND mustNot(QueryBuilders): NOT * should: : OR */
@Test
public void testQueryBuilder2(a) {
QueryBuilder queryBuilder = QueryBuilders.boolQuery()
.must(QueryBuilders.termQuery("user"."kimchy"))
.mustNot(QueryBuilders.termQuery("message"."nihao"))
.should(QueryBuilders.termQuery("gender"."male"));
searchFunction(queryBuilder);
}
/** * query only one id * QueryBuilders. IdsQuery (String... type).ids(Collection
ids) */
@Test
public void testIdsQuery(a) {
QueryBuilder queryBuilder = QueryBuilders.idsQuery().ids("1");
searchFunction(queryBuilder);
}
/** * Package query, higher than the set score, does not count correlation */
@Test
public void testConstantScoreQuery(a) {
QueryBuilder queryBuilder = QueryBuilders.constantScoreQuery(QueryBuilders.termQuery("name"."kimchy")).boost(2.0 f);
searchFunction(queryBuilder);
// Filter the query
/ / QueryBuilders constantScoreQuery (FilterBuilders termQuery (" name ", "kimchy")). The boost (2.0 f);
}
/** * disMax query * union the result of subquery, score uses the maximum score, * widely used for mui-field query */
@Test
public void testDisMaxQuery(a) {
QueryBuilder queryBuilder = QueryBuilders.disMaxQuery()
.add(QueryBuilders.termQuery("user"."kimch")) // Query conditions
.add(QueryBuilders.termQuery("message"."hello"))
.boost(1.3 f)
.tieBreaker(0.7 f);
searchFunction(queryBuilder);
}
Fuzzy query / * * * * can't use wildcards, don't know what to use * /
@Test
public void testFuzzyQuery(a) {
QueryBuilder queryBuilder = QueryBuilders.fuzzyQuery("user"."kimch");
searchFunction(queryBuilder);
}
/** * parent or child document query */
@Test
public void testChildQuery(a) {
QueryBuilder queryBuilder = QueryBuilders.hasChildQuery("sonDoc", QueryBuilders.termQuery("name"."vini"));
searchFunction(queryBuilder);
}
* {"more_like_this" : {"fields" : {"fields" : ["title", "content"], // to match the field, do not fill the default _all "like_text" : "Text like this one", // matching text within}} percent_terms_to_match: percentage of matches (term), default 0.3 min_term_freq: Max_query_terms: specifies the maximum number of terms allowed to be queried in a query statement. The default value is 25. Stop_words: sets the stop word and ignores the min_doc_freq. Max_doc_freq: Max_doc_freq: Max_doc_freq: Max_doc_freq: Max_doc_freq: Max_doc_freq: Max_doc_freq: Max_doc_freq: Max_doc_freq: Max_doc_freq: Max_doc_freq: Max_word_len: maximum word length, default unlimited BOOST_terms: sets the word weight, default 1 Boost: sets the query weight, default 1 Analyzer: sets the word divider used, default is the word divider specified with this field */
@Test
public void testMoreLikeThisQuery(a) {
QueryBuilder queryBuilder = QueryBuilders.moreLikeThisQuery("user")
.like("kimchy");
//.mintermfreq (1) // Minimum number of occurrences
// .maxQueryTerms(12); // The maximum number of words allowed to be queried
searchFunction(queryBuilder);
}
/** ** prefix query */
@Test
public void testPrefixQuery(a) {
QueryBuilder queryBuilder = QueryBuilders.matchQuery("user"."kimchy");
searchFunction(queryBuilder);
}
/** * Query parses query string */
@Test
public void testQueryString(a) {
QueryBuilder queryBuilder = QueryBuilders.queryStringQuery("+kimchy");
searchFunction(queryBuilder);
}
/** ** query */
public void testRangeQuery(a) {
QueryBuilder queryBuilder = QueryBuilders.rangeQuery("user")
.from("kimchy")
.to("wenbronk")
.includeLower(true) // contains an upper bound
.includeUpper(true); // Contains the next
searchFunction(queryBuilder);
}
/** * span query */
@Test
public void testSpanQueries(a) {
QueryBuilder queryBuilder1 = QueryBuilders.spanFirstQuery(QueryBuilders.spanTermQuery("name"."Gourd 580 baby"), 30000); // Max Indicates the end position of the query range
QueryBuilder queryBuilder2 = QueryBuilders.spanNearQuery()
.clause(QueryBuilders.spanTermQuery("name"."Gourd 580 baby")) // Span Term Queries
.clause(QueryBuilders.spanTermQuery("name"."Gourd 3812 children"))
.clause(QueryBuilders.spanTermQuery("name"."Gourd 7139"))
.slop(30000) // Slop factor
.inOrder(false)
.collectPayloads(false);
// Span Not
QueryBuilder queryBuilder3 = QueryBuilders.spanNotQuery()
.include(QueryBuilders.spanTermQuery("name"."Gourd 580 baby"))
.exclude(QueryBuilders.spanTermQuery("home"."2552 Street, Taiyuan City, Shanxi Province"));
// Span Or
QueryBuilder queryBuilder4 = QueryBuilders.spanOrQuery()
.clause(QueryBuilders.spanTermQuery("name"."Gourd 580 baby"))
.clause(QueryBuilders.spanTermQuery("name"."Gourd 3812 children"))
.clause(QueryBuilders.spanTermQuery("name"."Gourd 7139"));
// Span Term
QueryBuilder queryBuilder5 = QueryBuilders.spanTermQuery("name"."Gourd 580 baby");
}
/** * test subquery */
@Test
public void testTopChildrenQuery(a) {
QueryBuilders.hasChildQuery("tweet",
QueryBuilders.termQuery("user"."kimchy"))
.scoreMode("max");
}
/** * wildcard query, support ** to match any character sequence, including null * avoid * start, will retrieve a large number of content resulting in slow */
@Test
public void testWildCardQuery(a) {
QueryBuilder queryBuilder = QueryBuilders.wildcardQuery("user"."ki*hy");
searchFunction(queryBuilder);
}
/** * nested query, nested document query */
@Test
public void testNestedQuery(a) {
QueryBuilder queryBuilder = QueryBuilders.nestedQuery("location",
QueryBuilders.boolQuery()
.must(QueryBuilders.matchQuery("location.lat".0.962590433140581))
.must(QueryBuilders.rangeQuery("location.lon").lt(36.0000).gt(0.000)))
.scoreMode("total");
}
/**
* 测试索引查询
*/
@Test
public void testIndicesQueryBuilder (a) {
QueryBuilder queryBuilder = QueryBuilders.indicesQuery(
QueryBuilders.termQuery("user"."kimchy"), "index1"."index2")
.noMatchQuery(QueryBuilders.termQuery("user"."kimchy"));
}
/** * query traversal extract *@param queryBuilder
*/
private void searchFunction(QueryBuilder queryBuilder) {
SearchResponse response = client.prepareSearch("twitter")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setScroll(new TimeValue(60000))
.setQuery(queryBuilder)
.setSize(100).execute().actionGet();
while(true) {
response = client.prepareSearchScroll(response.getScrollId())
.setScroll(new TimeValue(60000)).execute().actionGet();
for (SearchHit hit : response.getHits()) {
Iterator<Entry<String, Object>> iterator = hit.getSource().entrySet().iterator();
while(iterator.hasNext()) {
Entry<String, Object> next = iterator.next();
System.out.println(next.getKey() + ":" + next.getValue());
if(response.getHits().hits().length == 0) {
break; }}}break;
}
// testResponse(response);
}
/** * Analysis of response results *@param response
*/
public void testResponse(SearchResponse response) {
// The number of records hit
long totalHits = response.getHits().totalHits();
for (SearchHit searchHit : response.getHits()) {
/ / grade
float score = searchHit.getScore();
/ / article id
int id = Integer.parseInt(searchHit.getSource().get("id").toString());
// title
String title = searchHit.getSource().get("title").toString();
/ / content
String content = searchHit.getSource().get("content").toString();
// Update time of the article
long updatetime = Long.parseLong(searchHit.getSource().get("updatetime").toString()); }}/** * Highlight the result */
public void testHighLighted(a) {
* Client.#().#().highlighter(hBuilder).execute().actionget (); HighlightBuilder hBuilder = new HighlightBuilder(); hBuilder.preTags(""); hBuilder.postTags("
"); hBuilder.field("user"); // Set the highlighted field */
// Add to query
SearchResponse response = client.prepareSearch("blog")
.setQuery(QueryBuilders.matchAllQuery())
.addHighlightedField("user") // Add highlighted fields
.setHighlighterPreTags("<h1>")
.setHighlighterPostTags("</h1>")
.execute().actionGet();
// Iterate over the result to get the highlighted fragment
SearchHits searchHits = response.getHits();
for(SearchHit hit:searchHits){
System.out.println("Print document search content in String mode :");
System.out.println(hit.getSourceAsString());
System.out.println("Print highlighted content in Map mode");
System.out.println(hit.getHighlightFields());
System.out.println("Traverse the highlight collection, print the highlight segment :");
Text[] text = hit.getHighlightFields().get("title").getFragments();
for(Text str : text) { System.out.println(str.string()); }}}}Copy the code