Boolean NOT in solr query - solr

How do I pick/ delete all the documents from Solr using the boolean NOT notion?
i.e. How do I delete all the documents from Solr who's id does NOT start with A59?

Use - to indicate NOT.
For example, to query documents with id not starting with A59, the query would be: -id:A59*, that is: /solr/select/?q=-id:A59*
To delete by query, post the query in a delete message to the update handler, as specified here.
EDIT: NOT (all uppercase) can also be used as boolean operator

Exclamation works for NOT as well, so:
/solr/select/?q=!id:A59*
should work in the case above.

I don't believe that a negative delete by query works. See this Jira ticket: https://issues.apache.org/jira/browse/SOLR-381
They do say that there is a workaround to prefix in a :, but I do not have any luck with that.
This does not work (same with using NOT)
java -Ddata=args -jar /opt/solr/example/exampledocs/post.jar "-userid:*"
java -jar /opt/solr/example/exampledocs/post.jar *.xml
Adding in a : gives a syntax error (same with using NOT)
java -Ddata=args -jar /opt/solr/example/exampledocs/post.jar ": -userid:*"
java -jar /opt/solr/example/exampledocs/post.jar *.xml
SimplePostTool: version 1.4
SimplePostTool: POSTing args to http://localhost:8983/solr/update..
SimplePostTool: FATAL: Solr returned an error #400 Error parsing Lucene query
SimplePostTool: version 1.4

Using the - symbol in-front of the files to implies that exclude that particular value. It will give result like NOT Equal
The following is sample url query string where. I have kept "&fq=-HQ_City_Code:MEL",
It will skip all the result which is having HQ_City_Code value MEL.
http://localhost:8983/solr/HQ_SOLR_Hotels/select?q=*:*&fq=HQ_National_Code:TH&fq=HQ_TYPE:hotel_EN&fq=HQ_Country_Code:AU&**fq=-HQ_City_Code:MEL**&wt=json&indent=true

before deleting please ensure that the ids that you are referring to is string and in no way would be formed by two terms combined .
The way I would do it is read the data from solr from a script and do a singular delete or in batches , because it provides a better control and validations over each ids which reduces the risk of wrong deletion
Hence
1 read the data from solr from a script using
/solr/select/?q=id:A59*
2 verify and validate the ids
3 delete them one by one or in a group of 10 ids at once
Regards
Rajat

As Mauricio stated:
Use the - symbol to indicate what you want exclude in your query.
The following two queries will delete all documents except the ones that begin with A59.
GET http://<url>/solr/<core>/update?stream.body=<delete><query>-id:A59*</query></delete>
GET http://<url>/solr/<core>/update?stream.body=<commit/>
The first line does the delete operation.
The second line does the commit.

Related

Spring Data Solr: Queries with "AND", "NOT" and "OR" not escaped or handled

We are using spring-data-solr, mainly using exact match/equals filter queries.
We have found that the values NOT, OR, and AND can be supplied, which are passed directly onto solr (without any pre-processing). This causes solr to error. For example, building a Criteria object like
Criteria.where("fuelType").is("AND")
Results in the following solr query
fq=fuelType:AND
We have found that if we call Solr directly with
fq=fuelType:"AND"
This would be fine, however, I can see that quotes are only added when there is whitespace in the value.
Is there something I am missing?
I still want to use the Standard Solr query parser if possible
The pull request for this has been merged,
https://jira.spring.io/browse/DATASOLR-437
https://github.com/spring-projects/spring-data-solr/pull/74

solr :cannot delete a document using post

I'm newbie to solr, I'm trying to test different possibilities to delete a document.
I have tested the
update?commit=true&stream.body=<delete><query>*:*</query></delete>.
An other method that I find from solr quick start said that a document can be deleted
using : bin/post -c gettingstarted -d "<delete><id>SP2514N</id></delete>"
When I try it seems that it works, but when I search for the id I find it.
why it doesn't work, and I wonder if there is other ways to delete a document (for example using the admin console).
Try this
http://localhost:8983/solr/gettingstarted/update?stream.body=<delete><query>id:SP2514N</query></delete>&commit=true
check Here for reference
It doesn't look like you issued a commit after you deleted (hence the commit=true in the URL deletion). Add a <commit/> after your delete and that should work. (see more on Solr Update XML here)

Carrot2 dcs php example class modification

I currently have solr and carrot2 configured and working on my server. I am using the dcs example class for php provided in the DCS download from project.carrot2.org . For reference the class can be found here https://github.com/amoghtolay/clustering/blob/master/carrot2-dcs-3.6.2/examples/php5/
I have tried a few things to modify the query to descending order and alter the number of records returned. The query used from a browser that give the results I need is q=*:*&sort=_docid_%20desc&rows=20. Although when I alter the query by altering the equivalent to line 35 in example.php found in link above to match the query I need I get the following error message "An error occurred during processing: HTTP error occurred, error code: 500" having the query only set to *:* works fine but it not the information needed. Also, source is set to solr.
Could anyone provide some assistance in getting this working, thanks.
If you'd like to pass some extra parameters to the Solr request URL, you'll need need to append them to the SolrDocumentSource.serviceUrlBase attribute. You can specify the number of results using the results attribute.

/select with 'q' parameter does not work

Whenever i query with q=: it shows all the documents but when i query with q=programmer 0 docs found.(contents is the default search field)
my schema has: id(unique),author,title,contents fields
Also query works fine for:
q=author:"Value" or q=title:"my book" etc, only for contents field no results.
Also when i query using spell checker(/spell?q=programmer) output shows spelling suggestions for this word,when 'programmer' is the right word and present in many documents.
I referred the example docs for configurations.
All of a sudden i am getting this,initially it worked fine.
I guess there some problem only in the contents field,but cannot figure it out.
Is it because indexes are not created properly for contents field?
(I am using solr 4.2 on Windows 7 with tomcat as webserver)
Please help.Thanks a lot in advance.
Are you sure you set the default search field? The reason you have this problem might be because you didn't set the <defaultSearchField> field in your schema.xml file. This is why "q=author:value" works while q=WHATEVER doesn't.
The Is used by Solr when parsing queries to
identify which field name should be searched in queries where an
explicit field name has not been used.
But also consider this:
The is used by Solr when parsing queries to
identify which field name should be searched in queries where an
explicit field name has not been used. It is preferable to not use or
rely on this setting; instead the request handler or query LocalParams
for a search should specify the default field(s) to search on. This
setting here can be omitted and it is being considered for
deprecation.
Do you have any data in your instance. try q=*:* and see what it returns. "for" is a stop word, may be it was filtered out. Look for something else as value to test.

Solr not searching (Very basic example)

Solr version :
4.2.1
Objective:
I am trying to get a very simplistic Solr example off the ground
So far:
Installed solr
Was able to run the example\tutorial successfully http://lucene.apache.org/solr/4_2_1/tutorial.html
Next:
Now I am trying to create my own schema
I have created a schema : http://pastebin.com/vj4ATa8d
And a Test Doc:http://pastebin.com/7fvZ5GTQ
I have added the doc to Solr using the command
java -jar post.jar testdoc.xml
What’s working:
In Solr Admin- I can see the schema
I can see one document uploaded
I can go to Admin console and query as follows:
Specify q as “:”. This works- shows the document
http://localhost:8983/solr/collection2/select?q=*%3A*&wt=xml&indent=true
What does not work:
If I give q as Nashua- I see no results
This is the default search field
Other attributes didn't work either
http://localhost:8983/solr/collection2/select?q=Nashua&wt=xml&indent=true
The debug response http://pastebin.com/fTneyEba
You need to either copy your fields into the default search field (in this case text) or qualify your query with the field you want to search against:
.../select?q=city:Nashua&wt=xml&indent=true
Things to read up on:
Default Search Field
Copy Fields
Both are documented here:
https://wiki.apache.org/solr/SchemaXml

Resources