Solr query by specific fields - solr

I use solr , latest version.
I run text query with "OR" condition by different fields.
I want to have indication due to which field the document return.
How can I do it?

Faceting could be an option here. In your Solr Query set "facet" to true. You also need to set "facet.field" to the fields that you are including in OR search criteria. The Solr Response will then show you how many results are returned for each of the search fields.
Here is the reference - https://cwiki.apache.org/confluence/display/solr/Faceting
-Amit

If you only want to specify which fields do you want to get back in the response, then you're after the fl parameter, if you do a request like:
http://localhost:8983/solr/demo/query?
q=title_t:black
fl=author,title
You're indicating that you only want to get back the author and title fields, something like:
{"response":{"numFound":2,"start":0,"docs":[
{
"title":"The Black Company",
"author":"Glen Cook"},
{
"title":"The Black Cauldron",
"author":"Lloyd Alexander"}]
}}

Related

SOLR: Search for a value in multiple fields

I am looking for a way of querying for values in multiple fields. Basically i am building a simple search engine where user can type ie. "Java How to XML JSON" and it will search for these values in 3 different fields categories, tags, description.
I read on some blog I should query all fields q=*:* and then filter based on those fields for example fq=categories:java,xml,how,to,json description:java,xml,how,to,json tags:java,xml,how,to,json
This works :| But it seems incorrect to just copy paste values like this.
Is there a correct way of doing this? I have been researching this for some time but i havent found a solution.
Any help is appreciated,
Thank you
You can use defType=edismax to get the extended dismax handler. This is meant to handle user typed queries (i.e. what you'd type in). You can then use qf (query fields) to tell the edismax handler which fields you want to search (and an optional weight for each field):
q=Java How to XML JSON&defType=edismax&qf=categories^5 tags description
.. will search each part of the string "Java How to XML JSON" in all the fields, and any hits in the categories field will be weighted five times higher than hits in the other two fields.

Amazon like refined filtering in solr

We need to implement drill down search like Amazon.
If any supplier is selected then,
currently it disabled rest of suppliers as solr facet only returns that selected filter.
query: supplier:supplierId
Though above query retuns multiple Screen Color/Screen Size, but on further selection, only the selected element is returned per filter section.
Any help to make it work like Amazon.com 's refined filtering will be much appreciated.
To provide multi-select faceting, you need to tag the filter involved so that you can exclude it when faceting on the corresponding field. This can be achieved using both the tag and ex local parameters.
Solr Ref Guide - Tagging and Excluding Filters :
To implement a multi-select facet for a given field, a GUI may want to still
display the other field values and their associated counts, as if
the corresponding filter constraint had not yet been applied.
To return counts for the field values that are currently not selected,
tag filters that directly constrain the field, and exclude those
filters when faceting on it.
For example, for a query that would originally look like :
q=mainquery&fq=supplier:supplierId&facet=true&facet.field=supplier
You would do the following :
q=mainquery&fq={!tag=sup}supplier:supplierId&facet=true&facet.field={!ex=sup}supplier

Solr edismax. How to retrieve the fields that gave the query result

I'm querying mulitple fields using the qf param. But I'm wondering how I can retrieve the field that gave the result.
Example(not a real example):
q={!edismax qf='tag content'}("tablet")AND("pc")
Field values:
doc1:
tag: tablet
content: The test has failled. Use a pc instead.
doc2:
tag: tablet pc
content: The test has worked.
As a result both documents will be returned because they both have the tablet and pc in their tag/content. Is it possible to know that doc2 had both hits in tag and doc1 only had 1 hit in tag and 1 in content? Debugquery doesn't seem to provide information about this.
I know I can increase the importance of a searchfield using the qf boost.
you can either:
use Highlighting
ask for debug info with debugQuery=true and parse the scoring info to find out.
I think 1 is easier, but it imposes some constraints on your fields (they must be stored for example)
Ok based on your response to my question.
Unfortunately, there is no way Solr currently returns which fields matched the query as part of your defaulting result docs . If the query is simple, looping over the returned stored fields is probably your best bet. Highlighting is be an option too.
There are couple of other options suggested here : http://grokbase.com/t/lucene/solr-user/117nkf36nq/determine-which-field-term-was-found

Solr - How do I get the number of documents for each field containing the search term within that field in Solr?

Imagine an index like the following:
id partno name description
1 1000.001 Apple iPod iPod by Apple
2 1000.123 Apple iPhone The iPhone
When the user searches for "Apple" both documents would be returned. Now I'd like to give the user the possibility to narrow down the results by limiting the search to one or more fields that have documents containing the term "Apple" within those fields.
So, ideally, the user would see something like this in the filter section of the ui after his first query:
Filter by field
name (2)
description (1)
When the user applies the filter for field "description", only documents which contain the term "Apple" within the field "description" would be returned. So the result set of that second request would be the iPod document only. For that I'd use a query like ?q=Apple&qf=description (I'm using the Extended DisMax Query Parser)
How can I accomplish that with Solr?
I already experimented with faceting, grouping and highlighting components, but did not really come to a decent solution to this.
[Update]
Just to make that clear again: The main problem here is to get the information needed for displaying the "Filter by field" section. This includes the names of the fields and the hits per field. Sending a second request with one of those filters applied already works.
Solr just plain Doesn't Do This. If you absolutely need it, I'd try it the multiple requests solution and benchmark it -- solr tends to be a lot faster than what people put in front of it, so an couple few requests might not be that big of a deal.
you could achieve this with two different search requests/queries:
name:apple -> 2 hits
description:apple -> 1 hit
EDIT:
You also could implement your own SearchComponent that executes multiple queries in the background and put it in the SearchHandler processing chain so you only will need a single query in the frontend.
if you want the term to be searched over the same fields every time, you have 2 options not breaking the "single query" requirement:
1) copyField: you group at index time all the fields that should match togheter. With just one copyfield your problem doesn't exist, if you need more than one, you're at the same spot.
2) you could filter the query each time dynamically adding the "fq" parameter at the end
http://<your_url_and_stuff>/?q=Apple&fq=name:Apple ...
this works if you'll be searching always on the same two fields (or you can setup them before querying) otherwise you'll always need at least a second query
Since i said "you have 2 options" but you actually have 3 (and i rushed my answer), here's the third:
3) the dismax plugin described by them like this:
The DisMaxQParserPlugin is designed to process simple user entered phrases
(without heavy syntax) and search for the individual words across several fields
using different weighting (boosts) based on the significance of each field.
so, if you can use it, you may want to give it a look and start from the qf parameters (that is what the option number 2 wanted to be about, but i changed it in favor of fq... don't ask me why...)
SolrFaceting should solve your problem.
Have a look at the Examples.
This can be achieved with Solr faceting, but it's not neat. For example, I can issue this query:
/select?q=*:*&rows=0&facet=true&facet.query=title:donkey&facet.query=text:donkey&wt=json
to find the number of documents containing donkey in the title and text fields. I may get this response:
{
"responseHeader":{"status":0,"QTime":1,"params":{"facet":"true","facet.query":["title:donkey","text:donkey"],"q":"*:*","wt":"json","rows":"0"}},
"response":{"numFound":3365840,"start":0,"docs":[]},
"facet_counts":{
"facet_queries":{
"title:donkey":127,
"text:donkey":4108
},
"facet_fields":{},
"facet_dates":{},
"facet_ranges":{}
}
}
Since you also want the documents back for the field-disjunctive query, something like the following works:
/select?q=donkey&defType=edismax&qf=text+titlle&rows=10&facet=true&facet.query=title:donkey&facet.query=text:donkey&wt=json

solr query not returning results

When I enter search url
http://localhost:8983/solr/select?qt=standard&rows=10&q=*:*
I get a response with 10 documents.
But when I want to test specific query, then nothing comes up. For example:
http://localhost:8983/solr/select?qt=standard&rows=10&q=white
Why is that happening? I clearly see in results, that there is document with word "White" in it. So Why solr dont return that document as result.?
q=*:* searches for all content on all the documents, hence you get back the results.
q=white will search for white on the default search field, which is usually text if you have not modified the schema.xml.
<defaultSearchField>text</defaultSearchField>
You can change the default field to be the field you want to search on.
OR use specific field to search on the specific field e.g. title q=title:white
If you want to search on multiple field, you can combine the fields into one field by using copyfields or use dismax request handler.

Resources