How to execute date range query on date index - SOLR - solr

What is the correct way to run the range queries on SOLR date index field. Below is the snapshot from schema.xml file for the field definition:
<dynamicField name="ds_*" type="pdate" indexed="true" stored="false" multiValued="false" docValues="true"/>
Context: The SOLR document contains a date element and value as "ds_clip_date":"2020-10-14T12:00:00Z".
Requirement is to allow user to fetch documents from this month/last 30 days/last 90 days/this year.
I tried building below from SOLR query dashboard - but this gives me all the documents without respecting the range:
http://localhost:8983/solr/testcore/select?facet.field=ds_clip_date&facet.query=facet.range%3Dds_clip_date%26f.ds_clip_date.facet.range.start%3D2020-11-17&facet=on&fl=ds_clip_date&q=*%3A*&rows=200&start=0
and I get the below summary:
"facet_counts":{
"facet_queries":{
"facet.range=ds_clip_date&f.ds_clip_date.facet.range.start=2020-11-17":0},
"facet_fields":{
"ds_clip_date":[
"2020-12-15T12:00:00Z",20,
"2020-12-16T12:00:00Z",14,
"2020-09-25T12:00:00Z",1,
"2020-10-14T12:00:00Z",1,
"2020-10-20T12:00:00Z",1,
"2020-10-22T12:00:00Z",1,
"2020-11-24T12:00:00Z",1,
"2020-12-17T12:00:00Z",1]},
"facet_ranges":{},
"facet_intervals":{},
"facet_heatmaps":{}}
Also, referring to some other QA, tried to build as below:
http://localhost:8983/solr/testcore/select?facet=true&facet.field=ds_clip_date&facet.range=ds_clip_date&f.ds_clip_date.facet.range.start=2020-12-15T00:00:00Z&f.ds_clip_date.facet.range.end=NOW&f.ds_clip_date.facet.range.gap=%2B1DAY
The result is as below:
{
"response":{"numFound":0,"start":0,"numFoundExact":true,"docs":[]
},
"facet_counts":{
"facet_queries":{},
"facet_fields":{
"ds_clip_date":[]},
"facet_ranges":{
"ds_clip_date":{
"counts":[
"2020-12-15T00:00:00Z",0,
"2020-12-16T00:00:00Z",0,
"2020-12-17T00:00:00Z",0],
"gap":"+1DAY",
"start":"2020-12-15T00:00:00Z",
"end":"2020-12-18T00:00:00Z"}},
"facet_intervals":{},
"facet_heatmaps":{}}}
However in the above summary you can find that 2020-12-15 has 20 documents indexed.
I am running SOLR 8.7.

Related

Why Solr Group get only one group,but Solr Facet get only two groups?

Solr:I have a field, the field type is string, the field content is '22 33', when I do Group, why do I get a group? But Facet has two groups
config:
<field name="unit_co" type="text_general" indexed="true" stored="true"/>
group result:
{{
"groupValue":"22",
"doclist":{"numFound":1,"start":0,"docs":\[
{
"unit_co":" 22 33 "}
}}]}}}
facet result:
{
"unit_co":[
"22",1,
"33",1]
}
I tried to solve this problem with other solutions, such as multi-value fields, but Solr does not support groups with multi-value fields.
solr error:can not use FieldCache on multivalued field

Solr 8.6 - combine facets on multivalued dateRange field with exclude tags, negate and OR clause

On Solr 8.6, I have a multivalued field of dateRange type which contains an availability table.
<field name="availabilities" docValues="false" type="dateRange" multiValued="true" indexed="true" required="false" stored="true"/>
"availabilities": [
"[2021-12-22T00:00:00Z TO 2022-01-02T23:59:59Z]",
"[2022-01-20T00:00:00Z TO 2022-02-02T23:59:59Z]",
"[2022-02-10T00:00:00Z TO 2022-02-21T23:59:59Z]"
]
With a facet, I want to be able to select the available and/or unavailable people.
The Solr query is as follows and works well.
http://localhost:8985/solr/rby/select?facet=on&facet.query={!key="FQ4#Available now" ex=tag_availabilities}availabilities:[NOW TO NOW]&fl=id,availabilities&fq={!tag=tag_availabilities}availabilities:([NOW TO NOW])&q=*:*&rows=10&wt=json
For the unavailable people query, it does not work when I exclude NOW with {} like this.
http://localhost:8985/solr/rby/select?facet=on&facet.query={!key="FQ4#Unavailable" ex=tag_availabilities}availabilities:{NOW TO NOW}&fl=id,availabilities&fq={!tag=tag_availabilities}availabilities:({NOW TO NOW})&q=*:*&rows=10&wt=json
Solr answers with the following error:
"msg": "Wrong order: 2021-12-30T16:18:06.304 TO 2021-12-30T16:18:06.302",
I found a workaround with - before field but now I can't combine the two filters of the facet with an OR.
http://localhost:8985/solr/rby/select?facet=on&facet.query={!key="FQ4#Unavailable" ex=tag_availabilities}-availabilities:[NOW TO NOW]&fl=id,availabilities&fq={!tag=tag_availabilities}-availabilities:([NOW TO NOW])&q=*:*&rows=1000&wt=json
Thanks for your help

Incorrect field reading during ranking

Solr version 5.1.0
Documents contain DocValues field "ts" with timestamp using during ranking.
<field name="ts" type="long" docValues="true" indexed="true" stored="true" multiValued="false"/>
If I directly request document at Solr Admin UI I see that it contains correctly value:
"ts": 1575624481951
But when I added logs into the ranking method I saw that "ts" values for the same document is 0.
LeafReader reader = context.reader();
NumericDocValues timeDV = DocValues.getNumeric(reader, "ts");
long timestamp = timeDV.get(doc);
LOG.info("ts: " + timestamp);
Log:
ts: 0
Problem was in incorrect deleting document from Solr.
That was reproducing with next sequence of actions:
Firstly document was added to Solr without field "ts".
After some actions in app document was added again but with field "ts".
When Solr tried to ranking this document had not this field.
I added additional logs and saw that first version of document was on one shard and second version (with field "ts") was on another shard.
I don't pretty sure why it may happened because as I know Solr should put the same document on the same shard.
But anyway it was fixed with deleting document from index before adding second version.

Sort on field completeness of Solr Documents

I have this Solr field
<field name="listing_thumbnail" type="string" indexed="false" stored="true"/>
Now when the results are shown the fields without the field value should be shown at the last. Is this possible in SOLR? To generalise is it possible to sort documents on field completeness?
You can make use of bq (Boost Query) Parameter of the dismax/edismax query handler. This allows to query if a field is empty or not and then affect the score, but to do so the field needs to be indexed=true.
If you had your field indexed you could add bq=(listing_thumbnail:*) - this would give a push to all documents with a value in that field.

solr fq; integer comparison on a substring

That is probably a bad title...
But let's say I have a bunch of strings in a multivalue field
<field name="stringweights" type="text_nostem" indexed="true" stored="true" multiValued="true"/>
Sample data might be:
history:10
geography:33
math:29
Now I want to write a fq where I select all records in solr where:
stringweights starts with "geography:"
and where the integer value after "geography:" is >= 10.
Is it possible to write a solr query like that?
(It's not possible to create an integer field in the solr schema named "geography", another called "math" etc because these string portions of the field are unknown at design time and can be many hundreds / thousands of different values.)
You may want to look into dynamic fields. Declare a dynamic field in your schema like:
<dynamicField name="stringweight_*" type="integer" indexed="true" stored="true"/>
Then you can have your docs like:
stringweight_history: 10
stringweight_geography: 33
stringweight_math: 29
Your filter query is then simply:
fq=stringweight_geography:[10 TO *]
You may need to build a custom indexer for doing this. Or use a script transformer with data import handler as mentioned here: Dynamic column names using DIH (DataImportHandler).

Resources