有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Lucene 5.5.4,NumericRangeQuery:如何按非范围值搜索,例如按整数数组搜索

我是Lucene的新手,正在寻求帮助

这是我的

/**
 * JPA Persistence Unit.
 */
@PersistenceContext
private EntityManager entityManager;

/**
 * Hibernate Full Text Entity Manager.
 */

private FullTextEntityManager ftem;

@PostConstruct
@Async
public void intializeIndexes() throws InterruptedException {
    FullTextEntityManager fullTextEntityManager =
        Search.getFullTextEntityManager(entityManager);
    fullTextEntityManager.createIndexer().startAndWait();
}

protected FullTextEntityManager getFullTextEntityManager() {
    ftem = Search.getFullTextEntityManager(entityManager);
    return ftem;

}

搜索方法主体

QueryBuilder qb = getFullTextEntityManager().getSearchFactory()
                                                .buildQueryBuilder()
                                                .forEntity(AmazonProduct.class)
                                                .get();

    BooleanJunction junction = qb.bool();

if(request.getMinPrice() != null || request.getMaxPrice() != null) {
        junction.must(NumericRangeQuery.newDoubleRange(PRICE_FIELD, request.getMinPrice(), request.getMaxPrice(), true, true));
    }

    if(request.getMinWeight() != null || request.getMaxWeight() != null) {
        junction.must(NumericRangeQuery.newDoubleRange(WEIGHT_FIELD, request.getMinWeight(), request.getMaxWeight(), true, true));
    }

    if (null != request.getProductTiers() && request.getProductTiers().length > 0) {
        for (int tier : request.getProductTiers()) {
            junction.must(NumericRangeQuery.newIntRange("productTier", tier, tier, true, true));
        }

当我按价格、重量完成范围搜索时,这是可以的

但是我不知道如何通过眼泪搜索,眼泪是整数数组,比如[1,3,5,7],所以我不需要这里的范围。我只需要眼泪应该是1,3,5或7的物体。我在数据库“tier”中有一列int。那么我应该如何组织查询呢?我需要数字范围查询吗? 非常感谢


共 (1) 个答案

  1. # 1 楼答案

    我使用布尔子查询解决了这个问题

    if (request.getProductTiers() != null && request.getProductTiers().length > 0) {
            BooleanJunction tierQuery = qb.bool();
            for (int tier : request.getProductTiers()) {
                tierQuery.should(NumericRangeQuery.newIntRange(PROD_TIER_KEY, tier, tier, true, true));
            }
            junction.must(tierQuery.createQuery());
        }
    

    我尝试过不同的查询,但这个有效

    子查询是这样工作的

    +productCategory:Home~2 +(productTier:[3 TO 3] productTier:[6 TO 6])
    

    括号内有带或条件的子查询