Realm.io [java] search by combined field - java

I have User model with Name and Surname properties, and I need query to search by name.
This code is now:
query
.beginGroup()
.contains("name", search, Case.INSENSITIVE)
.or()
.contains("surname", search, Case.INSENSITIVE)
.endGroup()
.findAll();
But, if i want to search Jerry Smith, i'll write "jerry smi" and won't get what i want, obviously because there's or. How should i do this?
I'm going to create and maintain fullname field, setting it on setters of name/surname and search by it, is it good path?

The easiest way would be to add a third field called "full name" where you put name + surname. Then you'd only search like this:
query.contains("full name", search, Case.INSENSITIVE).findAll();

RealmResults<Event> toEdit = realm.where(Event.class)
.equalTo("name", day)
.equalTo("surname", month)
.findAll();
Hope this will help ! Cheers !

Related

Find a node using partial searches

sample:
...
<PEOPLE>
<PERSON>JOHN, the fighter<PERSON/>
<PERSON>SALLY, the visionary<PERSON/>
<PGROUP>
<PERSON>YOYO<PERSON/>
<PERSON>BENJI<PERSON/>
</PGROUP>
<PERSON>builders<PERSON/>
<PERSON>cleaners<PERSON/>
<PGROUP>
<PERSON>Clowny, good person<PERSON/>
<PERSON>Clownee, bad person<PERSON/>
</PGROUP>
<PERSON>Gentleman<PERSON/>
</PEOPLE>
...
I want to plug in a string and find that person (if exists):
John,
builder,
clown (should return the first occurrence => Clowny, good person),
fighter,
...
I have tried these things but none seem to give me a result or a result i want:
//PERSON will select all the persons; contains(Person,'"+searchStr+"'); //PERSON[1] will select 3 persons; //PERSON[PERSON='YOYO'] doesnt work
none of these have worked so far. I want the full name to be returned.
EDIT:
This returns 1 if the searchStr is found (input is exact). without count it returns PEOPLE. Appending the expression with /PERSON always returns the first person if the person searched for was found.
count(//PEOPLE[//PERSON="Clowny, good person"])
I am going to leave this question up if somebody needs it in the future.
The right approach is to look for the text()
//PERSON[contains(text(),"vision")]/text()
Selecting the PERSON where the PERSON's text() contains the searchStr then return it's text()

Solr search not working properly

I am searching for String Kansas City in description field.
"q":"description: *Kansas City*", but I am getting the results for both Kansas and City. Also it is getting the results from content field as well. I am not sure why it is fetching results from content field. Please suggest me if I am doing any error in my query.
Your quoting is wrong
description:"kansas city"
for example
What are the stars for?
After tokenizing and parsing query it looks like kansas city is tokenized into "kansas" and "city" and filters are applied as per fieldtype definition.
then they are searched in fieldname specified.
description:*Kansas
after tokenizing/word splitting, "city" becomes
different word for which you didn't specify fieldname. so by default it is searched in defaultfield(which is may be content in your case)
defaultsearchfield:city*
in your case after parsing description:kansasandcontent:city you can see the same debugQuery=on with URL in your browser.

How to handle #select in Play! Framework

I'm trying to fill a combo using the helper #select of Play! in a form, but searching a lot, I not found what I exactly need.
I want something like this:
#select(
filmeForm("Director"),
options(Seq(aListOfDirectors))
)
This is a form of register of movies, that get Foreign key of a Director.
I need list the directors names, and when I send the form, I need to get the ID of selected director.
If this way is not possible, some similar way will be useful.
Can anyone help me?
Thanks in advance.
HTML select tag can have a set of option tags like the following:
<select id="directors-select" name="director">
<option value="steven-spilberg">Steven Spilberg</option>
<option value="stanley-kubric">Stanley Kubric</option>
</select>
So, to proper populate select's options, Play #select helper requires that the Seq contains a tuple (String, String), which will contains both the value attribute and also the "label" presented to the user. In other words, options parameter needs to be a Seq[(String, String)]. Here is the example given at the docs:
#select(
field = myForm("mySelect"),
options = Seq(
"Foo" -> "foo text",
"Bar" -> "bar text",
"Baz" -> "baz text"
),
'_default -> "Choose One",
'_disabled -> Seq("FooKey", "BazKey")
'cust_att_name -> "cust_att_value"
)
So, your aListOfDirectors needs to contains a (String, String) tuple. But it is actually pretty simple to solve this, just change your code to:
#select(
field = filmeForm("Director"),
options = aListOfDirectors.map(director => director.id.toString -> director.name)
)
Here, I'm considering that aListOfDirectors is a Seq[Director].
Where is it documented?
Play documentation for (Java) forms states that "there are several input helpers in the views.html.helper package." After that, I just looked up at the play scaladocs and then navigate to views.html.helper package. There you can find the docs for #select.
I know that when people says "read the docs" that sometimes sounds harsh, but it is a good advice considering that framework/software/lib developers (who really knows the framework/software/lib) spent their time to explain how to use the framework/software/lib, we for sure can spend some time reading the docs.

How to retrieve the Field that "hit" in Lucene

Maybe I'm really missing something.
I have indexed a bunch of key/value pairs in Lucene (v4.1 if it matters). Say I have
key1=value1 and key2=value2, e.g. as read from a properties file.
They get indexed both as specific fields and into a catchall "ALL" field, e.g.
new Field("key1", "value1", aFieldTypeMimickingKeywords);
new Field("key2", "value2", aFieldTypeMimickingKeywords);
new Field("ALL", "key1=value1", aFieldTypeMimickingKeywords);
new Field("ALL", "key2=value2", aFieldTypeMimickingKeywords);
// then get added to the Document of course...
I can then do a wildcard search, using
new WildcardQuery(new Term("ALL", "*alue1"));
and it will find the hit.
But, it would be nice to get more info, like "what was complete value (e.g. "key1=value1") that goes with that hit?".
The best I can figure out it to get the Document, then get the list of IndexableFields, then loop over all of them and see if the field.stringValue().contains("alue1"). (I can look at the data structures in the debugger and all the info is there)
This seems completely insane cause isn't that what Lucene just did? Shouldn't the Hit information return some of the Fields?
Is Lucene missing what seems like "obvious" functionality? Google and starting at the APIs hasn't revealed anything straightforward, but I feel like I must be searching on the wrong stuff.
You might want to try with IndexSearcher.explain() method. Once you get the ID of the matching document, prepare a query for each field (using the same search keywords) and invoke Explanation.isMatch() for each query: the ones that yield true will give you the matched field. Example:
for (String field: fields){
Query query = new WildcardQuery(new Term(field, "*alue1"));
Explanation ex = searcher.explain(query, docID);
if (ex.isMatch()){
//Your query matched field
}
}

java, Morphia how to compare strings when using the Morphia Find Methods

Im new to this so here goes.
Trying to get a user called "Bob" from the MongoDb.
I have the:
UserData ud = MonConMan.instance().getDb().find(UserData.class, "name","bob").get();
The "bob" cannot be found if it has capital "Bob".
I understand i can get a List and do equalsIgnoreCase but are
there some Operators i can use?
I have users logging on and must test to see if they are registered. A user can type his name anyway he likes so must find a way to equalsIgnoreCase. Yea this is a problem, i cannot get all names and do equalsIgnoreCase, if there are like 10,000. One could of course initially save all user names in lowercase but that would destroy the visual appearance of the name.
looking at the wiki but cannot see any..
http://code.google.com/p/morphia/wiki/Query
Use java regex, like this.
String name = "bob";
Pattern pattern = Pattern.compile("^" + bob + "$", Pattern.CASE_INSENSITIVE);//This line will create a pattern to match words starts with "b", ends with "b" and its case insensitive too.
Query<UserData> query = createQuery().field("name").equal(pattern).retrievedFields(true, "id");//Replace `id` with what ever name you use in UserData for '_id'
UserData user = query.get();
if(user!=null){
//he is already registered
}
else{
//He is a new guy
}
(I am not good at regex, so you may have read about$&^somewhere. )
You should be sure that the user names you are using to validate a new user should be unique across your system.
Ended up keeping two fields like
- lowercaseusername
- originalusername
This way i could search for a user using the lowercaseusername
You can make find a name of a UserData using this code :
Query<UserData> query = createQuery().filter("name","bob");
find(query);
In my application, this code return all UserData that haves a field name with "bob" value.
The code can be this way too :
Query<UserData> query = createQuery().field("name").equal("bob");
find(query);
These codes will be in a UserDataDao that extends BasicDao, and receives in the construtor the datastore from morphia.

Categories