Home > Back-end >  Elasticsearch query for less than the total ChongXie for help
Elasticsearch query for less than the total ChongXie for help

Time:10-08

Public void search3 (Book Book, int pagesize, int page) throws IOException {
ElasticsearchConfiguration esc=new ElasticsearchConfiguration ();
Esc. BuildClient ();
RestHighLevelClient client=esc. GetRestHighLevelClient ();
SearchSourceBuilder sourceBuilder=new SearchSourceBuilder ();
//
BoolQueryBuilder BoolQueryBuilder=QueryBuilders. BoolQuery ();
//
MatchPhraseQueryBuilder MatchPhraseQueryBuilder=QueryBuilders. MatchPhraseQuery (" status ", 0).
BoolQueryBuilder. Must (matchPhraseQueryBuilder);
//
RangeQueryBuilder RangeQueryBuilder=QueryBuilders. RangeQuery (" publishTime ");
RangeQueryBuilder. Gte (" the 2020-02-06 21:28:22 ");
RangeQueryBuilder. Lte (" the 2020-02-31 17:12:10 ");
BoolQueryBuilder. Must (rangeQueryBuilder);
////
//WildcardQueryBuilder qb=QueryBuilders. WildcardQuery (" title ",
//"* you * 2");
//boolQueryBuilder. Must (qb);
//
SourceBuilder. The from (0);
SourceBuilder. Size (2);//get record number, the default 10
//
SourceBuilder. Query (boolQueryBuilder);
SourceBuilder. Sort (" publishTime ", SortOrder. DESC);
The SearchRequest SearchRequest=new SearchRequest (EsConsts2 INDEX_NAME);
The searchRequest. Source (sourceBuilder);
The SearchResponse response=client. The search (searchRequest);
System. The out. Println (" search: "+ JSON. ToJSONString (response));
SearchHits hits=response. GetHits ();
SearchHit [] searchHits=hits. GetHits ();
System. Out. Println (" * * * * * * * * * * * * * * the size * * * * * * * * * * * * * * * * : "+ hits, totalHits);
//

For (SearchHit hit: searchHits) {
System. Out.println (" info - & gt; "+ hit. GetSourceAsString ());
}
Client. The close ();
Esc. Destroy ();
}

can get record but is always total of 0

search: {"took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":0,"max_score":null,"hits":[{"_index":"mylib","_type":"_doc","_id":"2","_score":null,"_source":{"number":"2","price":100,"province":"南京1000000","publishTime":"2020-02-07 19:47:41","status":0,"title":"你1000000"},"sort":[1581104861000]},{"_index":"mylib","_type":"_doc","_id":"164","_score":null,"_source":{"number":"164","price":100,"province":"南京164","publishTime":"2020-02-06 21:28:27","status":0,"title":"你164"},"sort":[1581024507000]}]}}


Seek warrior advice total yao sample to get the total


CodePudding user response:

The client is written such
Package com.wpool.com mon. Test. Es.

import org.apache.http.HttpHost;
The import org. Apache. HTTP. Auth. AuthScope;
The import org. Apache. HTTP. Auth. UsernamePasswordCredentials;
The import org. Apache. HTTP. Client. CredentialsProvider;
The import org. Apache. HTTP. Impl. Client. BasicCredentialsProvider;
The import org. Apache. HTTP. Impl. Nio. Client. HttpAsyncClientBuilder;
The import org. Elasticsearch. Client. RestClient;
The import org. Elasticsearch. Client. RestClientBuilder;
The import org. Elasticsearch. Client. RestHighLevelClient;
The import org. Slf4j. Logger;
The import org. Slf4j. LoggerFactory;
The import org. Springframework. Beans. Factory. DisposableBean;
The import org. Springframework. Beans. Factory. FactoryBean;
The import org. Springframework. Beans. Factory. InitializingBean;

Public class ElasticsearchConfiguration implements FactoryBean , InitializingBean DisposableBean {
Private final static Logger Logger=LoggerFactory. GetLogger (ElasticsearchConfiguration. Class);
Private static final String host="127.0.0.1";

Private static final int port=9200;

Private static final String username="";

Private static final String password="";

Private RestHighLevelClient RestHighLevelClient;

Public RestHighLevelClient getRestHighLevelClient () {
Return restHighLevelClient;
}

Public void setRestHighLevelClient (RestHighLevelClient RestHighLevelClient) {
Enclosing restHighLevelClient=restHighLevelClient;
}

Public void destroy () {
Try {
If (restHighLevelClient!=null) {
RestHighLevelClient. Close ();
}
} the catch (final Exception e) {
Logger. The info (um participant etMessage ());
}
}

Public RestHighLevelClient getObject () throws the Exception {
Return restHighLevelClient;
}

Public Class GetObjectType () {
Return RestHighLevelClient. Class;
}

Public Boolean isSingleton () {
return false;
}

Public void afterPropertiesSet () throws the Exception {
BuildClient ();
}

Public void buildClient () {
Final CredentialsProvider CredentialsProvider=new BasicCredentialsProvider ();
CredentialsProvider. SetCredentials (AuthScope. ANY new UsernamePasswordCredentials (username, password));
RestClientBuilder builder=RestClient. Builder (new HttpHost (host, port))
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related