简体   繁体   English

休眠搜索-在布尔查询中处理null

[英]Hibernate search - handling null in boolean query

Is there a best practice for handling optional sub-queries? 有处理可选子查询的最佳实践吗? So say my search service has 所以说我的搜索服务有

query = builder.bool().must(createQuery(field1, term1)).must(createQuery(field2, term2)).createQuery();

createQuery(field, term) {
     if(term != null) {
          return builder.keyword().onField(field).matching(term).createQuery();
     }
     return null;
}

With the default QueryBuilder if I use a query like this and the term is null, the resulting query is "+term1 +null" or something along those lines, which causes a null pointer exception when the query is executed against the index. 对于默认的QueryBuilder,如果我使用类似这样的查询,并且该术语为null,则生成的查询为“ + term1 + null”或类似的内容,这将对索引执行查询时导致空指针异常。 Is there a recommended way to avoid this issue? 有建议的方法来避免此问题吗? I was thinking about a custom QueryBuilder but I'm not sure how to tell the fulltext session to use my implementation rather than it's default. 我当时在考虑自定义QueryBuilder,但不确定如何告诉全文会话使用我的实现,而不是默认设置。 The only other way I can think of is something like 我能想到的唯一其他方法是

query;
query1 = createQuery(field1, term1);
query2 = createQuery(field2, term2);

if(query1 != null && query2 != null) {
    query = builder.bool().must(query1).must(query2).createQuery();
} else if(query1 != null && query2 == null) {
    query = query1;
} else if(query1 == null && query2 != null) {
    query = query2;
}

createQuery(field, term) {
     if(term != null) {
          return builder.keyword().onField(field).matching(term).createQuery();
     }
     return null;
}

But this gets really messy really fast if there are more than a handful of sub-queries. 但是,如果有很多子查询,这将很快变得非常混乱。

what about this 那这个呢

    org.json.JSONObject json = new org.json.JSONObject();
    json.put(field1, term1);
    json.put(field2, term2);
    ...


    bool = builder.bool();
    for (Iterator keys = json.keys(); keys.hasNext();) {
        String field = (String) keys.next();
        String term = (String) json.get(field);
        q = createQuery(field, term);
        if (q != null) {
            bool.must(q);
        }
    }
    query = bool.createQuery();

if you have duplicate fields with different terms you must use this : 如果您有重复的字段使用不同的术语,则必须使用以下命令:

    org.json.JSONObject json = new org.json.JSONObject();
    json.append(field1, term1);
    json.append(field2, term2);
    ...


    bool = builder.bool();
    for (Iterator keys = json.keys(); keys.hasNext();) {
        String field = (String) keys.next();
        JSONArray terms = (JSONArray) json.get(field);
        for (int i = 0; i < terms.length(); i++) {
            String term = (String) terms.get(i);
            q = createQuery(field, term);
            if (q != null) {
                bool.must(q);
            }
        }
    }
    query = bool.createQuery();

What you might do is introducing a method whose sole purpose would be to add a "must" in a null-safe way. 您可能会引入一种方法,其唯一目的是以null安全的方式添加“必须”。 Ie do something like this: 即做这样的事情:

BooleanJunction junction = builder.bool();
must(junction, createQuery(field1, term1));
must(junction, createQuery(field2, term2));
query = junction.createQuery();

void must(BooleanJunction junction, Query query) {
    if (query != null) {
        junction.must(query);
    }
}

Query createQuery(String field, Object term) {
     if(term != null) {
          return builder.keyword().onField(field).matching(term).createQuery();
     }
     return null;
}

This would take out the "fluidity" of the BooleanJunction API, but since it's at the top-level only, I guess it's not so bad. 这将消除BooleanJunction API的“流畅性”,但是由于它仅处于顶层,因此我认为还不错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM