本文整理汇总了Java中org.elasticsearch.search.builder.SearchSourceBuilder类的典型用法代码示例。如果您正苦于以下问题:Java SearchSourceBuilder类的具体用法?Java SearchSourceBuilder怎么用?Java SearchSourceBuilder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SearchSourceBuilder类属于org.elasticsearch.search.builder包,在下文中一共展示了SearchSourceBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test
import org.elasticsearch.search.builder.SearchSourceBuilder; //导入依赖的package包/类
/**
* Here to establish QueryBuilder patterns
*/
void test() {
SearchSourceBuilder b;
BoolQueryBuilder qb = QueryBuilders.boolQuery();
QueryBuilder qbmm = QueryBuilders.multiMatchQuery("name", "fieldNames");
QueryBuilder qbm = QueryBuilders.matchQuery("name", "text");
QueryBuilder qbmp = QueryBuilders.matchPhraseQuery("name", "text");
QueryBuilder qb1 = QueryBuilders.termQuery(ITQCoreOntology.TUPLE_SUBJECT_PROPERTY, "");
QueryBuilder qb2 = QueryBuilders.termQuery(ITQCoreOntology.TUPLE_OBJECT_PROPERTY, "");
QueryBuilder qb3 = QueryBuilders.termQuery(ITQCoreOntology.INSTANCE_OF_PROPERTY_TYPE, "");
qb.must(qb3);
qb.should(qb1);
qb.should(qb2);
QueryBuilder partial = QueryBuilders.regexpQuery("name", "regexp");
QueryBuilder wildcard = QueryBuilders.wildcardQuery("name", "query");
}
示例2: selectTermAll
import org.elasticsearch.search.builder.SearchSourceBuilder; //导入依赖的package包/类
public String selectTermAll(String indexs,String types,String field,String value){
try {
if(StringUtil.isEmpty(indexs))indexs="_all";
if(xclient==null){
init();
}
SearchSourceBuilder search = new SearchSourceBuilder();
if(!StringUtil.isEmpty(field)&&!StringUtil.isEmpty(value)&&!(field.matches(regex)||field.matches(value))){
search.query(QueryBuilders.termQuery(field, value));
}
search.aggregation(AggregationBuilders.terms("data").field(field+".keyword"));
search.explain(false);
SearchRequest request = new SearchRequest();
request.searchType(SearchType.DFS_QUERY_THEN_FETCH);
request.source(search);
request.indices(indexs.split(","));
request.types(types.split(","));
SearchResponse response = xclient.search(request);
return response.toString();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
示例3: elasticSearch
import org.elasticsearch.search.builder.SearchSourceBuilder; //导入依赖的package包/类
private static void elasticSearch(JestClient client, String indexName, String type, String query) {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
QueryStringQueryBuilder queryStringQueryBuilder = QueryBuilders.queryStringQuery(query);
searchSourceBuilder.query(queryStringQueryBuilder);
Search searchElastic = (Search) new Search.Builder(searchSourceBuilder.toString())
// multiple index or types can be added.
.addIndex(indexName).addType(type).build();
try {
SearchResult elasticRs = client.execute(searchElastic);
List<Hit<Article, Void>> hits = elasticRs.getHits(Article.class);
for (Hit<Article, Void> hit : hits) {
Article talk = hit.source;
System.out.println("Elastic hits: " + talk.toString());
}
} catch (IOException e) {
e.printStackTrace();
}
}
示例4: DfsOnlyRequest
import org.elasticsearch.search.builder.SearchSourceBuilder; //导入依赖的package包/类
public DfsOnlyRequest(Fields termVectorsFields, String[] indices, String[] types, Set<String> selectedFields) throws IOException {
super(indices);
// build a search request with a query of all the terms
final BoolQueryBuilder boolBuilder = boolQuery();
for (String fieldName : termVectorsFields) {
if ((selectedFields != null) && (!selectedFields.contains(fieldName))) {
continue;
}
Terms terms = termVectorsFields.terms(fieldName);
TermsEnum iterator = terms.iterator();
while (iterator.next() != null) {
String text = iterator.term().utf8ToString();
boolBuilder.should(QueryBuilders.termQuery(fieldName, text));
}
}
// wrap a search request object
this.searchRequest = new SearchRequest(indices).types(types).source(new SearchSourceBuilder().query(boolBuilder));
}
示例5: testGetByModulePackage
import org.elasticsearch.search.builder.SearchSourceBuilder; //导入依赖的package包/类
@Test
public void testGetByModulePackage() throws Exception {
SearchResult result = client.execute(new Search.Builder(new SearchSourceBuilder()
.size(1)
.query(QueryBuilders.matchQuery("modulePackage", "testing-mod")).toString())
.addIndex(AddOnInfoAndVersions.ES_INDEX)
.build());
SearchResult resultNotFound = client.execute(new Search.Builder(new SearchSourceBuilder()
.size(1)
.query(QueryBuilders.matchQuery("modulePackage", "fake-mod")).toString())
.addIndex(AddOnInfoAndVersions.ES_INDEX)
.build());
List<AddOnInfoAndVersions> searchResult = result.getHits(AddOnInfoAndVersions.class).stream()
.map(sr -> sr.source)
.collect(Collectors.toList());
List<AddOnInfoAndVersions> searchResultNotFound = resultNotFound.getHits(AddOnInfoAndVersions.class).stream()
.map(sr -> sr.source)
.collect(Collectors.toList());
assertNotNull(searchResult);
assertThat(searchResultNotFound, IsEmptyCollection.empty());
System.out.println("Module Package Name: "+searchResult.get(0).getModulePackage());
}
示例6: testPostingsHighlighterFuzzyQuery
import org.elasticsearch.search.builder.SearchSourceBuilder; //导入依赖的package包/类
public void testPostingsHighlighterFuzzyQuery() throws Exception {
assertAcked(prepareCreate("test").addMapping("type1", type1PostingsffsetsMapping()));
ensureGreen();
client().prepareIndex("test", "type1")
.setSource("field1", "this is a test", "field2", "The quick brown fox jumps over the lazy dog! Second sentence.").get();
refresh();
for (String type : UNIFIED_AND_NULL) {
logger.info("--> highlighting and searching on field2");
SearchSourceBuilder source = searchSource().query(fuzzyQuery("field2", "quck"))
.highlighter(highlight().field("field2").highlighterType(type));
SearchResponse searchResponse = client().prepareSearch("test").setSource(source).get();
assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The <em>quick</em> brown fox jumps over the lazy dog!"));
}
}
示例7: selectMatchAll
import org.elasticsearch.search.builder.SearchSourceBuilder; //导入依赖的package包/类
public String selectMatchAll(String indexs,String types,String field,String value){
try {
if(StringUtil.isEmpty(indexs))indexs="_all";
if(xclient==null){
init();
}
SearchSourceBuilder search = new SearchSourceBuilder();
if(!StringUtil.isEmpty(field)&&!StringUtil.isEmpty(value)&&!(field.matches(regex)||field.matches(value))){
search.query(QueryBuilders.matchQuery(field, value));
}
search.aggregation(AggregationBuilders.terms("data").field(field+".keyword"));
search.explain(false);
SearchRequest request = new SearchRequest();
request.searchType(SearchType.DFS_QUERY_THEN_FETCH);
request.source(search);
request.indices(indexs.split(","));
request.types(types.split(","));
SearchResponse response = xclient.search(request);
return response.toString();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
示例8: fullTextQuerySingleField
import org.elasticsearch.search.builder.SearchSourceBuilder; //导入依赖的package包/类
public String fullTextQuerySingleField(String textQuery, boolean isPhraseQuery,
String fieldName, int start, int count) {
QueryBuilder qb;
if (!isPhraseQuery)
qb = QueryBuilders.matchQuery(fieldName, textQuery);
else
qb = QueryBuilders.matchPhraseQuery(fieldName, textQuery);
SearchSourceBuilder ssb = new SearchSourceBuilder();
ssb.query(qb);
ssb.from(start);
if (count > -1)
ssb.size(count);
return ssb.toString();
}
示例9: fullTextQueryMultipleFields
import org.elasticsearch.search.builder.SearchSourceBuilder; //导入依赖的package包/类
public String fullTextQueryMultipleFields(String textQuery, boolean isPhraseQuery,
List<String> fieldNames, int start, int count) {
BoolQueryBuilder qb = QueryBuilders.boolQuery();
String fn;
Iterator<String>itr = fieldNames.iterator();
while (itr.hasNext()) {
fn = itr.next();
if (!isPhraseQuery)
qb.should(QueryBuilders.matchQuery(fn, textQuery));
else
qb.should(QueryBuilders.matchPhraseQuery(fn, textQuery));
}
SearchSourceBuilder ssb = new SearchSourceBuilder();
ssb.query(qb);
ssb.from(start);
if (count > -1)
ssb.size(count);
return ssb.toString();
}
示例10: setUp
import org.elasticsearch.search.builder.SearchSourceBuilder; //导入依赖的package包/类
@Before
@Override
public void setUp() throws Exception {
super.setUp();
final ExecutorService directExecutor = EsExecutors.newDirectExecutorService();
threadPool = new TestThreadPool(getTestName()) {
@Override
public ExecutorService executor(String name) {
return directExecutor;
}
@Override
public ScheduledFuture<?> schedule(TimeValue delay, String name, Runnable command) {
command.run();
return null;
}
};
retries = 0;
searchRequest = new SearchRequest();
searchRequest.scroll(timeValueMinutes(5));
searchRequest.source(new SearchSourceBuilder().size(10).version(true).sort("_doc").size(123));
retriesAllowed = 0;
}
示例11: testInitialSearchParamsFields
import org.elasticsearch.search.builder.SearchSourceBuilder; //导入依赖的package包/类
public void testInitialSearchParamsFields() {
SearchRequest searchRequest = new SearchRequest().source(new SearchSourceBuilder());
// Test request without any fields
Version remoteVersion = VersionUtils.randomVersion(random());
assertThat(initialSearchParams(searchRequest, remoteVersion),
not(either(hasKey("stored_fields")).or(hasKey("fields"))));
// Setup some fields for the next two tests
searchRequest.source().storedField("_source").storedField("_id");
// Test stored_fields for versions that support it
remoteVersion = VersionUtils.randomVersionBetween(random(), Version.V_5_0_0_alpha4, null);
assertThat(initialSearchParams(searchRequest, remoteVersion), hasEntry("stored_fields", "_source,_id"));
// Test fields for versions that support it
remoteVersion = VersionUtils.randomVersionBetween(random(), null, Version.V_5_0_0_alpha3);
assertThat(initialSearchParams(searchRequest, remoteVersion), hasEntry("fields", "_source,_id"));
}
示例12: testInitialSearchParamsMisc
import org.elasticsearch.search.builder.SearchSourceBuilder; //导入依赖的package包/类
public void testInitialSearchParamsMisc() {
SearchRequest searchRequest = new SearchRequest().source(new SearchSourceBuilder());
Version remoteVersion = Version.fromId(between(0, Version.CURRENT.id));
TimeValue scroll = null;
if (randomBoolean()) {
scroll = TimeValue.parseTimeValue(randomPositiveTimeValue(), "test");
searchRequest.scroll(scroll);
}
int size = between(0, Integer.MAX_VALUE);
searchRequest.source().size(size);
Boolean fetchVersion = null;
if (randomBoolean()) {
fetchVersion = randomBoolean();
searchRequest.source().version(fetchVersion);
}
Map<String, String> params = initialSearchParams(searchRequest, remoteVersion);
assertThat(params, scroll == null ? not(hasKey("scroll")) : hasEntry("scroll", scroll.toString()));
assertThat(params, hasEntry("size", Integer.toString(size)));
assertThat(params, fetchVersion == null || fetchVersion == true ? hasEntry("version", null) : not(hasEntry("version", null)));
}
示例13: testInitialSearchEntity
import org.elasticsearch.search.builder.SearchSourceBuilder; //导入依赖的package包/类
public void testInitialSearchEntity() throws IOException {
SearchRequest searchRequest = new SearchRequest();
searchRequest.source(new SearchSourceBuilder());
String query = "{\"match_all\":{}}";
HttpEntity entity = initialSearchEntity(searchRequest, new BytesArray(query));
assertEquals(ContentType.APPLICATION_JSON.toString(), entity.getContentType().getValue());
assertEquals("{\"query\":" + query + ",\"_source\":true}",
Streams.copyToString(new InputStreamReader(entity.getContent(), StandardCharsets.UTF_8)));
// Source filtering is included if set up
searchRequest.source().fetchSource(new String[] {"in1", "in2"}, new String[] {"out"});
entity = initialSearchEntity(searchRequest, new BytesArray(query));
assertEquals(ContentType.APPLICATION_JSON.toString(), entity.getContentType().getValue());
assertEquals("{\"query\":" + query + ",\"_source\":{\"includes\":[\"in1\",\"in2\"],\"excludes\":[\"out\"]}}",
Streams.copyToString(new InputStreamReader(entity.getContent(), StandardCharsets.UTF_8)));
// Invalid XContent fails
RuntimeException e = expectThrows(RuntimeException.class,
() -> initialSearchEntity(searchRequest, new BytesArray("{}, \"trailing\": {}")));
assertThat(e.getCause().getMessage(), containsString("Unexpected character (',' (code 44))"));
e = expectThrows(RuntimeException.class, () -> initialSearchEntity(searchRequest, new BytesArray("{")));
assertThat(e.getCause().getMessage(), containsString("Unexpected end-of-input"));
}
示例14: innerReadFrom
import org.elasticsearch.search.builder.SearchSourceBuilder; //导入依赖的package包/类
protected void innerReadFrom(StreamInput in) throws IOException {
shardId = ShardId.readShardId(in);
searchType = SearchType.fromId(in.readByte());
numberOfShards = in.readVInt();
scroll = in.readOptionalWriteable(Scroll::new);
source = in.readOptionalWriteable(SearchSourceBuilder::new);
types = in.readStringArray();
aliasFilter = new AliasFilter(in);
if (in.getVersion().onOrAfter(Version.V_5_2_0_UNRELEASED)) {
indexBoost = in.readFloat();
} else {
// Nodes < 5.2.0 doesn't send index boost. Read it from source.
if (source != null) {
Optional<SearchSourceBuilder.IndexBoost> boost = source.indexBoosts()
.stream()
.filter(ib -> ib.getIndex().equals(shardId.getIndexName()))
.findFirst();
indexBoost = boost.isPresent() ? boost.get().getBoost() : 1.0f;
} else {
indexBoost = 1.0f;
}
}
nowInMillis = in.readVLong();
requestCache = in.readOptionalBoolean();
}
示例15: parseRequest
import org.elasticsearch.search.builder.SearchSourceBuilder; //导入依赖的package包/类
/**
* Parses a {@link RestRequest} body and returns a {@link MultiSearchRequest}
*/
public static MultiSearchRequest parseRequest(RestRequest restRequest, boolean allowExplicitIndex) throws IOException {
MultiSearchRequest multiRequest = new MultiSearchRequest();
if (restRequest.hasParam("max_concurrent_searches")) {
multiRequest.maxConcurrentSearchRequests(restRequest.paramAsInt("max_concurrent_searches", 0));
}
parseMultiLineRequest(restRequest, multiRequest.indicesOptions(), allowExplicitIndex, (searchRequest, parser) -> {
try {
final QueryParseContext queryParseContext = new QueryParseContext(parser);
searchRequest.source(SearchSourceBuilder.fromXContent(queryParseContext));
multiRequest.add(searchRequest);
} catch (IOException e) {
throw new ElasticsearchParseException("Exception when parsing search request", e);
}
});
return multiRequest;
}