# Fuzzy Search

Procedure to integrate Elastic Search for Fuzzy search with any of the eGov modules.

**Creating the index as per the requirement**

1. The elastic index must be used for fuzzy search to improve performance, and when PII fields are involved, which cannot be accessed directly in the DB, to apply fuzzy logic.
2. When PII data are involved, those fields shouldn’t be returned by Elasticsearch; they can only be used to apply filters. To enable this, that particular field should be indexed but not stored.

**Example:**

```
{
  "mappings": {
    "general": {
      "_source": {
        "excludes": [
          "name"
        ]
      },
      "properties": {
        "id": {
          "type": "text"
        },
        "dob": {
          "type": "date"
        },
        "name": {
          "type": "text"
        }
      }
    }
  }
}
```

In the example above, when data is posted, all fields are stored, but the index never returns excluded source fields when searched.

**Querying an index**

```
curl --location --request GET 'localhost:9200/_search?pretty' \

--header 'Content-Type: application/json' \
--data-raw '{
    "query": {
        "fuzzy": {
            "name": {
                "value": "ki"
            }
        }
    }
}'
```

This is a simple request; additional parameters can be used to enhance the fuzzy search.

```
curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "fuzzy": {
      "name": {
        "value": "ki",
        "fuzziness": "AUTO",
        "max_expansions": 50,
        "prefix_length": 0,
        "transpositions": true
      }
    }
  }
}
'
```

**Elastic Integration with code**

1. Add the Elasticsearch host from the eGov cluster.
2. Divide the search params between the ones that will be sent to Elastic and others to DB (search to Elastic should be made only if the fuzzy fields are present in the search request)
3. The result of the Elasticsearch query should be only the required primary ID of the entity (property-id, trade license-id, etc.).
4. Then the resulting IDs should be used in the standard search as-is.

Please refer to the following [commit](https://github.com/egovernments/municipal-services/commit/9917276346b6a0136a9afef87c1b93e2f4ec5746) for the integration

**Application Properties**

Properties need to be added for integration

```
#Elastic search properties

elasticsearch.host=http://localhost:9200/
elasticsearch.search.endpoint=/_search
property.es.index=property-services
pt.search.name.fuziness=2
pt.search.doorno.fuziness=2
pt.search.oldpropertyid.fuziness=2
pt.fuzzy.searh.is.wildcard=true
```

The indices need to be reindexed for fuzzy search to work if the index is modified, particularly when protected fields are indexed but not returned in search results.

**Reindexing for PT**: [Refer here](/local-governance/access/local-governance-stack/property-tax/property-tax-service/fuzzy-search/fuzzy-search-reindexing.md).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.digit.org/local-governance/access/local-governance-stack/property-tax/property-tax-service/fuzzy-search.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
