Recently in my project, we started working on improving our Search Capability. We were using the couchbase full-text search and wanted to move to something more powerful and dynamic. After going through multiple suggestions, we decided to go ahead with Elastic Search. This post includes all the necessary APIs you would need to build a simple search on Elastic, using Kibana.
In these all examples, we have considered our Index name as Cat.
- To get all the Indexes in the Elastic, use this.
GET /_cat/indices?h=index
- Now, to create an Index in Elastic, make a PUT request with the index name.
PUT cat
- To get the Index Mapping, use
GET cat/_mapping
- Delete Index- In case you want to remove an index altogether, just make a delete request in Kibana with the index name.
DELETE cat
- Reindexing-Sometimes you will face an issue, where you have to copy all the documents from one index to another. So, in that case, _reindex will come in the picture, just enter the source and destination index name, it will copy the documents. You can also add ingest pipeline option, in case you want all your documents to run through this pipeline before indexing, this is optional.
POST _reindex
{
"source": {
"index": "cat"
},
"dest": {
"index": "cat_backup",
"pipeline": "some_ingest_pipeline"
}
}
- Delete one document- In case you want to delete a document from one of the indexes, just follow the below API syntax.
DELETE cat/_doc/123456789
Now, As you are up and running with the Elastic Index creation. Let's talk about a few requests with basic elastic search queries.
- To delete documents matching specific conditions, use Delete By Query calls.
POST /cat/_delete_by_query
{
"query": {
"match_all": {}
}
}
- Get count of all Documents, in an index.
GET cat/_count
- To search for documents with id.
GET cat/_search
{
"query": {
"match": {
"_id": "97351395-2241-4287-953e-6987dbf93827"
}
}
}
- The above query will return all the fields of the document, but in case you only need some specific fields. Use the query with _source key, which allows us to pass the fields which we need in the document.
GET cat/_search
{
"_source": [
"title",
"description"
],
"query": {
"match": {
"_id": "97351395-2241-4287-953e-6987dbf93827"
}
}
}
- If you are not aware of the field, where you will get the search term, and want to search all the fields for the search text, then use query_string
GET cat/_search
{
"query": {
"query_string": {
"query": "kiwi"
}
}
}
- To search for the exact terms, use match_phrase.
GET cat/_search
{
"query": {
"match_phrase": {
"name": "deals of the day"
}
}
}
- To search for the parent document using child, try the below request.
GET cat/_search
{
"query": {
"parent_id": {
"type": "child",
"id": "00000001"
}
}
}
- Using the below query, you can search for a document, where the colour field is either black or white, whereas the breed is Persian.
GET cat/_search
{
"query": {
"bool": {
"must": {
"match": {
"breed": "Persian"
}
},
"should": [
{
"match": {
"color": "black"
}
},
{
"match": {
"color": "white"
}
}
],
"minimum_should_match": 1
}
}
}
- In case you are using any analyzer in the index, so to verify how search terms will behave use analyze requests. As result, you will get the tokens, on which the actual search will take place.
GET cat/_analyze
{
"analyzer": "ascii-stop-analyzer",
"text": "aรงaรญ a la carte"
}
That's a wrap, for now, Further, I will update this post with the component template, index template, Ingest Pipeline and Index Mapping.
Please Ignore the spacing issue in query JSONs because of the markdown format.
Important Links:
Top comments (0)