本文整理汇总了Java中org.springframework.data.elasticsearch.core.query.SearchQuery类的典型用法代码示例。如果您正苦于以下问题:Java SearchQuery类的具体用法?Java SearchQuery怎么用?Java SearchQuery使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SearchQuery类属于org.springframework.data.elasticsearch.core.query包,在下文中一共展示了SearchQuery类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: searchCity
import org.springframework.data.elasticsearch.core.query.SearchQuery; //导入依赖的package包/类
@Override
public List<City> searchCity(Integer pageNumber,
Integer pageSize,
String searchContent) {
// 分页参数
Pageable pageable = new PageRequest(pageNumber, pageSize);
// Function Score Query
FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery()
.add(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("cityname", searchContent)),
ScoreFunctionBuilders.weightFactorFunction(1000))
.add(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("description", searchContent)),
ScoreFunctionBuilders.weightFactorFunction(100));
// 创建搜索 DSL 查询
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withPageable(pageable)
.withQuery(functionScoreQueryBuilder).build();
LOGGER.info("\n searchCity(): searchContent [" + searchContent + "] \n DSL = \n " + searchQuery.getQuery().toString());
Page<City> searchPageResults = cityRepository.search(searchQuery);
return searchPageResults.getContent();
}
示例2: fetchReleaseRecord
import org.springframework.data.elasticsearch.core.query.SearchQuery; //导入依赖的package包/类
private CmsRelease fetchReleaseRecord(String nsPath, Date ts, int genTime) throws InterruptedException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(CmsConstants.SEARCH_TS_PATTERN);
Thread.sleep(3000);
SearchQuery latestRelease = new NativeSearchQueryBuilder()
.withIndices("cms-*")
.withTypes("release").withFilter(
FilterBuilders.andFilter(
FilterBuilders.queryFilter(QueryBuilders.termQuery("nsPath.keyword", nsPath)),
FilterBuilders.queryFilter(QueryBuilders.rangeQuery("created").
from(simpleDateFormat.format(DateUtils.addMinutes(ts, -(genTime + 10)))).
to(simpleDateFormat.format(ts))))).
withSort(SortBuilders.fieldSort("created").order(SortOrder.DESC)).build();
List<CmsReleaseSearch> ciList = indexer.getTemplate().queryForList(latestRelease, CmsReleaseSearch.class);
if (!ciList.isEmpty()) {
return ciList.get(0);
}
throw new RuntimeException("Cant find bom release for deployment plan generation event");
}
示例3: getTopicByCateAndUser
import org.springframework.data.elasticsearch.core.query.SearchQuery; //导入依赖的package包/类
@Override
public FacetedPage<Topic> getTopicByCateAndUser(String cate , String q , String user ,final int p , final int ps) {
FacetedPage<Topic> pages = null ;
NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(termQuery("cate" , cate)).withQuery(termQuery("creater" , user)).withSort(new FieldSortBuilder("top").unmappedType("boolean").order(SortOrder.DESC)).withSort(new FieldSortBuilder("updatetime").unmappedType("date").order(SortOrder.DESC));
if(!StringUtils.isBlank(q)){
searchQueryBuilder.withQuery(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND)) ;
}
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(new PageRequest(p, ps));
if(elasticsearchTemplate.indexExists(Topic.class)){
if(!StringUtils.isBlank(q)){
pages = elasticsearchTemplate.queryForPage(searchQuery, Topic.class, new UKResultMapper());
}else{
pages = elasticsearchTemplate.queryForPage(searchQuery, Topic.class);
}
}
return pages ;
}
示例4: searchCity
import org.springframework.data.elasticsearch.core.query.SearchQuery; //导入依赖的package包/类
@Override
public List<City> searchCity(Integer pageNumber, Integer pageSize, String searchContent) {
// 校验分页参数
if (pageSize == null || pageSize <= 0) {
pageSize = PAGE_SIZE;
}
if (pageNumber == null || pageNumber < DEFAULT_PAGE_NUMBER) {
pageNumber = DEFAULT_PAGE_NUMBER;
}
LOGGER.info("\n searchCity: searchContent [" + searchContent + "] \n ");
// 构建搜索查询
SearchQuery searchQuery = getCitySearchQuery(pageNumber,pageSize,searchContent);
LOGGER.info("\n searchCity: searchContent [" + searchContent + "] \n DSL = \n " + searchQuery.getQuery().toString());
Page<City> cityPage = cityRepository.search(searchQuery);
return cityPage.getContent();
}
示例5: getCitySearchQuery
import org.springframework.data.elasticsearch.core.query.SearchQuery; //导入依赖的package包/类
/**
* 根据搜索词构造搜索查询语句
*
* 代码流程:
* - 权重分查询
* - 短语匹配
* - 设置权重分最小值
* - 设置分页参数
*
* @param pageNumber 当前页码
* @param pageSize 每页大小
* @param searchContent 搜索内容
* @return
*/
private SearchQuery getCitySearchQuery(Integer pageNumber, Integer pageSize,String searchContent) {
// 短语匹配到的搜索词,求和模式累加权重分
// 权重分查询 https://www.elastic.co/guide/cn/elasticsearch/guide/current/function-score-query.html
// - 短语匹配 https://www.elastic.co/guide/cn/elasticsearch/guide/current/phrase-matching.html
// - 字段对应权重分设置,可以优化成 enum
// - 由于无相关性的分值默认为 1 ,设置权重分最小值为 10
FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery()
.add(QueryBuilders.matchPhraseQuery("name", searchContent),
ScoreFunctionBuilders.weightFactorFunction(1000))
.add(QueryBuilders.matchPhraseQuery("description", searchContent),
ScoreFunctionBuilders.weightFactorFunction(500))
.scoreMode(SCORE_MODE_SUM).setMinScore(MIN_SCORE);
// 分页参数
Pageable pageable = new PageRequest(pageNumber, pageSize);
return new NativeSearchQueryBuilder()
.withPageable(pageable)
.withQuery(functionScoreQueryBuilder).build();
}
示例6: processNotificationMsg
import org.springframework.data.elasticsearch.core.query.SearchQuery; //导入依赖的package包/类
/**
* Update ops notifications to CIs
*
* @param notificationMsg notification message to process
*/
private void processNotificationMsg(CmsNotificationSearch notificationMsg) {
String id = String.valueOf(notificationMsg.getCmsId());
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withIndices(indexer.getIndexName())
.withTypes("ci").withQuery(queryStringQuery(id).field("ciId"))
.build();
List<CmsCISearch> ciList = indexer.getTemplate().queryForList(searchQuery, CmsCISearch.class);
if (!ciList.isEmpty()) {
CmsCISearch ciSearch = ciList.get(0);
if ("bom.Compute".equals(ciSearch.getCiClassName())) {
String hypervisor = ciSearch.getCiAttributes().get("hypervisor");
if (hypervisor != null) {
notificationMsg.setHypervisor(hypervisor);
}
}
ciSearch.setOps(notificationMsg);
indexer.index(id, "ci", GSON_ES.toJson(ciSearch));
logger.info("updated ops notification for ci id::" + id);
} else {
logger.warn("ci record not found for id::" + id);
}
}
示例7: findByOptimal
import org.springframework.data.elasticsearch.core.query.SearchQuery; //导入依赖的package包/类
@Override
public List<TopicComment> findByOptimal(String dataid) {
List<TopicComment> commentList = null ;
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(termQuery("dataid" , dataid)).withQuery(termQuery("optimal" , true)).build();
if(elasticsearchTemplate.indexExists(TopicComment.class)){
commentList = elasticsearchTemplate.queryForList(searchQuery, TopicComment.class);
}
return commentList ;
}
示例8: findByCon
import org.springframework.data.elasticsearch.core.query.SearchQuery; //导入依赖的package包/类
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public FacetedPage<Serializable> findByCon(Class clazz,NativeSearchQueryBuilder searchQueryBuilder, String q , final int p , final int ps) {
FacetedPage<Serializable> pages = null ;
if(!StringUtils.isBlank(q)){
searchQueryBuilder.withQuery(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND)) ;
}
SearchQuery searchQuery = searchQueryBuilder.build();
if(elasticsearchTemplate.indexExists(clazz)){
if(!StringUtils.isBlank(q)){
pages = elasticsearchTemplate.queryForPage(searchQuery, clazz, new UKResultMapper());
}else{
pages = elasticsearchTemplate.queryForPage(searchQuery, clazz , new UKAggTopResultExtractor("userid" , "top"));
}
}
return pages ;
}
示例9: findByCon
import org.springframework.data.elasticsearch.core.query.SearchQuery; //导入依赖的package包/类
@Override
public FacetedPage<Topic> findByCon(NativeSearchQueryBuilder searchQueryBuilder, String q , final int p , final int ps) {
FacetedPage<Topic> pages = null ;
if(!StringUtils.isBlank(q)){
searchQueryBuilder.withQuery(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND)) ;
}
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(new PageRequest(p, ps));
if(elasticsearchTemplate.indexExists(Topic.class)){
if(!StringUtils.isBlank(q)){
pages = elasticsearchTemplate.queryForPage(searchQuery, Topic.class , new UKResultMapper());
}else{
pages = elasticsearchTemplate.queryForPage(searchQuery, Topic.class);
}
}
return pages ;
}
示例10: searchCity
import org.springframework.data.elasticsearch.core.query.SearchQuery; //导入依赖的package包/类
@Override
public List<City> searchCity(Integer pageNumber,
Integer pageSize,
String searchContent) {
// 分页参数
Pageable pageable = new PageRequest(pageNumber, pageSize);
// Function Score Query
FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery()
.add(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("cityname", searchContent)),
ScoreFunctionBuilders.weightFactorFunction(1000))
.add(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("description", searchContent)),
ScoreFunctionBuilders.weightFactorFunction(100));
// 创建搜索 DSL 查询
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withPageable(pageable)
.withQuery(functionScoreQueryBuilder).build();
LOGGER.info("\n searchCity(): searchContent [" + searchContent + "] \n DSL = \n " + searchQuery.getQuery().toString());
Page<City> searchPageResults = cityRepository.search(searchQuery);
return searchPageResults.getContent();
}
示例11: testForElasticsearchTemplate
import org.springframework.data.elasticsearch.core.query.SearchQuery; //导入依赖的package包/类
@Test
public void testForElasticsearchTemplate() throws Exception {
QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
SearchQuery searchQuery = new NativeSearchQuery(
QueryBuilders.matchAllQuery()
);
elasticsearchTemplate.queryForList(searchQuery, GeoBoundingBox.class);
}
示例12: findWithHighlightedSummary
import org.springframework.data.elasticsearch.core.query.SearchQuery; //导入依赖的package包/类
@Override
public Page<File> findWithHighlightedSummary(Pageable pageable, String query, List<String> version, List<String> project) {
//QueryBuilder searchQuery = Queries.constructQuery(query);
//return elasticsearchTemplate.queryForPage(new NativeSearchQuery(searchQuery), File.class, new ResultHighlightMapper());
NativeSearchQueryBuilder nativeQuery = Queries.constructQueryWithHighlight(query, pageable, 3);
BoolQueryBuilder ensembleVersion = QueryBuilders.boolQuery();
BoolQueryBuilder ensembleProjet = QueryBuilders.boolQuery();
if (version != null && !version.isEmpty()) {
ensembleVersion = ensembleVersion.should(QueryBuilders.termsQuery("version.raw", version));
}
if (project != null && !project.isEmpty()) {
ensembleProjet = ensembleProjet.should(QueryBuilders.termsQuery("project.raw", project));
}
nativeQuery = nativeQuery.withFilter(QueryBuilders.boolQuery().must(ensembleVersion).must(ensembleProjet));
log.debug("query : {}", nativeQuery.toString());
SearchQuery searchQuery = nativeQuery.build();
log.debug("query : {}", searchQuery.getQuery());
log.debug("filter: {}", searchQuery.getFilter());
return elasticsearchTemplate.queryForPage(searchQuery, File.class, new ResultHighlightMapper());
// SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();
}
示例13: shouldReturnAggregatedResponseForGivenSearchQuery
import org.springframework.data.elasticsearch.core.query.SearchQuery; //导入依赖的package包/类
@Test
public void shouldReturnAggregatedResponseForGivenSearchQuery() {
// given
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(matchAllQuery())
.withSearchType(QUERY_THEN_FETCH)
.withPageable(PageRequest.of(0, 1))
.withIndices("articles").withTypes("article")
.addAggregation(terms("subjects").field("subject"))
.build();
// when
AggregatedPage<ArticleEntity> result = elasticsearchTemplate.queryForPage(searchQuery, ArticleEntity.class);
// then
TermsAggregation subjectAgg = result.getAggregation("subjects", TermsAggregation.class);
assertThat(subjectAgg, is(notNullValue()));
assertThat(subjectAgg, is(notNullValue()));
assertThat(subjectAgg.getBuckets().get(0).getKey(), is("computing"));
assertThat(subjectAgg.getBuckets().get(0).getCount(), is(3L));
assertThat(subjectAgg.getBuckets().get(1).getKey(), is("accounting"));
assertThat(subjectAgg.getBuckets().get(1).getCount(), is(1L));
}
示例14: findAll
import org.springframework.data.elasticsearch.core.query.SearchQuery; //导入依赖的package包/类
@Override
public Iterable<T> findAll(Iterable<ID> ids) {
Assert.notNull(ids, "ids can't be null.");
SearchQuery query = new NativeSearchQueryBuilder() //
.withQuery(and(idsQuery().ids(idsArray(ids)), aclFilter())) //
.build();
return elasticsearchOperations.queryForList(query, getEntityClass());
}
示例15: findPropertiesByLocation
import org.springframework.data.elasticsearch.core.query.SearchQuery; //导入依赖的package包/类
@Override
public List<Property> findPropertiesByLocation(final GeoPoint geoPoint, Double distance, final SortOrder sortOrder) {
GeoDistanceFilterBuilder filter = FilterBuilders.geoDistanceFilter("location").point(geoPoint.getLat(), geoPoint.getLon())
.distance(distance, DistanceUnit.KILOMETERS);
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withFilter(filter)
.withSort(
SortBuilders.geoDistanceSort("location").point(geoPoint.getLat(), geoPoint.getLon())
.order(sortOrder == null ? SortOrder.ASC : sortOrder)).build();
searchQuery.addIndices("searchahouse");
searchQuery.addTypes("property");
List<Property> properties = this.elasticsearchOperations.queryForList(searchQuery, Property.class);
return properties;
}