在BoolQueryBuilder中,Should子句不起作用

时间:2016-06-09 03:55:47

标签: elasticsearch elasticsearch-2.0 elasticsearch2

我的ES方法

public VendorSearchResult searchVendors(String query, Locale country, CostType... costtypes) 
{
    try {

    BoolQueryBuilder bqBuilder = new BoolQueryBuilder();

    bqBuilder.must(new TypeQueryBuilder(VENDOR_INDEXTYPE));

    if (country != null) {
        bqBuilder.must(QueryBuilders.termQuery("address.countrycode", country.getCountry().toLowerCase()));
    }

    bqBuilder.must(QueryBuilders.multiMatchQuery(query, VENDOR_SEARCHED_FIELDS).type(Type.PHRASE_PREFIX));
    /*
     *Should Clause
     */
    for (CostType costType : costtypes)
         bqBuilder.should(QueryBuilders.termsQuery("vendortype", costType.name()));
    bqBuilder.minimumNumberShouldMatch(1);

    VendorSearchResult sr = elasticSearch(bqBuilder, SORTFIELD, SortOrder.ASC);

    return sr;
}

private VendorSearchResult elasticSearch(QueryBuilder pQueryBuilder, String sortParameter, SortOrder sortOrder,
        String... types) {
    try {
        SearchAction action = SearchAction.INSTANCE;
        SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(client, action);

        searchRequestBuilder.setQuery(pQueryBuilder); 
        searchRequestBuilder.setIndices(VENDOR_INDEX);
        searchRequestBuilder.setSearchType(SearchType.DFS_QUERY_THEN_FETCH); 

        if (StringUtils.isNotEmpty(sortParameter)) {
            searchRequestBuilder.addSort(sortParameter, sortOrder);
        }

        SearchResponse response = searchRequestBuilder.execute().actionGet();

        return convertSearchResponse(response);
}
  • 当我评论应该子句时,我可以从Elasticsearch获得结果。
  • 如果我取消注释,则不会提取任何结果。

还有其他方法可以使用boolQueryBuilder吗?

可能是什么问题?

COSTTYPE

public enum CostType {

/** The parking. */
PARKING("PARKING"),
/** The fuel. */
FUEL("FUEL"),
/** The cardealership. */
VEHICLEDEALER("VEHICLEDEALER"),
/** The creditor. */
FINANCING("FINANCING"),
/** The garage. */
GARAGE("GARAGE"),
/** The insurance. */
INSURANCE("INSURANCE"),
/** The leasing. */
LEASING("LEASING");

/** The cost type. */
private String costType;

/**
 * Instantiates a new cost type.
 *
 * @param costType
 *          the cost type
 */
private CostType(String costType) {
    this.costType = costType;
}

/**
 * Gets the cost type.
 *
 * @return the cost type
 */
public String getCostType() {
    return costType;
}

/*
 * (non-Javadoc)
 * 
 * @see java.lang.Enum#toString()
 */
@Override
public String toString() {
    return costType;
}
}

JSON文档

{ "_index": "Vendor_Index", "_type": "Vendor_Type", "_id": "732cfd82-f764-44f5-a5d9-574f4eb7000a", "_version": 52, "_score": 1, "_source": { "vendortype": "FUEL", "address": { "city": "Heimsheim", "housenumber": "1", "street": "Römerstr.", "countrycode": "de", "postcode": "71292", "geopoint": { "alt": "435", "lon": "8.86853", "lat": "48.82422" } }, "name": "OMV Tankstelle Heimsheim", "id": "732cfd82-f764-44f5-a5d9-574f4eb7000a", "creationDate": "2014-10-25T18:30:00.000Z", "brand": "OMV" } }

0 个答案:

没有答案