我有数据结构:
{
first_name: Ann,
last_name: Smith
}
我想搜索记录使用查询:
我尝试使用Java代码进行搜索:
Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "elasticsearch").build();
TransportClient transportClient = new TransportClient(settings);
transportClient = transportClient.addTransportAddress(new InetSocketTransportAddress("localhost", 9300));
Client client = transportClient;
SearchRequestBuilder srb1 = client.prepareSearch().setQuery(QueryBuilders.matchQuery("firstName", firstName));
SearchRequestBuilder srb2 = client.prepareSearch().setQuery(QueryBuilders.matchQuery("lastName", lastName));
MultiSearchResponse query = client.prepareMultiSearch()
.add(srb1)
.add(srb2)
.execute().actionGet();
for (MultiSearchResponse.Item item : query.getResponses()) {
SearchResponse response = item.getResponse();
System.out.println(response);
}
transportClient.close();
client.close();
但我没有好结果。而ElasticSearch Java API文档非常少。
答案 0 :(得分:1)
我认为你需要这个:
SearchRequestBuilder srb1 = client.prepareSearch().setQuery(QueryBuilders
.boolQuery()
.must(QueryBuilders.termQuery("first_name", "Ann"))
.must(QueryBuilders.termQuery("last_name", "Smith")));
以上查询会为您提供first_name = Ann AND last_name = Smith
SearchRequestBuilder srb2 = client.prepareSearch().setQuery(QueryBuilders
.boolQuery()
.should(QueryBuilders.termQuery("first_name", "Ann"))
.should(QueryBuilders.termQuery("last_name", "Smith")));
以上查询会为您提供first_name = Ann OR last_name = Smith
SearchRequestBuilder srb3 = client.prepareSearch().setQuery(QueryBuilders
.boolQuery()
.must(QueryBuilders.wildcardQuery("first_name", "n*"))
.must(QueryBuilders.wildcardQuery("last_name", "mi*")));
上面的查询将给出`first_name以n开头的结果以及以mi
开头的last_name BTW prepareMultiSearch
仅用于使用单个请求向ES发送多个搜索请求(通信开销较少)。
请看一下:http://search-lucene.com/。您可以找到大量有关Elasticsearch,Lucene,Solr和相关项目的有用信息。