2013-01-12 2 views
8

최근에 검색 세계를 탐구하기 시작했으며 MongoDB의 색인으로 ES를 사용하려고합니다. 성공적으로 통합 할 수 있었지만 검색 API가 다소 복잡하고 혼란 스럽습니다. Java API는 그다지 도움이되지 않습니다. 정확한 일치 항목을 찾을 수는 있지만 전체 텍스트 검색은 어떻게합니까?Java API를 사용한 ElasticSearch 전문 검색

Settings settings = ImmutableSettings.settingsBuilder() 
    .put("cluster.name", "elasticsearch").build(); 
Client client = new TransportClient(settings) 
    .addTransportAddress(new InetSocketTransportAddress("host-ip", 9300)); 
SearchResponse response = client.prepareSearch("mongoindex") 
    .setSearchType(SearchType.QUERY_AND_FETCH) 
    .setQuery(termQuery("name", "*name*")) 
    .setFrom(0).setSize(60).setExplain(true) 
    .execute() 
    .actionGet(); 

나는 아무런 문제가 .setQuery(termQuery("name", "testname"))를 사용하여 "name":"testname"를 발견하지,하지만 위의 예와 "name":"this is a test name" 작동하지 않습니다 여기 내 코드입니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

+0

볼 수 있습니다. –

답변

8

인터넷을 몇 시간 동안 크롤링 한 후에 나는 javadocs의 도움을 받아 알아낼 수있었습니다. 가장 중요한 것은 인터페이스 *QueryBuilder*과 구현 클래스입니다. 내 질문에 FieldQueryBuilder을 사용했는데 아래의 setQuery 방법에 나와 있습니다.

SearchResponse response = client.prepareSearch("mongoindex") 
    .setSearchType(SearchType.QUERY_AND_FETCH) 
    .setQuery(fieldQuery("name", "test name")) 
    .setFrom(0).setSize(60).setExplain(true) 
    .execute() 
    .actionGet(); 
SearchHit[] results = response.getHits().getHits(); 
for (SearchHit hit : results) { 
    System.out.println(hit.getId()); //prints out the id of the document 
    Map<String,Object> result = hit.getSource(); //the retrieved document 
} 

결과로 생성 된 Map 개체를 사용하면 get 메서드를 호출하여 관련 데이터를 검색 할 수 있습니다.

0

termQuery in elasticsearch은 검색 구문에 Lucence를 사용합니다. Lucene docs에 따르면 "*"와일드 카드는 검색의 첫 번째 용어로 사용할 수 없습니다.

+0

그럴 경우 어떻게 텍스트 내에서 구문 (또는 하위 문자열)을 검색 할 수 있습니까? –

+0

나는 query_string을 사용하여 http://www.elasticsearch.org/guide/reference/query-dsl/query-string-query.html을 추측 할 것입니다. – mjhm

+0

불행히도, 그것은 Java에 있지 않습니다. 이것은 제 질문의 핵심입니다. 내가 자바 API를 사용하고 있기 때문이다. 링크를 보았지만 자바로 변환 할 수는 없습니다. –

관련 문제