Giter Club home page Giter Club logo

elasticsearch-tips's Introduction

Elasticsearch Tips

Assuming that we have our Elasticsearch available at http://localhost:9201

Creating an index

Create an index called "blog" with a custom analyzer:

curl -X PUT -H "Content-Type: application/json" localhost:9201/blog \
-d @- << EOF
{
    "settings": {
        "analysis": {
            "filter": {
                "my_stemmer": {
                    "type": "stemmer",
                    "name": "english"
                }
            },
            "analyzer": {
                "my_custom_analyzer": {
                    "type": "custom",
                    "tokenizer": "standard",
                    "filter": [
                        "trim",
                        "lowercase",
                        "my_stemmer"
                    ]
                }
            }
        }
    },
    "mappings": {
        "post": {
            "properties": {
                "title": {
                    "type": "text",
                    "analyzer": "my_custom_analyzer"
                },
                "content": {
                    "type": "text",
                    "analyzer": "my_custom_analyzer"
                },
                "tags": {
                    "type": "keyword"
                },
                "likes": {
                    "type": "long"
                }
            }
        }
    }
}
EOF

Check the index and its settings

More on indices API: https://www.elastic.co/guide/en/elasticsearch/reference/current/indices.html

curl -X GET localhost:9201/blog?pretty
curl -X GET localhost:9201/blog/_settings?pretty

(and stats)

curl -X GET localhost:9201/blog/_stats?pretty

Add some data with the bulk upload operation

Let's add some data to continue with the search and aggregations functionality. The data is placed in the test_data file in this repository.

curl -s -H "Content-Type: application/x-ndjson" -X POST localhost:9201/_bulk --data-binary "@test_data"

and check what that everything is fine

curl localhost:9201/blog/post/1?pretty

Check how our analyzer works

Read more on _analyze API: https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-analyze.html

curl -X GET -H "Content-Type: application/json" localhost:9201/blog/_analyze?pretty -d '{ "analyzer": "my_custom_analyzer", "text": "The quick brown fox jumps over the lazy dog" }'

Searching documents

More on search:

Search by term:

curl -H "Content-Type: application/json" localhost:9201/blog/_search?pretty -d '{ "query": { "term": { "tags": "traveling" } } }'

Note: when we perform search by a term, Elasticsearch does not analyze the input. So if we specified Traveling instead of traveling, we wouldn't find anything.

Full text search:

curl -H "Content-Type: application/json" localhost:9201/blog/_search?pretty -d '{ "query": { "match": { "title": "vacation" } } }'

In this case we will still get the result as Elasticsearch analyzes the input before matching to the tokens in the inverted index.

Fuzzy queries, correcting spelling mistakes:

curl -H "Content-Type: application/json" localhost:9201/blog/_search?pretty -d '{ "query": { "fuzzy": { "content": "peeple" } } }'

Multiple conditions and filters:

curl -H "Content-Type: application/json" localhost:9201/blog/_search?pretty -d '{ "query": { "bool": { "must": [ { "match": { "content": "compilation" } }, { "match": { "content": "kitten" } } ], "filter": { "term": { "tags": "cats" } } } } }'

Aggregations

Read more about aggregations: https://www.elastic.co/guide/en/elasticsearch/reference/6.6/search-aggregations.html

The average number of likes of all blog posts:

curl -X POST -H "Content-Type: application/json" localhost:9201/blog/_search?pretty -d '{ "query": { "match_all": {} }, "size": "0", "aggs": { "average_likes": { "avg": { "field": "likes" } } } }'

Printing all tags and number of articles related to each tag using aggregation by terms:

curl -X POST -H "Content-Type: application/json" localhost:9201/blog/_search?pretty -d '{ "query": { "match_all": {} }, "size": "0", "aggs": { "articles_by_tags": { "terms": { "field": "tags" } } } }'

elasticsearch-tips's People

Contributors

savva-k avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.