简体   繁体   English

Lucene不是null查询?

[英]Lucene not null query?

How can we construct a query to search for particular field to be not null? 我们如何构造一个查询来搜索特定字段不为空?

field_name:* is not working. field_name:*不起作用。 I tried field_name:[a* to z*] this works fine for English, but does not cover all languages. 我尝试了field_name:[a* to z*]这适用于英语,但不包括所有语言。

Any alternative suggestions? 还有其他建议吗?

我发现这在某些情况下工作field:([0 TO 9] [a TO z])

This is currently not supported by Lucene. Lucene目前不支持此功能。 See this for a discussion. 请参阅讨论。

An alternative option may be to store some pre-defined string (like nullnullnullnull ) as the field value if it is null. 替代选项可以是将一些预定义的字符串(如nullnullnullnull )存储为字段值(如果它为null)。 Then you can use a negative filter to remove these records. 然后,您可以使用负过滤器删除这些记录。 (I don't like this much, but can't think of a better option) (我不喜欢这么多,但想不出更好的选择)

Try field:[* TO *] or field:["" TO *] . 尝试field:[* TO *]field:["" TO *] But it's probably better to use a filter for this though. 但是,尽管使用过滤器可能更好。

For anyone else arriving late to the question, the documentation includes this little snippet: 对于迟到问题的其他人来说, 文档包含这个小片段:

  • where the field title has any non-null value: 字段title具有任何非空值:
    _exists_:title

I was having the same problem but there's a property you can set on the query parser which lets you have wildcard characters at the start of a search term. 我遇到了同样的问题但是你可以在查询解析器上设置一个属性,它允许你在搜索词的开头有通配符。

queryParser.setAllowLeadingWildcard(true);

This solved the problem for me 这解决了我的问题

Please see Wildcard at the Beginning of a searchterm -Lucene 在searchterm -Lucene的开头Wildcard

In the current Lucene version FieldValueQuery is outdated. 在目前的Lucene版本中,FieldValueQuery已经过时了。 Currently you can use: 目前你可以使用:

new DocValuesFieldExistsQuery(name)

This works only for SortedDocValuesFields, so you have to add them while creating the document. 这仅适用于SortedDocValuesFields,因此您必须在创建文档时添加它们。

doc.add(new SortedDocValuesField(name, new BytesRef(value));
doc.add(new StringField(name, value, Field.Store.Yes)); //optional

The second line enables you to retrieve the value (DocValuesFields' values cannot be read as Strings). 第二行使您可以检索值(DocValuesFields的值不能读作字符串)。
Note that lucene allows for multiple fields with the same name as in the aforementioned example. 请注意,lucene允许多个字段具有与上述示例中相同的名称。

Have a look at org.apache.lucene.search.FieldValueQuery : 看看org.apache.lucene.search.FieldValueQuery

A Query that matches documents that have a value for a given field as reported by LeafReader#getDocsWithField(String).

please note, that it only works with DocValues, so you would need to change a way you create your document: 请注意,它只适用于DocValues,因此您需要更改创建文档的方式:

document.add(new StringField("field-name", "field-value", Field.Store.YES));
document.add(new SortedDocValuesField("field-name", new BytesRef("field-value")));

here I added two fields - you still need regular StringField to get value. 在这里我添加了两个字段 - 你仍然需要常规的StringField来获取值。 You may choose to use BinaryDocValues#get() for older versions of Lucene, but as I see, it is removed in v7. 您可以选择对较旧版本的Lucene使用BinaryDocValues#get() ,但正如我所见,它在v7中被删除。 Not sure, what is a proper way to retrieve a value now - please check this 不确定,现在检索值的正确方法是什么 - 请检查一下

I have just started to play around with lucene (via logstash elastic search) and find that this seems to work from the kibana UI. 我刚刚开始使用lucene(通过logstash弹性搜索),并发现这似乎可以在kibana UI中使用。 I am not sure yet if this is some intelligence in elastic search or kibana, i just know that elastic search borrows from the lucene syntax. 我不确定这是弹性搜索还是kibana的智能,我只知道弹性搜索借用了lucene语法。

application:unit-test && !exception

will return all results from my unit tests which have not had an exception 将返回我没有例外的单元测试的所有结果

application:unit-test && exception

returns those which have a non null exception indexed. 返回索引非null异常的那些。 so you might try just 所以你可以尝试一下

field

or 要么

!field

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

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