Setting pseudo fields (fl) in Solrj - java

I want to use a pseudo field to return the distance from the center of my solr (geo) spatial search, like it's explained here: http://wiki.apache.org/solr/SpatialSearch#geodist_-_The_distance_function when it says:
Returning the distance
Solr4.0
You can use the pseudo-field feature to return the distance along with the stored fields of each document by adding fl=geodist() to the request. Use an alias like fl=dist:geodist() to make the distance come back in the dist pseudo-field instead. Here is an example of sorting by distance ascending and returning the distance for each document in dist.
...&q=:&sfield=store&pt=45.15,-93.85&sort=geodist() asc&fl=dist:geodist()
Now, I'm using solrj (4.5.1) and I can't find a way to set the fl=_dist_:geodist() part properly. I can actually manage to add it to the solrQuery object doing:
solrQuery.setParam('fl', '_dist_:geodist()')
with no compilation errors, but for some reason this is messing up my returned documents.
Any ideas how it should be done?
Ps. code is in groovy language, don't freak out for no semi-colons or string within single quotes :)
* UPDATE *
Setting the fl param as explained above, actually results in returning documents which only contains the _dist_ field!

After a few minutes of search, i found this article: http://solr.pl/en/2011/11/22/solr-4-0-new-fl-parameter-functionalities-first-look/
It explains how to return the new alias field(s) in addition to all other parameters, simply like this (please note the * part):
fl=*,stock:sum(stockMain,stockShop)
So, in my example for solrj, it will be:
solrQuery.setParam('fl', '*,_dist_:geodist()')

Related

Java serialization of a query to list of strings

I stumbled upon a strange issue and cant find quick solution to deal with it. Front side of my application sends query to back-end I added square brackets for readability.
as it should be
http://localhost:3000/?params=[First_value],[Anhui Jantene Hone Textiles Co., Ltd.],[Third_Value]
as it is
http://localhost:3000/?params=[first_value],[Anhui Jantene Hone Textiles Co.], [Ltd.],[Third_Value]
params are serialized to List but there is a problem when given value on a front-side has comma in the name, then instead of one value I get 2 entries. In a given example expected size of a list should be 3 but I get 4 elements.
Any help is appreciated
You can't do this in the query string because of how a comma is treated. You need to use a POST with a request body like this:
["First_value", "Anhui Jantene Hone Textiles Co., Ltd.","Third_Value"]

How do I define "params" in GLPK for Java API?

I was using glpsol with a .mod file containing both the problem and the data for it.
However, I want to use its Java API to instantiate the problem within my application, without the need to write/read files and run them with glpsol.
In my problem, I have "sets" that are given afterwards in the data section, and also params in function of these sets, for example:
set ROBOTS;
param L{ROBOTS}, integer;
And then, at the data section:
data;
set ROBOTS := ag1 ag2 ag3;
What I want to know is what method can I use to add such params to the problem, as well as how to retrieve them.
In order to observe how this problem was being represented, I've tried reading the problem and the data from files and extracted the rows and cols of the problem through the methods glp_get_row_name and glp_get_col_name. I came to the conclusion that the rows are the objective and constraints, whilst the columns are the values of a var f that is declared as follows and used in some of the constraints as well as in the objective:
var f{ROBOTS,SUBTASKS}, binary;
I could not find in the documentation a way to extract these params from the problem. Also, I have no idea about where my other vars went, since only f appeared in the columns. But as the program was able to solve the instantiated problem and had the same result as the solution given by glpsol, I know that it has all of this data, I just want to know where it stores it.
I was reading the documentation from here: http://glpk-java.sourceforge.net/apidocs/org/gnu/glpk/GLPK.html
Sorry for the lack of correct terminology. Thanks in advance.
var f{ROBOTS,SUBTASKS}, binary;
ROBOTS and SUBTASKS only exist in the GMPL language model.
After the model is translated the problem is stored as a sparse matrix. You only have column numbers and row numbers for addressing.

Using Apache Solr's boost query function with Spring in Java

I'm writing a Java application that is using Apache Solr to index and search through a list of articles. A requirement I am dealing with is that when a user searches for something, we are supplying a list of recommended related search terms, and the user has the option to include those extra terms in their search. The problem I'm having, however, is that we want the user's original search term to be prioritized, and results that match that should appear before results that only match related terms.
My research suggests that Solr's boost function is the solution for this, but I'm having some trouble getting it to work with Spring. The code all runs fine and I get my search results as expected, but the boost function doesn't seem to actually be re-ordering my searches at all. For example, I'm trying to do something like this:
Query query = new SimpleQuery();
Criteria searchCriteria = Criteria.where("title").contains("A").boost((float) 2);
Criteria extraCriteria = Criteria.where("title").contains("B").boost((float) 1);
query.addCriteria(searchCriteria.or(extraCriteria));
In this example I would be searching for any document whose title contains "A" or "B", but I want to boost results that match "A" to the top of the list.
I've also tried using the Extended DisMax Query Parser with a different syntax to achieve the same result, with similar lack of success. To follow the same example pattern, I'm trying to use the expression criteria as follows:
Query query = new SimpleQuery();
Criteria searchCriteria = Criteria.where("title").expression("A^2.0 OR B^1.0");
query.setDefType("edismax");
query.addCriteria(searchCriteria);
Again I would expect this to return documents with titles matching "A" or "B" but boost results matching "A", and again it simply doesn't seem to actually affect the ordering of my results at all.
Okay, I figured out the problem here. Elsewhere in the code someone else had added this snippet:
query.setPageRequest(pageable);
This was done to support pagination of the search results, but the pageable object ALSO contained some sort orders that looks like they got added to the query as part of the .setPageRequest method. Something to look out for in the future, it looks like sorts override boosting when working with Spring Solr queries in this scenario.

Lucene searching by numeric values

I'm building a Java Lucene-based search system that, on addition, adds a certain number of meta-fields, one of which is a sourceId field, which denotes where the entry came from.
I'm now trying to retrieve all documents from a particular source, but the index doesn't appear to be able to find them. However, if I search for a wildcard value, the returned documents all have the correct value for this field.
The lucene query I'm using is quite simple, basically index-source-id:1 but that fails to return any hits, if I search for content:a* I get dozens of documents, all of which, when asked, return the value 1 for the index-source-id value, which is correct.
Any ideas?
I have only worked with the PHP port, however, have you checked what text analyzer you are using? This FAQ seems to indicate that like the PHP version, you need to use a diffrent one that doesn't remove digits.
You can find a list of analyzers here
Just to be sure, you have set the id to be indexable?

lucene set boost on fields at search time

Is it possible to adjust the boost of a field with the Query object before running the search?
I know the proper way to do it is to change the fields boost during indexing, but it takes about 4 days to make an index and was just wondering if there's a quick hack i can do for now.
also i have tried hardcoding in the boost to the search query, ie
AND field(this that other)^7
and that works, and it would be the end of it, EXCEPT i want to reduce the relevance of this part of the query,
i want
AND field(this that other)^.1
but i get empty results.
thanks
You can extend Similarity and in the Searcher, use
setSimilarity(Similarity)
By extending Similarity, you can adapt the scoring mechanism in Lucene to your needs.
EDIT:
More specifically, you can override the lengthNorm method in Similarity (or a subclass thereof):
public float lengthNorm(String fieldName, int numTokens){
return fieldWeights.get(fieldName)*super.lengthNorm(fieldName, numTokens);
}
fieldWeights could be a Map attribute in which you specify the weight you want to attach to each field. If you keep a reference to fieldWeights somewhere, you can change the field weights to whatever you want just before you perform a search (But do this for only one query at a time, to experiment).
Why not just boost the terms you want with a higher value and leave the ones you are trying to un-boost at zero?

Categories