我正在使用scala(2.12)通过高级Java rest客户端(6.5)查询Elasticsearch(6.5)。
libraryDependencies += "org.elasticsearch" % "elasticsearch" % "6.5.4"
libraryDependencies += "org.elasticsearch.client" % "elasticsearch-rest-high-level-client" % "6.5.4"
基本查询(matchQuery)和滚动对我来说效果很好。
val credentialsProvider = new BasicCredentialsProvider
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("user", "password"))
val builder = RestClient.builder(new HttpHost("host", port, "https"))
builder.setHttpClientConfigCallback(new HttpClientConfigCallback() {
override def customizeHttpClient(httpClientBuilder: HttpAsyncClientBuilder): HttpAsyncClientBuilder = httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)
})
var client = new RestHighLevelClient(builder)
val scroll = new Scroll(TimeValue.timeValueMinutes(1L))
// Index
val searchRequest = new SearchRequest("index")
val searchSourceBuilder = new SearchSourceBuilder()
searchSourceBuilder.query(QueryBuilders.matchQuery("name.field", "value"))
searchSourceBuilder.size(500)
searchRequest.source(searchSourceBuilder)
searchRequest.scroll(scroll)
var searchResponse = client.search(searchRequest, RequestOptions.DEFAULT)
var scrollId = searchResponse.getScrollId
var searchHits = searchResponse.getHits().getHits
while (searchHits != null && searchHits.length > 0) {
val scrollRequest = new SearchScrollRequest(scrollId)
scrollRequest.scroll(scroll)
searchResponse = client.scroll(scrollRequest, RequestOptions.DEFAULT)
println(searchResponse.getHits.getHits.toList)
scrollId = searchResponse.getScrollId
searchHits = searchResponse.getHits.getHits
}
基本上上述查询对所有返回的数据均按预期工作。
现在,我的情况是我需要正则表达式(而不是mathQuery)。
所以我只更改了这一行;但它现在什么也没拿。
searchSourceBuilder.query(QueryBuilders.regexpQuery("name.field", "value"))
我在这里想念什么?如何使用Regexp?