분산 검색 Elasticsearch-QueryBuilders.matchAllQuery
4
/**
* A query that match on all documents.
*/
public static MatchAllQueryBuilder matchAllQuery() {
return new MatchAllQueryBuilder();
}
모든 Document의Query, 다음은 matchAllQuery의 예와 일반적인 장면의 코드입니다.
/**
* @author Geloin
*/
package com.gsoft.gcrsearch.util;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import junit.framework.Assert;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gsoft.gcrsearch.entity.Person;
/**
* @author Geloin
*
*/
public class QueryBuildersTest {
private static Client client;
private static ObjectMapper mapper;
@BeforeClass
public static void beforeClass() {
System.out.println("============== beforeClass()");
NodeBuilder builder = NodeBuilder.nodeBuilder();
String clusterName = PropertyManager.getContextProperty("cluster.name");
builder.clusterName(clusterName);
Node node = builder.node();
client = node.client();
mapper = new ObjectMapper();
}
@Before
public void beforeMethod() throws Exception {
System.out.println("============== beforeMethod()");
List<Person> persons = new ArrayList<Person>();
List<IndexRequest> requests = new ArrayList<IndexRequest>();
for (int i = 0; i < 10; i++) {
Person person = new Person();
person.setAge(20 + i);
person.setId(UUID.randomUUID().toString());
person.setIsStudent(true);
person.setName(" " + i);
person.setSex(" ");
persons.add(person);
String index = "user"; //
String type = "tb_person" + i; //
String json = mapper.writeValueAsString(person);
IndexRequest request = client
.prepareIndex(index, type, person.getId()).setSource(json)
.request();
requests.add(request);
}
BulkRequestBuilder bulkRequest = client.prepareBulk();
for (IndexRequest request : requests) {
bulkRequest.add(request);
}
BulkResponse bulkResponse = bulkRequest.execute().actionGet();
if (bulkResponse.hasFailures()) {
Assert.fail(" !");
}
}
/**
* :
*
* @author Geloin
* @throws Exception
*/
@Test
public void matchAllQuery() throws Exception {
QueryBuilder builder = QueryBuilders.matchAllQuery();
SearchResponse response = client
.prepareSearch("user")
.setTypes("tb_person0", "tb_person1", "tb_person2",
"tb_person3", "tb_person4")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(builder) // Query
.setFilter(FilterBuilders.rangeFilter("age").from(20).to(22)) // Filter
.setFrom(0).setSize(60).setExplain(true).execute().actionGet();
SearchHits hits = response.getHits();
for (SearchHit hit : hits) {
String json = hit.getSourceAsString();
Person newPerson = mapper.readValue(json, Person.class);
System.out.println("name\t\t" + newPerson.getName());
System.out.println("sex\t\t" + newPerson.getSex());
System.out.println("age\t\t" + newPerson.getAge());
System.out.println("isStudent\t\t" + newPerson.getIsStudent());
}
}
/**
* : ( )Index、 ( )Type
* @author Geloin
* @throws Exception
*/
@Test
public void matchAllQueryForCount() throws Exception {
QueryBuilder builder = QueryBuilders.matchAllQuery();
long count = client.prepareCount("user")
.setQuery(builder)
.setTypes("tb_person0", "tb_person1", "tb_person2",
"tb_person3", "tb_person4").execute().actionGet()
.count();
Assert.assertTrue(count >= 3);
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.