Index Creation and Document Addition Flashcards
Index Creation
PUT /my_index
Add Documents
POST /my_index/_doc/1
{
“name”: “John Doe”,
“age”: 30,
“location”: “New York”
}
Basic Search
GET /my_index/_search
{
“query”: {
“match”: {
“name”: “John”
}
}
}
This will return documents in my_index where the name field matches “John”.
Match Query
GET /my_index/_search
{
“query”: {
“match”: {
“content”: “Elasticsearch tutorial”
}
}
}
The above query searches for documents where the content field contains terms related to “Elasticsearch tutorial”.
Term Query
GET /my_index/_search
{
“query”: {
“term”: {
“status”: “published”
}
}
}
This example searches for documents where the status field exactly matches “published”.
Multi Match Query
GET /my_index/_search
{
“query”: {
“multi_match”: {
“query”: “Elasticsearch tutorial”,
“fields”: [“title”, “content”, “tags”]
}
}
}
This query searches for documents where “Elasticsearch tutorial” appears in any of the specified fields (title, content, or tags).
Match Phrase Query
GET /my_index/_search
{
“query”: {
“match_phrase”: {
“content”: “quick brown fox”
}
}
}
This query finds documents where the phrase “quick brown fox” appears in the content field, in that exact order.
Match All Query
GET /my_index/_search
{
“query”: {
“match_all”: {}
}
}
The match_all query returns all documents in the index. It is often used as a starting point for filtering or sorting the entire data set.
Range Query
GET /my_index/_search
{
“query”: {
“range”: {
“publish_date”: {
“gte”: “2023-01-01”,
“lte”: “2023-12-31”
}
}
}
}
This query searches for documents where the publish_date falls within the specified range.
Boolean Search
GET /my_index/_search
{
“query”: {
“bool”: {
“must”: [
{ “match”: { “title”: “Elasticsearch” } }
],
“must_not”: [
{ “term”: { “status”: “archived” } }
],
“should”: [
{ “match”: { “tags”: “search” } }
]
}
}
}
In this example:
The must clause requires the title field to contain “Elasticsearch”.
The must_not clause excludes documents where the status is “archived”.
The should clause boosts the relevance of documents that also have “search” in the tags field.
Wildcard Query
The wildcard query allows using wildcard characters like * (matches zero or more characters) and ? (matches a single character).
{
“query”: {
“wildcard”: {
“username”: “user*”
}
}
}
Prefix Query
{
“query”: {
“prefix”: {
“product”: “ela”
}
}
}
This query finds documents where the product field starts with “ela” (e.g., “elastic”, “elaborate”).
Exists
{
“query”: {
“exists”: {
“field”: “tags”
}
}
}
This query retrieves documents that have a non-null tags field.
Fuzzy Query
{
“query”: {
“fuzzy”: {
“name”: {
“value”: “elastik”,
“fuzziness”: 2
}
}
}
}
This query finds documents where the name field is similar to “elastik” with up to two character changes.