136

I have a large solr index, and I have noticed some fields are not updated correctly (the index is dynamic).

This has resulted in some fields having an empty "id" field.

I have tried these queries, but they didn't work:

 id:''
 id:NULL
 id:null
 id:""
 id:
 id:['' TO *]

Is there a way to query empty fields?

Thanks

0

8 Answers 8

163

Try this:

?q=-id:["" TO *]
Sign up to request clarification or add additional context in comments.

5 Comments

Even though the SolrQuerySyntax page says -id:[* TO *], only -id:["" TO *] worked for me on solr 1.4.
@user2043553 Nope, if you ?q=-id:* you get Cannot parse '-q:*': '*' or '?' not allowed as first character in WildcardQuery
@YzmirRamirez I've tried with the example of Solr 4.5.1 and ?q=-id:* seems to work as expected. Maybe the parsing error is related to this issue.
Sorry, forgot the version ... Lucene Specification Version: 3.2.0 I was using. Glad they added the syntax in Solr 4.5.1.
Beware that this syntax seems to also return rows whose field value starts with a whitespace (in Solr 4.3)
139

One caveat! If you want to compose this via OR or AND you cannot use it in this form:

-myfield:*

but you must use

(*:* NOT myfield:*)

This form is perfectly composable. Apparently SOLR will expand the first form to the second, but only when it is a top node. Hope this saves you some time!

7 Comments

This answer deserves more points than it actually has. You saved us a lot of time!
+1 here as well. I implemented the other options but I had to include it in an fq= rather than q= and also had to implement an OR to check if the field was empty OR had a specific value. This is the only option that worked for that use case.
I agree this should be the accepted answer on the question
You saved me so much of a headache. I'm not sure thank you is sufficient.
I also had to use fq, with q = *:*. Also note you obviously can't do this check against non-indexed/only-stored fields which caught me out for a second.
|
71

According to SolrQuerySyntax, you can use q=-id:[* TO *].

1 Comment

This should be marked as the correct answer. See stackoverflow.com/questions/10722145/…
13

If you have a large index, you should use a default value

   <field ... default="EMPTY" />

and then query for this default value. This is much more efficient than q=-id:["" TO *]

2 Comments

Would this only work for fields of type String? How would you do it for for boolean?
I guess, it should work in the same way. But I have never checked it.
3

You can also use it like this.

fq=!id:['' TO *]

Comments

2

A note added here, to make the field searchable first, it needs the field type in SOLR schema set to "indexed = true". Then you can use "field_name:*" for string type and "field_name:[* TO *]" for numeric type.

Comments

1

If you are using SolrSharp, it does not support negative queries.

You need to change QueryParameter.cs (Create a new parameter)

private bool _negativeQuery = false;

public QueryParameter(string field, string value, ParameterJoin parameterJoin = ParameterJoin.AND, bool negativeQuery = false)
{
    this._field = field;
    this._value = value.Trim();
    this._parameterJoin = parameterJoin;
    this._negativeQuery = negativeQuery;
}

public bool NegativeQuery
{
    get { return _negativeQuery; }
    set { _negativeQuery = value; }
}

And in QueryParameterCollection.cs class, the ToString() override, looks if the Negative parameter is true

arQ[x] = (qp.NegativeQuery ? "-(" : "(") + qp.ToString() + ")" + (qp.Boost != 1 ? "^" + qp.Boost.ToString() : "");

When you call the parameter creator, if it's a negative value. Simple change the propertie

List<QueryParameter> QueryParameters = new List<QueryParameter>();
QueryParameters.Add(new QueryParameter("PartnerList", "[* TO *]", ParameterJoin.AND, true));

Comments

1

you can do it with filter query q=*:*&fq=-id:*

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.