Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search by field in Lucene

Tags:

lucene

Although being a total newbie, may be this question is pretty naive. I want to search my index based on the index. So I tried created a document with just one index, Name, and then want to search for that particular field.

I am doing this in process of trying to find out if I can update the fields of a document without actually deleting a document in lucene.

Thanks.

like image 709
Sunil Avatar asked Oct 13 '10 03:10

Sunil


People also ask

How do you search in Lucene?

Lucene supports fielded data. When performing a search you can either specify a field, or use the default field. The field names and default field is implementation specific. You can search any field by typing the field name followed by a colon ":" and then the term you are looking for.

How do you write a Lucene query?

A query written in Lucene can be broken down into three parts: Field The ID or name of a specific container of information in a database. If a field is referenced in a query string, a colon ( : ) must follow the field name. Terms Items you would like to search for in a database.

What is Boolean query in Lucene?

BooleanQuery is used to search documents which are a result of multiple queries using AND, OR or NOT operators.

Is Lucene is same as Elasticsearch?

Elasticsearch is built over Lucene and provides a JSON based REST API to refer to Lucene features. Elasticsearch provides a distributed system on top of Lucene. A distributed system is not something Lucene is aware of or built for. Elasticsearch provides this abstraction of distributed structure.


1 Answers

You can search for words within a particular field with the colon syntax i.e. name:john.

But because a lot of indexes just have one field you are going to want to search on, there is a default field, in case you just search for john. You can set which field that is when you instanciate your QueryParser

QueryParser parser = new QueryParser(Version.LUCENE_30, "name", anAnalyzer);
Query q = parser.parse("john");

If you want to create your queries programmatically rather than parsing a user-entered query string, then you also have to specify the field explicitly, for example:

Query q = new TermQuery(new Term("name", "john"));

Links: Using fields in Lucene queries (Lucene Query Syntax) | QueryParser Javadoc | TermQuery Javadoc

like image 129
Adrian Smith Avatar answered Sep 28 '22 02:09

Adrian Smith