I use lucene.net. I indexed documents with two fields. First field is name and second field is count.
I use this index for autocomplete. I need find 10 names by prefix and the higest count.
How can I do it?
try something like
Sort sort = new Sort(new SortField("countfieldname", SortField.INT,true));
PrefixQuery pq = new PrefixQuery(new Term("namefieldname", "prefix"));
indexSearcher.Search(pq,null,10, sort);
Related
(MongoDB Full Text Search)
Hello,
I have put some fields in index and this is how I could search for a search_keyword.
BasicDBObject search = new BasicDBObject("$search", "search_keyword");
BasicDBObject textSearch = new BasicDBObject("$text", search);
DBCursor cursor = users.find(textSearch);
I don't want search_keyword to be searched in all the fields specified in the index. *Is there any method to specify search_keyword to be searched in specific fields from the index??*
If so, please give me some idea how to do it in Java.
Thank you.
If you want to index a single field and search for it then it is the way it works by default. Lets say you want to index the field companyName. When you perform $text search on this collection, only the data from the companyName field will be used because you only included that field in your index.
Now the second scenario, your $text index includes more than one field. In this case you cannot limit the search to only look for values indexed from a specific field. The $text index is constructed on the collection level and a collection can have at most one $text index. Your option to limit search on specific field in this case may be to use regex instead.
MongoDB has the flexibility to fulfil requirements of other scenarios, but you can also evaluate using other technologies if your application is heavily search-driven and you are primarily after a full-text search engine for locating documents by keyword with a rich query syntax. ElasticSearch might be an alternative here. It really depends on the type of the application and your needs.
I want to query item from mongoDB using morphia, but I found the order method has only one parameter, can I query data with multi order field like:
List items = Item.q().order("-updateTime").order("-createTime");
You just separate the fields with commas. The wiki has examples: https://github.com/mongodb/morphia/wiki/Query#sort
You can use
List items = Item.q().order("-updateTime,createTime,otherField").asList();
I am using MongoDB for our Application with java as shown below .
I am making a below query as shown below :
public Set<String> findDuplicateEquities() {
BasicDBObject query = new BasicDBObject();
BasicDBObject sort = new BasicDBObject();
query.put("symbol", "SUNG");
sort.put("price", "1");
DBCursor cursor = collection.find(query).sort(sort);
Do i need to create the index for the price field also , as i am usng it for the sort purpse ??
It depends on how many documents are returned by find({symbol:"SUNG"}). When only a handfull of entries are returned which then need to be sorted, I wouldn't bother. But when this query returns several hundred documents or more, creating a compound index including price could help, because then the entries can already be retrieved in a sorted order from the index and don't have to be sorted afterwards.
Note that an index which is supposed to speed up a find( ... ).sort( ... ) needs to be a compound index which starts with all fields which are matched exactly by the find-query followed immediately by the fields you are sorting by in the correct order and direction. For more information read the documentation about compound indexes.
I'm trying to do create a query to exclude all documents which have an empty/null value in one specific field.
What is the query syntax or the programmatic way to do this?
You can use a required range query, which is open at both sides, like:
+field:[* TO *]
That is probably adequate, assuming that the documents to exclude have no value in the index.
If some form of default value appears, you would have to exclude that value as well, like:
+field:[* TO *] -field:NULL
I am trying to run a query on Lucene .NET 2.9.2 index without any luck:
My index holds documents, some of them contain numeric field called "MyNum" and some of them are not.
The field is indexed.
I am trying to count the total documents that contain the field, no matter the fields value.
Could some one please help me out?
A query like fieldX:* should return all of the documents that contain field "fieldX".
You may need to allow for a prefixed * in your search (I don't have a copy of Lucene up at the moment.)
You may use the wildcard query to retrieve all documents with specific field. Just provide the * as value (this is just regular wildcard). Here is the sample code:
IndexSearcher searcher = new IndexSearcher(reader);
TopDocs docs = searcher.Search(new WildcardQuery(new Term("MyNum", "*")), int.MaxValue);
Console.WriteLine(docs.TotalHits);