Java Code Examples for org.elasticsearch.index.query.QueryBuilders#termQuery()
The following examples show how to use
org.elasticsearch.index.query.QueryBuilders#termQuery() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DateRangeAggregationMain.java From elasticsearch-pool with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws IOException { RestHighLevelClient client = HighLevelClient.getInstance(); try{ DateRangeAggregationBuilder dateRangeAggregationBuilder = AggregationBuilders.dateRange("date_range"); dateRangeAggregationBuilder.field("ctm"); dateRangeAggregationBuilder.format("yyyy-MM-dd HH:mm:ss"); dateRangeAggregationBuilder.addRange("2018-07-10 00:00:00","2018-07-11 00:00:00"); dateRangeAggregationBuilder.timeZone(DateTimeZone.forOffsetHours(0)); TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("cmd","weather_info"); SearchRequest searchRequest = new SearchRequest("serverlog_20180710");//限定index searchRequest.types("log");//限定type SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(termQueryBuilder); searchSourceBuilder.aggregation(dateRangeAggregationBuilder); searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = client.search(searchRequest); System.out.println(searchResponse); }finally{ HighLevelClient.close(); } }
Example 2
Source File: ESSearchTest.java From summerframework with Apache License 2.0 | 6 votes |
@Test public void constantScoreQuery() throws IOException { queryPair.setFieldNames(new String[] {"productName"}); queryPair.setContent("android"); QueryBuilder qb = QueryBuilders.termQuery(queryPair.getFieldNames()[0], queryPair.getContent()); queryCondition.setQueryBuilder(QueryBuilders.constantScoreQuery(qb).boost(2.0f)); List<Spu> list = elasticsearchTemplate.analyzeSearchResponse(Spu.class, elasticsearchTemplate.executeQuery("es_test", queryCondition, "type")); log.info("json string is:{}", mapper.writeValueAsString(list)); log.info("list size is:{}", list.size()); assertThat(list.size(), is(1)); assertThat(list.get(0), equalTo(spu1)); }
Example 3
Source File: TestTransportClient.java From jframe with Apache License 2.0 | 6 votes |
@Test public void testScrollSearch() { QueryBuilder qb = QueryBuilders.termQuery("multi", "test"); // 100 hits per shard will be returned for each scroll SearchResponse scrollResp = client.prepareSearch("index1").addSort(SortParseElement.DOC_FIELD_NAME, SortOrder.ASC) .setScroll(new TimeValue(60000)).setQuery(qb).setSize(100).execute().actionGet(); // Scroll until no hits are returned while (true) { for (SearchHit hit : scrollResp.getHits().getHits()) { // Handle the hit... } scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(60000)).execute().actionGet(); // Break condition: No hits are returned if (scrollResp.getHits().getHits().length == 0) { break; } } }
Example 4
Source File: EsQueryBuilder.java From es-service-parent with Apache License 2.0 | 6 votes |
/** * 精确匹配处理 * * @param boolQ * @param conditions * @param conditionType */ private void doTerm(BoolQueryBuilder boolQ, List<Condition> conditions, ConditionType conditionType) { QueryBuilder term; BoolQueryBuilder subBoolQ; for (Condition condition : conditions) { if (condition.isSpaceSplit()) { subBoolQ = QueryBuilders.boolQuery(); for (String word : KeyWordUtil.processKeyWord(condition.getValue())) { subBoolQ.should(QueryBuilders.termQuery(condition.getFiled(), word)); } term = subBoolQ; } else { term = QueryBuilders.termQuery(condition.getFiled(), condition.getValue()); } mergeBuilder(boolQ, term, conditionType); } }
Example 5
Source File: IndexTest.java From bigdata-tutorial with Apache License 2.0 | 5 votes |
public void search() throws Exception { try { QueryBuilder qb = QueryBuilders.termQuery("title", "大师"); SearchResponse scrollResp = client.prepareSearch("app").setSearchType(SearchType.SCAN).setScroll( new TimeValue(60000)).setQuery(qb).setSize(100).execute().actionGet(); while (true) { scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(600000)).execute().actionGet(); for (SearchHit hit : scrollResp.getHits()) { Map<String, Object> source = hit.getSource(); if (!source.isEmpty()) { for (Iterator<Map.Entry<String, Object>> it = source.entrySet().iterator(); it.hasNext();) { Map.Entry<String, Object> entry = it.next(); System.out.println(entry.getKey() + "=======" + entry.getValue()); } } } if (scrollResp.getHits().getTotalHits() == 0) { break; } } } catch (Exception e) { e.printStackTrace(); } finally { client.close(); } }
Example 6
Source File: ElasticsearchIndex.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
private QueryBuilder addContextTerm(QueryBuilder qb, Resource ctx) { BoolQueryBuilder combinedQuery = QueryBuilders.boolQuery(); QueryBuilder idQuery = QueryBuilders.termQuery(SearchFields.CONTEXT_FIELD_NAME, SearchFields.getContextID(ctx)); if (ctx != null) { // the specified named graph combinedQuery.must(idQuery); } else { // not the unnamed graph combinedQuery.mustNot(idQuery); } combinedQuery.must(qb); return combinedQuery; }
Example 7
Source File: ApiAdminSearchlistAction.java From fess with Apache License 2.0 | 5 votes |
@Execute public JsonResponse<ApiResult> delete$doc(final String id) { try { final QueryBuilder query = QueryBuilders.termQuery(fessConfig.getIndexFieldDocId(), id); fessEsClient.deleteByQuery(fessConfig.getIndexDocumentUpdateIndex(), query); saveInfo(messages -> messages.addSuccessDeleteDocFromIndex(GLOBAL)); } catch (final Exception e) { throwValidationErrorApi(messages -> messages.addErrorsFailedToDeleteDocInAdmin(GLOBAL)); } return asJson(new ApiResponse().status(Status.OK).result()); }
Example 8
Source File: DateHistogramAggregationMain.java From elasticsearch-pool with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws IOException { RestHighLevelClient client = HighLevelClient.getInstance(); try{ DateHistogramAggregationBuilder dateHistogramAggregationBuilder = AggregationBuilders.dateHistogram("ctm_date_histogram"); dateHistogramAggregationBuilder.field("ctm");//设置直方图针对的字段 dateHistogramAggregationBuilder.dateHistogramInterval(DateHistogramInterval.hours(6));//直方图每个分组对应的范围 dateHistogramAggregationBuilder.timeZone(DateTimeZone.forOffsetHours(8));//时区偏移 dateHistogramAggregationBuilder.keyed(true);//是否需要key名 dateHistogramAggregationBuilder.format("yyyy-MM-dd HH:mm");//key名格式 // dateHistogramAggregationBuilder.order(BucketOrder.aggregation("_key",true));//分组key的排序 // dateHistogramAggregationBuilder.minDocCount(0);//对于每个分组最少具有多少条数据,少于这个设置,则该分组不显示 // dateHistogramAggregationBuilder.extendedBounds(0,8000);//设置分组区间的下线和上线,只有当min_doc_count为0时有效 TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("cmd","weather_info"); SearchRequest searchRequest = new SearchRequest("serverlog_20180710");//限定index searchRequest.types("log");//限定type SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(termQueryBuilder); searchSourceBuilder.aggregation(dateHistogramAggregationBuilder); searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = client.search(searchRequest); System.out.println(searchResponse); }finally{ HighLevelClient.close(); } }
Example 9
Source File: DetectorMappingRepositoryImpl.java From adaptive-alerting with Apache License 2.0 | 5 votes |
@Override public MatchingDetectorsResponse findMatchingDetectorMappings(List<Map<String, String>> tagsList) { try { List<BytesReference> refList = new ArrayList<>(); for (Map<String, String> tags : tagsList) { XContentBuilder xContent = XContentFactory.jsonBuilder(); xContent.map(tags); refList.add(BytesReference.bytes(xContent)); } PercolateQueryBuilder percolateQuery = new PercolateQueryBuilder(PercolatorDetectorMapping.QUERY_KEYWORD, refList, XContentType.JSON); val termQuery = QueryBuilders.termQuery("aa_enabled", true); BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder(); boolQueryBuilder.filter(percolateQuery); boolQueryBuilder.filter(termQuery); SearchSourceBuilder searchSourceBuilder = elasticsearchUtil.getSourceBuilder(boolQueryBuilder); searchSourceBuilder.timeout(new TimeValue(elasticSearchProperties.getConfig().getConnectionTimeout())); //FIXME setting default result set size to 500. // This is for returning detectors matching for set of metrics searchSourceBuilder.size(500); return getDetectorMappings(searchSourceBuilder, tagsList); } catch (IOException e) { log.error("Error ES lookup", e); throw new RuntimeException(e); } }
Example 10
Source File: LegacyDetectorRepositoryImpl.java From adaptive-alerting with Apache License 2.0 | 5 votes |
@Override public DetectorDocument findByUuid(String uuid) { MDC.put("DetectorUuid", uuid); val queryBuilder = QueryBuilders.termQuery("uuid", uuid); val searchSourceBuilder = elasticsearchUtil.getSourceBuilder(queryBuilder).size(DEFAULT_ES_RESULTS_SIZE); val searchRequest = elasticsearchUtil.getSearchRequest(searchSourceBuilder, DETECTOR_INDEX, DETECTOR_DOC_TYPE); val detectors = getDetectorsFromElasticSearch(searchRequest); MDC.remove("DetectorUuid"); return detectors.isEmpty() ? null : detectors.get(0); }
Example 11
Source File: ElasticsearchSearchVertexQuery.java From vertexium with Apache License 2.0 | 5 votes |
private QueryBuilder getDirectionOutEdgeFilter() { QueryBuilder outVertexIdFilter = QueryBuilders.termQuery(Elasticsearch7SearchIndex.OUT_VERTEX_ID_FIELD_NAME, sourceVertex.getId()); if (otherVertexId != null) { QueryBuilder inVertexIdFilter = QueryBuilders.termQuery(Elasticsearch7SearchIndex.IN_VERTEX_ID_FIELD_NAME, otherVertexId); return QueryBuilders.boolQuery() .must(outVertexIdFilter) .must(inVertexIdFilter); } return outVertexIdFilter; }
Example 12
Source File: ElasticsearchSearchVertexQuery.java From vertexium with Apache License 2.0 | 5 votes |
private QueryBuilder getDirectionInEdgeFilter() { QueryBuilder outVertexIdFilter = QueryBuilders.termQuery(Elasticsearch5SearchIndex.IN_VERTEX_ID_FIELD_NAME, sourceVertex.getId()); if (otherVertexId != null) { QueryBuilder inVertexIdFilter = QueryBuilders.termQuery(Elasticsearch5SearchIndex.OUT_VERTEX_ID_FIELD_NAME, otherVertexId); return QueryBuilders.boolQuery() .must(outVertexIdFilter) .must(inVertexIdFilter); } return outVertexIdFilter; }
Example 13
Source File: ElasticsearchAnySearchDAO.java From syncope with Apache License 2.0 | 4 votes |
private static QueryBuilder getQueryBuilder(final ResourceCond cond) { return QueryBuilders.termQuery("resources", cond.getResourceKey()); }
Example 14
Source File: ElasticsearchAnySearchDAO.java From syncope with Apache License 2.0 | 4 votes |
private static QueryBuilder getQueryBuilder(final RelationshipTypeCond cond) { return QueryBuilders.termQuery("relationshipTypes", cond.getRelationshipTypeKey()); }
Example 15
Source File: ElasticsearchCatalogService.java From staccato with Apache License 2.0 | 4 votes |
/** * Produces a list of all unique values for a given field in a collection. * * @param collection The collection that will be queried. * @param path The request path containing property fieldsExtension and values for which to find unique values * @return A list of unique values */ @Override public List<String> getValuesForField(CollectionMetadata collection, List<String> path) { String fieldName = path.get(path.size() - 1); List<String> values = new LinkedList<>(); fieldName = "properties." + fieldName; // build the term aggregation from the last subcatalog property in the url path TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder(fieldName + "_Agg", ValueType.STRING).size(10000); aggregationBuilder.field(fieldName); // the query based on all of the unique subcatalog value previously selected QueryBuilder query = QueryBuilders.boolQuery(); for (int i = 2; i * 2 <= path.size(); i = i + 2) { String property = "properties." + path.get(i); String value = path.get(i + 1); QueryBuilder pathQuery = QueryBuilders.termQuery(property, value); ((BoolQueryBuilder) query).must(pathQuery); } SearchRequest request = new SearchRequest().indices(indexAliasLookup.getReadAlias(collection.getId())) .searchType(SearchType.DFS_QUERY_THEN_FETCH) .source(new SearchSourceBuilder().query(query).aggregation(aggregationBuilder).size(0)); SearchResponse response; try { response = client.search(request, RequestOptions.DEFAULT); } catch (Exception ex) { log.error("Error getting aggregations.", ex); throw new RuntimeException("Error getting aggregations.", ex); } if (response == null) return Collections.EMPTY_LIST; ParsedTerms terms = response.getAggregations().get(fieldName + "_Agg"); if (terms != null) { List<ParsedTerms.ParsedBucket> buckets = (List<ParsedTerms.ParsedBucket>) terms.getBuckets(); if (buckets != null && buckets.size() > 0) { for (ParsedTerms.ParsedBucket bucket : buckets) { values.add(bucket.getKeyAsString()); } } return values; } return Collections.EMPTY_LIST; }
Example 16
Source File: InvokeChainQueryHandler.java From uavstack with Apache License 2.0 | 4 votes |
/** * 已知trace id查所有关联trace * * @param data */ @SuppressWarnings("rawtypes") private void queryByTrace(UAVHttpMessage data) { String traceid = data.getRequest("traceid"); if (StringHelper.isEmpty(traceid)) { data.putResponse("rs", "ERR"); data.putResponse("msg", "No TraceID Found"); return; } QueryBuilder qb = QueryBuilders.termQuery("traceid", traceid); SortBuilder[] sorts = buildSort(data); String indexDate = getIndexDateFromTraceId(traceid); data.putRequest("indexdate", indexDate); queryToList(data, qb, null, sorts); }
Example 17
Source File: ImageIntegrationTests.java From elasticsearch-image with Apache License 2.0 | 4 votes |
@Test public void test_index_search_image() throws Exception { String mapping = copyToStringFromClasspath("/mapping/test-mapping.json"); client().admin().indices().putMapping(putMappingRequest(INDEX_NAME).type(DOC_TYPE_NAME).source(mapping)).actionGet(); int totalImages = randomIntBetween(10, 50); // generate random images and index String nameToSearch = null; byte[] imgToSearch = null; String idToSearch = null; for (int i = 0; i < totalImages; i ++) { byte[] imageByte = getRandomImage(); String name = randomAsciiOfLength(5); IndexResponse response = index(INDEX_NAME, DOC_TYPE_NAME, jsonBuilder().startObject().field("img", imageByte).field("name", name).endObject()); if (nameToSearch == null || imgToSearch == null || idToSearch == null) { nameToSearch = name; imgToSearch = imageByte; idToSearch = response.getId(); } } refresh(); // test search with hash ImageQueryBuilder imageQueryBuilder = new ImageQueryBuilder("img").feature(FeatureEnum.CEDD.name()).image(imgToSearch).hash(HashEnum.BIT_SAMPLING.name()); SearchResponse searchResponse = client().prepareSearch(INDEX_NAME).setTypes(DOC_TYPE_NAME).setQuery(imageQueryBuilder).addFields("img.metadata.exif_ifd0.x_resolution", "name").setSize(totalImages).get(); assertNoFailures(searchResponse); SearchHits hits = searchResponse.getHits(); assertThat("Should match at least one image", hits.getTotalHits(), greaterThanOrEqualTo(1l)); // if using hash, total result maybe different than number of images SearchHit hit = hits.getHits()[0]; assertThat("First should be exact match and has score 1", hit.getScore(), equalTo(2.0f)); assertImageScore(hits, nameToSearch, 2.0f); assertThat("Should have metadata", hit.getFields().get("img.metadata.exif_ifd0.x_resolution").getValues(), hasSize(1)); // test search without hash and with boost ImageQueryBuilder imageQueryBuilder2 = new ImageQueryBuilder("img").feature(FeatureEnum.CEDD.name()).image(imgToSearch).boost(2.0f); SearchResponse searchResponse2 = client().prepareSearch(INDEX_NAME).setTypes(DOC_TYPE_NAME).setQuery(imageQueryBuilder2).setSize(totalImages).get(); assertNoFailures(searchResponse2); SearchHits hits2 = searchResponse2.getHits(); assertThat("Should get all images", hits2.getTotalHits(), equalTo((long)totalImages)); // no hash used, total result should be same as number of images assertThat("First should be exact match and has score 2", searchResponse2.getHits().getMaxScore(), equalTo(4.0f)); assertImageScore(hits2, nameToSearch, 4.0f); // test search for name as well BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); boolQueryBuilder.must(QueryBuilders.termQuery("name", nameToSearch)); boolQueryBuilder.must(new ImageQueryBuilder("img").feature(FeatureEnum.CEDD.name()).image(imgToSearch)); SearchResponse searchResponse3 = client().prepareSearch(INDEX_NAME).setTypes(DOC_TYPE_NAME).setQuery(boolQueryBuilder).setSize(totalImages).get(); assertNoFailures(searchResponse3); SearchHits hits3 = searchResponse3.getHits(); assertThat("Should match one document only", hits3.getTotalHits(), equalTo(1l)); // added filename to query, should have only one result SearchHit hit3 = hits3.getHits()[0]; assertThat((String)hit3.getSource().get("name"), equalTo(nameToSearch)); // test search with hash and limit ImageQueryBuilder imageQueryBuilder4 = new ImageQueryBuilder("img").feature(FeatureEnum.CEDD.name()).image(imgToSearch).hash(HashEnum.BIT_SAMPLING.name()).limit(10); SearchResponse searchResponse4 = client().prepareSearch(INDEX_NAME).setTypes(DOC_TYPE_NAME).setQuery(imageQueryBuilder4).setSize(totalImages).get(); assertNoFailures(searchResponse4); SearchHits hits4 = searchResponse4.getHits(); assertThat("Should match at least one image", hits4.getTotalHits(), greaterThanOrEqualTo(1l)); // if using hash, total result maybe different than number of images SearchHit hit4 = hits4.getHits()[0]; assertThat("First should be exact match and has score 1", hit4.getScore(), equalTo(2.0f)); assertImageScore(hits4, nameToSearch, 2.0f); // test search metadata TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("img.metadata.exif_ifd0.x_resolution", "72 dots per inch"); SearchResponse searchResponse5 = client().prepareSearch(INDEX_NAME).setTypes(DOC_TYPE_NAME).setQuery(termQueryBuilder).setSize(totalImages).get(); assertNoFailures(searchResponse5); SearchHits hits5 = searchResponse5.getHits(); assertThat("Should match at least one record", hits5.getTotalHits(), greaterThanOrEqualTo(1l)); // if using hash, total result maybe different than number of images // test search with exist image ImageQueryBuilder imageQueryBuilder6 = new ImageQueryBuilder("img").feature(FeatureEnum.CEDD.name()).lookupIndex(INDEX_NAME).lookupType(DOC_TYPE_NAME).lookupId(idToSearch).lookupPath("img"); SearchResponse searchResponse6 = client().prepareSearch(INDEX_NAME).setTypes(DOC_TYPE_NAME).setQuery(imageQueryBuilder6).setSize(totalImages).get(); assertNoFailures(searchResponse6); SearchHits hits6 = searchResponse6.getHits(); assertThat("Should match at least one image", hits6.getTotalHits(), equalTo((long) totalImages)); SearchHit hit6 = hits6.getHits()[0]; assertThat("First should be exact match and has score 1", hit6.getScore(), equalTo(2.0f)); assertImageScore(hits6, nameToSearch, 2.0f); // test search with exist image using hash ImageQueryBuilder imageQueryBuilder7 = new ImageQueryBuilder("img").feature(FeatureEnum.CEDD.name()).lookupIndex(INDEX_NAME).lookupType(DOC_TYPE_NAME).lookupId(idToSearch).lookupPath("img").hash(HashEnum.BIT_SAMPLING.name()); SearchResponse searchResponse7 = client().prepareSearch(INDEX_NAME).setTypes(DOC_TYPE_NAME).setQuery(imageQueryBuilder7).setSize(totalImages).get(); assertNoFailures(searchResponse7); SearchHits hits7 = searchResponse7.getHits(); assertThat("Should match at least one image", hits7.getTotalHits(), equalTo((long) totalImages)); SearchHit hit7 = hits7.getHits()[0]; assertThat("First should be exact match and has score 1", hit7.getScore(), equalTo(2.0f)); assertImageScore(hits7, nameToSearch, 2.0f); }
Example 18
Source File: EsAbstractConditionQuery.java From fess with Apache License 2.0 | 4 votes |
protected TermQueryBuilder regTermQ(String name, Object value) { checkEsInvalidQuery(name, value); TermQueryBuilder termQuery = QueryBuilders.termQuery(name, value); regQ(termQuery); return termQuery; }
Example 19
Source File: ElasticSearchHelper.java From sunbird-lms-service with MIT License | 3 votes |
/** * This method returns TermQueryBuilder with boosts if any provided * * @param name of the field for termquery * @param value of the field for termquery * @param boost for increasing the search parameters priority * @return TermQueryBuilder */ private static TermQueryBuilder createTermQuery(String name, Object value, Float boost) { if (isNotNull(boost)) { return QueryBuilders.termQuery(name, value).boost(boost); } else { return QueryBuilders.termQuery(name, value); } }
Example 20
Source File: ElasticsearchIndex.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 3 votes |
/** * Evaluates the given query only for the given resource. * * @param resource * @param request * @param query * @return search hits */ public SearchHits search(Resource resource, SearchRequestBuilder request, QueryBuilder query) { // rewrite the query QueryBuilder idQuery = QueryBuilders.termQuery(SearchFields.URI_FIELD_NAME, SearchFields.getResourceID(resource)); QueryBuilder combinedQuery = QueryBuilders.boolQuery().must(idQuery).must(query); return search(request, combinedQuery); }