Count total docs containing specific field in Lucene index - lucene.net

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);

Related

How to create an index exemption on Firestore subdocuments?

We have a database structured as follows:
Collection foo
Documents
Collection bar
Documents with many fields (approaching the 1 MB limit)
Trying to write a document to the bar collection containing 34571 fields, I get (from the Go API):
rpc error: code = InvalidArgument desc = too many builtin index entries for entity
OK, fine, it seems I need to add an exemption:
Large array or map fields
Large array or map fields can approach the limit of 20,000 index entries per document. If you are not querying based on a large array or map field, you should exempt it from indexing.
But how? The console only lets me set a single collection name and a single field path, and slashes aren't accepted:
I tried other combinations, but / isn't accepted in either the Collection ID or the Field path, and using ., while not clearly forbidden, results in a generic error when trying to save the exemption. I'm also not sure if * is allowed.
Index exemptions are based on collection ID and not collection path. In this case, you can enter bar as the collection ID. This also means the exemption applies to all collections with ID bar, regardless of hierarchy.
As for the fields, you can specify only a single field path per exemption. The "*" all-selector is not supported. There is a limit of 200 index exemptions so you wouldn't be able to exempt all 34571 fields. If possible, I suggest moving your fields into a map. Then you could disable indexing on the map field.

How to check whether a certain field is indexed in mongodb or not?

I know db.collection.getIndexes() can return all the indexes in collection, but sometimes it will return a long result if collection has many indexes, and this will make it difficult for you to find whether a certain field is indexed.
So how can you check whether a certain field is indexed in mongodb?
Yes, you can easily view whether you field is indexed or not using below query:
db.collection.stats().indexSizes().c2_YOUR_FIELD_NAME
In above query, use your own collection name and field name. If it returns some value on console, the field is indexed.
THIS will give you much indepth-knowhow

How to search through a nested field which is a list in MongoDB

I would like to search for a pattern, for each element in a list, which is a nested field in MongoDB.
I have an Object, which contains authors list, which can contain from 0 to N elements. I want to retrieve objects whose authors met the search pattern. An author contains 4 fields, I only scan for names and surnames. How to do that within MongoDB query?
You could use a query like:
db.article.find({$and:[{"authors.givenNames":"javier"},{"authors.familyName":"XXXXX"}]})
You can read about it in This link
Hope it helps you!

Searching a value in a specific field in MongoDB Full Text Search

(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.

Prefixquery and sort in lucene

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);