Package org.elasticsearch.index.query

Examples of org.elasticsearch.index.query.BoolQueryBuilder


        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];
View Full Code Here


    public DfsOnlyRequest(Fields termVectorFields, String[] indices, String[] types, Set<String> selectedFields) throws IOException {
        super(indices);

        // build a search request with a query of all the terms
        final BoolQueryBuilder boolBuilder = boolQuery();
        TermsEnum iterator = null;
        for (String fieldName : termVectorFields) {
            if ((selectedFields != null) && (!selectedFields.contains(fieldName))) {
                continue;
            }
            Terms terms = termVectorFields.terms(fieldName);
            iterator = terms.iterator(iterator);
            while (iterator.next() != null) {
                String text = iterator.term().utf8ToString();
                boolBuilder.should(QueryBuilders.termQuery(fieldName, text));
            }
        }
        // wrap a search request object
        this.searchRequest = new SearchRequest(indices).types(types).source(new SearchSourceBuilder().query(boolBuilder));
    }
View Full Code Here

            public void onResponse(GetResponse getResponse) {
                if (!getResponse.isExists()) {
                    listener.onFailure(new DocumentMissingException(null, request.type(), request.id()));
                    return;
                }
                final BoolQueryBuilder boolBuilder = boolQuery();
                try {
                    final DocumentMapper docMapper = indicesService.indexServiceSafe(concreteIndex).mapperService().documentMapper(request.type());
                    if (docMapper == null) {
                        throw new ElasticsearchException("No DocumentMapper found for type [" + request.type() + "]");
                    }
                    final Set<String> fields = newHashSet();
                    if (request.fields() != null) {
                        for (String field : request.fields()) {
                            FieldMappers fieldMappers = docMapper.mappers().smartName(field);
                            if (fieldMappers != null) {
                                fields.add(fieldMappers.mapper().names().indexName());
                            } else {
                                fields.add(field);
                            }
                        }
                    }

                    if (!fields.isEmpty()) {
                        // if fields are not empty, see if we got them in the response
                        for (Iterator<String> it = fields.iterator(); it.hasNext(); ) {
                            String field = it.next();
                            GetField getField = getResponse.getField(field);
                            if (getField != null) {
                                for (Object value : getField.getValues()) {
                                    addMoreLikeThis(request, boolBuilder, getField.getName(), value.toString(), true);
                                }
                                it.remove();
                            }
                        }
                        if (!fields.isEmpty()) {
                            // if we don't get all the fields in the get response, see if we can parse the source
                            parseSource(getResponse, boolBuilder, docMapper, fields, request);
                        }
                    } else {
                        // we did not ask for any fields, try and get it from the source
                        parseSource(getResponse, boolBuilder, docMapper, fields, request);
                    }

                    if (!boolBuilder.hasClauses()) {
                        // no field added, fail
                        listener.onFailure(new ElasticsearchException("No fields found to fetch the 'likeText' from"));
                        return;
                    }

                    // exclude myself
                    if (!request.include()) {
                        Term uidTerm = docMapper.uidMapper().term(request.type(), request.id());
                        boolBuilder.mustNot(termQuery(uidTerm.field(), uidTerm.text()));
                        boolBuilder.adjustPureNegative(false);
                    }
                } catch (Throwable e) {
                    listener.onFailure(e);
                    return;
                }
View Full Code Here

  private void createQuery() {

    queryBuilder = new MatchAllQueryBuilder();

    // The boolean query builder
    BoolQueryBuilder booleanQuery = new BoolQueryBuilder();

    // Terms
    if (searchTerms != null) {
      for (Map.Entry<String, Set<Object>> entry : searchTerms.entrySet()) {
        Set<Object> values = entry.getValue();
        if (values.size() == 1)
          booleanQuery.must(new TermsQueryBuilder(entry.getKey(), values.iterator().next()));
        else
          booleanQuery.must(new TermsQueryBuilder(entry.getKey(), values.toArray(new String[values.size()])));
      }
      this.queryBuilder = booleanQuery;
    }

    // Negative terms
    if (negativeSearchTerms != null) {
      for (Map.Entry<String, Set<Object>> entry : negativeSearchTerms.entrySet()) {
        Set<Object> values = entry.getValue();
        if (values.size() == 1)
          booleanQuery.mustNot(new TermsQueryBuilder(entry.getKey(), values.iterator().next()));
        else
          booleanQuery.mustNot(new TermsQueryBuilder(entry.getKey(), values.toArray(new String[values.size()])));
      }
      this.queryBuilder = booleanQuery;
    }

    // Date ranges
    if (dateRanges != null) {
      for (DateRange dr : dateRanges) {
        booleanQuery.must(dr.getQueryBuilder());
      }
      this.queryBuilder = booleanQuery;
    }

    // Text
    if (text != null) {
      MatchQueryBuilder textQueryBuilder = QueryBuilders.matchQuery(TEXT, text);
      booleanQuery.must(textQueryBuilder);
      this.queryBuilder = booleanQuery;
    }

    // Fuzzy text
    if (fuzzyText != null) {
      FuzzyLikeThisQueryBuilder fuzzyQueryBuilder = QueryBuilders.fuzzyLikeThisQuery(TEXT_FUZZY).likeText(fuzzyText);
      booleanQuery.must(fuzzyQueryBuilder);
      this.queryBuilder = booleanQuery;
    }

    // Fulltext
    if (fulltext != null) {
      MatchQueryBuilder textQueryBuilder = QueryBuilders.matchQuery(FULLTEXT, fulltext);
      booleanQuery.must(textQueryBuilder);
      this.queryBuilder = booleanQuery;
    }

    // Fuzzy fulltext
    if (fuzzyFulltext != null) {
      FuzzyLikeThisQueryBuilder fuzzyQueryBuilder = QueryBuilders.fuzzyLikeThisQuery(FULLTEXT_FUZZY).likeText(fuzzyFulltext);
      booleanQuery.must(fuzzyQueryBuilder);
      this.queryBuilder = booleanQuery;
    }

    // Recency boost. We differentiate between various (random) levels of
    // recency
    if (recencyBoost) {
      Calendar date = Calendar.getInstance();
      // Last week
      RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery(MODIFIED);
      date.add(Calendar.WEEK_OF_YEAR, -1);
      rangeQueryBuilder.gt(date.getTimeInMillis()).boost(10);
      booleanQuery.should(rangeQueryBuilder);
      // Last month
      rangeQueryBuilder = QueryBuilders.rangeQuery(MODIFIED);
      date.add(Calendar.WEEK_OF_YEAR, -3);
      rangeQueryBuilder.gt(date.getTimeInMillis()).boost(5);
      booleanQuery.should(rangeQueryBuilder);
      // Last year
      rangeQueryBuilder = QueryBuilders.rangeQuery(MODIFIED);
      date.add(Calendar.MONTH, -5);
      rangeQueryBuilder.gt(date.getTimeInMillis()).boost(2);
      booleanQuery.should(rangeQueryBuilder);
    }

    QueryBuilder unfilteredQuery = queryBuilder;
    List<FilterBuilder> filters = new ArrayList<FilterBuilder>();
View Full Code Here

*/
class CriteriaQueryProcessor {


    QueryBuilder createQueryFromCriteria(Criteria criteria) {
        BoolQueryBuilder query = boolQuery();

        ListIterator<Criteria> chainIterator = criteria.getCriteriaChain().listIterator();
        while (chainIterator.hasNext()) {
            Criteria chainedCriteria = chainIterator.next();
            if(chainedCriteria.isOr()){
                query.should(createQueryFragmentForCriteria(chainedCriteria));
            }else if(chainedCriteria.isNegating()){
                query.mustNot(createQueryFragmentForCriteria(chainedCriteria));
            }else{
                query.must(createQueryFragmentForCriteria(chainedCriteria));
            }
        }
        return query;
    }
View Full Code Here

        return query;
    }

    private QueryBuilder buildNegationQuery(String fieldName, Iterator<Criteria.CriteriaEntry> it){
        BoolQueryBuilder notQuery =  boolQuery();
        while (it.hasNext()){
            notQuery.mustNot(fieldQuery(fieldName, it.next().getValue()));
        }
        return notQuery;
    }
View Full Code Here

TOP

Related Classes of org.elasticsearch.index.query.BoolQueryBuilder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.