robingrindrod
robingrindrod

Reputation: 472

Is it possible to have a document match any query for a particular field?

In Solr it is possible to query for any value in a certain field with a query such as field:*. I would like to be able to perform the inverse of this such that I can add a value to a document (*) that causes it to match any query on that field.

For instance, the following document:

{
    "a": 1,
    "b": *,
    "c": *
}

should match these queries:

but not these queries:

The reason I want to do this is so that whenever a user searches for a:1 the document will be matched no matter what other conditions are specified. I can achieve this by editing the query but would prefer not to. Is there a way to do this?

Upvotes: 0

Views: 51

Answers (2)

Alexandre Rafalovitch
Alexandre Rafalovitch

Reputation: 9789

You could probably do something similar with Solr switch statement (reference, example). You pass your condition in a variable different from standard Solr and then switch on the values/presence/absence in that variable.

I use that for advanced multi-field search and don't use actual value, just presence or absence of value as a way to trigger a Solr condition.

Upvotes: 0

mindas
mindas

Reputation: 26713

This is a bit of a strange request. Effectively what you want is to make a certain field useless as with this sort of logic, any data for this field becomes irrelevant (as the query will match it no matter what is persisted). Anyway, this should be possible.

In Lucene, you could do the following:

  • when creating any document, add some static plug for the field b, e.g. set the value to MatchAllValuesNoMatterWhatTheWeatherIs
  • implement your own query class and override public Query rewrite(IndexReader reader) method
  • the overridden method would change the query term for b (if it exists) into b:MatchAllValuesNoMatterWhatTheWeatherIs. Other terms should remain untouched.

Upvotes: 1

Related Questions