How can i search inside array of a multivalued field
My Data is like this
<str name="Key">8</str>
<arr name="city">
<str>Achabal (NAC)</str>
<str>Addi Gam</str>
<str>Adeh Hall</str>
<str>Aho Paisan</str>
<str>Akin Gam</str>
<str>Akura</str>
.......
</arr>
<str name="state">Chandigarh</str>
I want to search inside city filed i am trying the query as below
q=city:*Ak* AND state:Chandigarh <br>
But not working
Above mentioned data is single document
Multivalued fields have no difference with monovalued field (from query perspective). Note that in your query there's an error: SOLR doesn't support using a * symbol as the first character of a search.
See links below
http://wiki.apache.org/solr/AnalyzersTokenizersTokenFilters#solr.ReversedWildcardFilterFactory
http://solr.pl/en/2010/12/20/wildcard-queries-and-how-solr-handles-them
http://www.solrtutorial.com/solr-query-syntax.html
You should not use & in your query but + or %20 (space) instead. & separates url query parameters and state stuff will not be passed as value for q.
Try q=city:*Ak*+state:Chandigarh or q=city:*Ak* state:Chandigarh.
What you also probably would like to do is use filter query instead of query parameter here:
q=city*Ak*&fq=state:Chandigarh
This will query for all cities with 'Ak' and limit results only to ones with state='Chandigarh'
Related
I am using Solr version 5 for searching data. I am using below query which searches for keyword in all fields.
http://localhost:8983/solr/document/select?q=keyword1+keyword2&wt=json
Can anyone suggest me query to search for keyword only in title field.
Thanks.
use
http://localhost:8983/solr/document/select?q=title:*yourkeyword*&wt=json
or for exact match
http://localhost:8983/solr/document/select?q=title:"yourkeyword"&wt=json
You can not search for a keyword in all fields without some extra work:
How can I search all field in SOLR that contain the keywords,.?
The "q"-Parameter contains the query string and for the standard parser this means that you must specify the field via colon like in
fieldname:searchterm
or the standard parser will use the default field. The default field is specified in the "df"-Parameter and if you did not change your solrconfig.xml you will search in the "text"-Field because you will find something like
<requestHandler name="/select" class="solr.SearchHandler">
<lst name="defaults">
<str name="df">text</str>
</lst>
</requestHandler>
P.S. If you want to search in all fields you have either to copy all field-content to one field or you must use a specific query parser like dismax parser, where you can list all your fields in the "qf"-Parameter.
P.P.S. You can not search in all fields but you can highlight in all fields :-)
The best way is to run the query from Admin concole. When we run it, it also provides the actuall SQL query executed. Just copy the query and use it.
About the question: search specific field value from the Solr. In the admin console look for 'Q' text box. write the yourfield=value OR yourfield:value. Hit the 'Execute Query' button. Top right side the SQL will be available.
Generated Query: ......select?indent=on&q=YOURFIELD:"VALUE"&wt=json
I am having a list of profiles in solr, indexing with some empty fields (e.g.: country/category etc).
<arr name="country">
<str>185</str>
</arr>
<arr name="category">
<int>38</int>
</arr>
I want to search profiles with no country. I used -country:['' TO *] as country is a string field.
Now how can I check it for an integer field? The field may be empty (no field) or with value 0. I tried category:0 but it is not giving me the correct output, output is empty in this case.
For string type of field you could query like
-country:* OR country:""
It'll give you all the Solr document which doesn't have country field at all. Or country with empty string.
For integer type field
-category:* OR category:0
It'll give you result with empty(no field) or with value 0.
There is an issue with Solr OR and NOT search. Please refer to using OR and NOT in solr query for a better understanding of OR and NOT query.
Currently i have sample data like this :
<doc>
<int name="name">Nice Dress</int>
<arr name="keyword">
<str>best cocktail dress</str>
<str>platform complete pumps</str>
<str>platform pumps</str>
<str>slip dress</str>
</arr>
I used multiple value for "keyword" field.
case 1
defType:edismax
qf:keyword
q:cocktail dress
solr will return the data.
case 2
defType:edismax
qf:keyword
q:coctail dress pump
it still return the data, If we see from the sample data, no keyword contain all this 3 word ('coctail' 'dress' 'pump') in one row of each keyword.
How to make solr not to return this result?
Thanks.
Check for two parameters
positionIncrementGap - For multivalued fields this parameter would decide what is it distance between the two fields in the multivalued fields. If this value is 100 so the distance between the two multivalued fields would be 100 positions.
Note - The default positionIncrementGap is 0
Check for the qs query slop parameter for dismax which will will decide the slop match between the terms.
Try this query:
q:(coctail dress pump)~100
with your positionIncrementGap set to something like 300.
Those values will need to change depending on how long are your data.
Is it possible to search in Solr over two fields using two different words and get back only those results which contain both of them?
For example, if I have fields "type" and "location" , I want only those results who have type='furniture' and location = 'office' in them.
You can use boolean operators and search on individual fields.
q=type:furniture AND location:office
If the values are fixed, it is better to use Filter Queries for Performance.
fq=type:furniture AND location:office
The suggested solutions have the drawback, that you have to care about escaping special characters.
If the user searches for "type:d'or AND location:coffee break" the query will fail.
I suggest to combine two edismax handlers:
<requestHandler name="/combine" class="solr.SearchHandler" default="false">
<lst name="invariants">
<str name="q">
(_query_:"{!edismax qf='type' v=$uq1}"
AND _query_:"{!edismax qf='location' v=$uq2}")
</str>
</lst>
</requestHandler>
Call the request handler like this:
http://localhost:8983/solr/collection1/combine?uq1=furniture&uq2=office
Explanation
The variables $uq1 and $uq2 will be replaced by the request parameters uq1 and uq2 will.
The result of the first edismax query (uq1) is combined by logical AND with the second edismax query (uq2)
Solr Docs
https://wiki.apache.org/solr/LocalParams
You can also use the boostQuery function on the dismaxRequest handler as
type=dismax&bq=type:furniture AND location:office
fq=type:furniture AND location:office
Instead of using AND, this could be break into two filter queries as well.
fq=type:furniture
fq=location:office
I have an application where users can pick car parts. They pick their vehicle and then pick vehicle attributes as facets. After they select their vehicle, they can pick facets like engine size, for example, to narrow down the list of results. The problem was, not all documents have an engine size (it's an empty value in Solr), as it doesn't matter for all parts. For example, an engine size rarely matters for an air filter. So even if a user picked 3.5L for their engine size, I still wanted to show the air filters on the screen as a possible part the user could pick.
I did some searching and the following facet query works perfectly:
enginesize:"3.5" OR enginesize:(*:* AND -enginesize:[* TO *])
This query would match either 3.5 or would match records where there was no value for the engine size field (no value meant it didn't matter, and it fit the car). Perfect...
THE PROBLEM: I recently made the vehicle attribute fields multivalued fields, so I could store attributes for each part as a list. I then applied faceting to it, and it worked fine. However, the problem came up when I applied the query previously mentioned above. While selecting the enginesize facet narrowed down the number of documents displayed to only documents that have that engine size, records (I also use the word record to mean document) that had empty values (i.e. "") for enginesize were not appearing. The same query above does not work for multivalued facets the same way it did when enginesize was a single valued field.
Example:
<doc>
<str name="part">engine mount</str>
<arr name="enginesize">
<str/>
<str/>
<str>3.5</str>
<str>3.5</str>
<str>3.5</str>
<str>3.5</str>
<str>3.5</str>
</arr>
<doc>
<doc>
<str name="part">engine bolt</str>
<arr name="enginesize">
<str>6</str>
<str>6</str>
<str>6</str>
<str>6</str>
<str>6</str>
</arr>
<doc>
<doc>
<str name="part">air filter</str>
<arr name="enginesize">
<str/>
<str/>
<str></str>
<str></str>
<str></str>
<str></str>
<str></str>
</arr>
<doc>
What I am looking for is a query that will pull back documents 1 and 3 above when I do a facet search for the engine size for 3.5. The first document (the engine mount) matches, because it contains the value in one of the multivalued fields "enginesize" that I am looking for (contains 3.5 in one of the fields). However, the third document for the air filter doesn't get returned because of the empty <str> values. I do not want to return the second document at all because it doesn't match the facet value
I basically want a query that will match empty string values for a given facet and also match the actual value, so I get both documents returned.
Does someone have a query that would return document 1 and document 3 (the engine bracket and the air filter), but not the engine bolt document?
I tried the following without success (including the one at the very top of this question):
// returns everything
enginesize:"3.5" OR (enginesize:[* TO *] )
// only returns document 1
enginesize:"3.5" OR (enginesize:["" TO ""] AND -enginesize:"3.5")
// only returns document 1
enginesize:"3.5" OR (enginesize:"")
I imported the data above using a CSV file, I set the field keepEmpty=true. I tried instead manually inserting a space into the field when I generated the CSV file (which would give you <str> </str>, instead of the previous , and then retried the queries. Doing that, I got the following results:
// returns document 1
enginesize:"3.5" OR enginesize:(*:* AND -enginesize:[* TO *])
// returns all documents
enginesize:"3.5" OR (enginesize:["" TO ""] AND -enginesize:"3.5")
// returns all documents
enginesize:"3.5" OR (enginesize:"")
Does anyone have a query that would work for either situation, whether I have a space as the blank value or simply no value at all?
How about changing how you index, instead of how you query?
Instead of trying to index "engine size doesn't matter" as an empty record, index it as "ANY".
Then your query simply becomes enginesize:"3.5" OR (enginesize:ANY)
i've just been playing with this and found a hint that seems to do the trick for me. translated to your query it should be:
enginesize:"3.5" OR (-enginesize:["" TO *])
hth,
andi
update: after some more testing i don't think this works reliably — for some indexes it had to be the other way round and without the minus sign, i.e. enginesize:[* TO ""]. this might depend on the index type, if it's multi-valued or even on the actual values.
in any case it seems too much of a hack. i'll probably resolve to substituting the empty value with a special marker...
I had the same problem, but solved it in https://stackoverflow.com/a/35633038/13365:
enginesize:"3.5" OR (*:* NOT enginesize:["" TO *])
The -enginesize solution didn't work for me.