# Class: Index

## helma.Search.Index(directory, analyzer)

Instances of this class represent a Lucene search index located in either a directory on disk or in RAM. This class provides various methods for modifying the underlying Lucene index.

## Constructor

#### new Index(directory, analyzer)

Creates a new instance of helma.Search.Index

##### Parameters:

Name | Type | Description
--- | --- | ---
`directory` | org.apache.lucene.store.Directory | The directory where the Lucene index is located at.
`analyzer` | org.apache.lucene.analysis.Analyzer | The analyzer to use when modifying the index.

Source:
  - helma/Search.js, line 260

### Methods

#### addDocument(doc)

Adds a document to the index.

##### Parameters:

Name | Type | Description
--- | --- | ---
`doc` | helma.Search.Document | The document to add to the index.

Source:
  - helma/Search.js, line 487

#### addDocuments(docs)

Adds all documents in the passed collection to this index.

##### Parameters:

Name | Type | Description
--- | --- | ---
`docs` | java.util.Hashtable | java.util.Vector | Array | The documents to add to the index.

Source:
  - helma/Search.js, line 504

#### addIndexes(dir)

Merges the indexes passed as argument into this one.

##### Parameters:

Name | Type | Description
--- | --- | ---
`dir` | org.apache.lucene.store.Directory | At least one index director to add to this index.

Source:
  - helma/Search.js, line 335

#### close()

Closes the underlying index directory for future operations.

Source:
  - helma/Search.js, line 478

#### count(fieldName, fieldValue)

Returns the number of documents with the given field name and value.

##### Parameters:

Name | Type | Description
--- | --- | ---
`fieldName` | String | The name of the field
`fieldValue` | String | The value of the field

Source:
  - helma/Search.js, line 405

##### Returns:

The number of documents

#### create()

Creates a new index. This will delete any existing index files in the directory of this index.

Source:
  - helma/Search.js, line 359

##### Returns:

True if creating the index was successful, false otherwise

#### getAnalyzer()

Returns the analyzer used within this index.

Source:
  - helma/Search.js, line 309

##### Returns:

The analyzer used within this index.

#### getDirectory()

Returns the directory the underlying Lucene index is located at.

Source:
  - helma/Search.js, line 300

##### Returns:

The directory of this index

#### getFieldNames()

Returns an array containing all field names in this index.

Source:
  - helma/Search.js, line 437

##### Returns:

An array with the field names in this index.

#### getReader()

Returns an IndexReader instance. Due to locking issues an IndexModifier should be used for deleting documents.

Source:
  - helma/Search.js, line 291

##### Returns:

An IndexReader instance

#### getSearcher()

Returns a searcher for querying this index.

Source:
  - helma/Search.js, line 318

##### Returns:

A searcher useable for querying the index.

#### getWriter(create, autoCommit)

Returns an IndexWriter instance that can be used to add documents to the underlying index or to perform various other modifying operations. If the index is currently locked this method will try for the next two seconds to create the IndexWriter, otherwise it will throw an error.

##### Parameters:

Name | Type | Description
--- | --- | ---
`create` | Boolean | True to create the index (overwriting an existing index), false to append to an existing index. Defaults to false
`autoCommit` | Boolean | Enables or disables auto commit (defaults to false)

Source:
  - helma/Search.js, line 275

##### Returns:

An IndexWriter instance.

#### isLocked()

Checks if the index is locked.

Source:
  - helma/Search.js, line 461

##### Returns:

True if the underlying index is locked, false otherwise.

#### optimize()

Optimizes the underlying index.

Source:
  - helma/Search.js, line 420

#### removeDocument(fieldName, fieldValue)

Remove all documents from the index whose field-value with the given name matches the passed value argument.

##### Parameters:

Name | Type | Description
--- | --- | ---
`fieldName` | String | The name of the field
`fieldValue` | String | The value of the field.

Source:
  - helma/Search.js, line 534

#### removeDocuments(fieldName, values)

Removes all documents whose field with the given name matches the values passed as argument.

##### Parameters:

Name | Type | Description
--- | --- | ---
`fieldName` | String | The name of the field
`values` | java.util.Hashtable | java.util.Vector | Array | The values that define the documents that should be removed from the index.

Source:
  - helma/Search.js, line 556

#### size()

Returns the number of documents in this index.

Source:
  - helma/Search.js, line 386

##### Returns:

The number of documents in this index.

#### unlock()

Unlocks the index. Use this with caution, as it removes any active locks in the Lucene index, which might lead to index corruption.

Source:
  - helma/Search.js, line 470

#### updateDocument(docObj, fieldName)

Updates the index with the document passed as argument. In contrast to addDocument() this removes any existing objects whose fieldName matches the one of the document object. Eg. if the document object has a field "Id" with the value "123", all document objects whose fieldName "Id" matches "123" will be removed from the index before.

##### Parameters:

Name | Type | Description
--- | --- | ---
`docObj` | helma.Search.Document | Document object to add to index.
`fieldName` | String | The name of the identifier field.

Source:
  - helma/Search.js, line 594