Skip to content

Instantly share code, notes, and snippets.

@frizbee
Created September 23, 2019 06:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frizbee/49855c832f9827778e9848860eed9950 to your computer and use it in GitHub Desktop.
Save frizbee/49855c832f9827778e9848860eed9950 to your computer and use it in GitHub Desktop.
Some of the calls to elastic search

GET

Simple

curl -X GET "localhost:9200/twitter"

More complicated

curl -X GET "localhost:9200/_search" -H 'Content-Type: application/json' -d'
{
  "query" : {
    "term" : {
      "user" : "kimchy"
    }
  }
}'

curl -X GET "#{host}:9200/_search" -H 'Content-Type: application/json' -d'
{
  "from" : #{offset}, "size" : #{limit},
  "query": {
    "query_string" : {
      "fields" : ["url","content", "full_name", "name", "short_name", "message"],
      "query" : "发行者"
    }
  },
  "highlight": {
    "pre_tags" : ["<mark>"],
    "post_tags" : ["</mark>"],
    "fields": {
      "full_name": {},
      "title": {},
      "content": {}
    }
  }
}'


curl -X GET "localhost:9200/_search" -H 'Content-Type: application/json' -d'
{
  "from" : 0, "size" : 10,      
    "query": {
      "bool" : {
        "must" : {
          "bool" : {
            "should" : [{
               "query_string" : {
                 "fields" : ["url","content", "full_name", "name", "short_name", "message"],
                 "query" : "bursa"
               }
             }]
           }
         },
         "must_not": { "match": {"board": "Structured Warrants" }}
       }
     },
     "highlight": {
       "pre_tags" : ["<mark>"],
       "post_tags" : ["</mark>"],
        "fields": {
        "full_name": {},
        "title": {},
        "content": {}
       }
     }
   }'

curl -X GET "localhost:9200/companies/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "must": {
        "bool" : { 
          "should": [
            { "match": { "short_name": "cimb" }},
            { "match": { "board": "main_market" }} 
          ],
          "must": { "match": { "short_name": "cimb" }} 
        }
      },
      "must_not": { "match": {"board": "Structured Warrants" }}
    }
  }
}
'

curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "query_string": {
      "query": "\"Ekuiti menawarkan potensi\""
    }
  }
}
'

curl -X GET "localhost:9200/_search" -H 'Content-Type: application/json' -d'
{
  "indices_boost" : [
    { "announcements" : 0.5 },
    { "*" : 1.5 }
  ],
  "query": {
    "dis_max": {
      "queries": [
        { "match": { "content": "cimb" }}
      ]
    }
  }
}
'

curl -X GET "localhost:9200/_search" -H 'Content-Type: application/json' -d'
{
    "indices_boost" : [
        { "alias1" : 1.4 },
        { "index*" : 1.3 }
    ]
}
'

curl -XGET "172.16.211.82:9200/*,-companies/_search" -H 'Content-Type: application/json' -d'
{
  "from" : 0, "size" : 10,
    "query": {
      "query_string" : {
        "fields" : ["url","content", "full_name", "lang", "name", "short_name", "message", "stock_id", "stock_name"],
        "query" : "bursa english"
      }
    },
    "highlight": {
      "pre_tags" : ["<mark>"],
      "post_tags" : ["</mark>"],
      "fields": {
        "full_name": {},
        "title": {},
        "content": {}
      }
    }
  }'

GET pagination

curl -X GET "localhost:9200/*,-announcements/_search" -H 'Content-Type: application/json' -d'
{
    "from" : 0, "size" : 100,
    "query": {
        "match_all": {}
    }
}
'

curl -X GET "localhost:9200/*/_search" -H 'Content-Type: application/json' -d'
{
    "from" : 0, "size" : 100,
    "query": {
        "match_all": {}
    }
}
'

Search with quotes

curl -X GET "localhost:9200/_search" -H 'Content-Type: application/json' -d'
        {
          "indices_boost" : [
            { "announcements" : 0.5 },
            { "*" : 1.5 }
          ],
          "from" : 0, "size" : 20,
            "query": {
                "query_string" : {
                    "fields" : ["url","content", "full_name", "title", "name", "short_name", "message", "stock_id", "stock_name", "announcement_titile"],
                    "query" : ".*\\"COMPANY ANNOUNCEMENTS\\".*"
                }
            },
            "highlight": {
                "pre_tags" : ["<mark>"],
                "post_tags" : ["</mark>"],
                "fields": {
                  "title": {},
                  "content": {}
                }
            }
        }'

if params[:keyword].include?("\"")
 keyword.gsub!(/\"|.\*/,"")
 keyword.gsub!(/^|$/,"\\\"").gsub!(/^|$/, ".*")
elsif params[:keyword].include?("\'")
  keyword.gsub!(/\'|.\*/,"")
  keyword.gsub!(/^|$/,"\\\"").gsub!(/^|$/, ".*")
end

PUT

curl -XPUT 'http://localhost:9200/bursa/test/1?pretty' -H 'Content-Type: application/json' -d '
{
  "name": "bursa",
  "type":"website"
}'


Test put

curl -X PUT "172.16.211.82:9200/announcements/_doc/1" -H 'Content-Type: application/json' -d'
      {
          "announcement_id" : "7889993939939333",
          "post_date" : "2018-08-17 00:00:00 +0800",
          "title" : "title",
          "stock_code" : "92992",
          "lang" : "english",
          "company_name" : "company name",
          "url" : "https://preview.bursamalaysia.com/market_information/announcements/company_announcement/announcement_details?ann_id=33",
          "content" : "contenet"
      }'

Settings

curl -XPUT "http://localhost:9200/announcements/_settings" -H 'Content-Type: application/json' -d '{ "index" : { "max_result_window" : 100000 } }'

Settings allow to delete/edit

curl -X PUT "localhost:9200/bm_/_settings" -H 'Content-Type: application/json' -d'
{
  "index": {
    "blocks": {
      "read_only_allow_delete": "false"
    }
  }
}'

Mapping

curl -X PUT "localhost:9200/announcements" -H 'Content-Type: application/json' -d'
      {
      "settings" : {
        "index.number_of_shards":1,
        "index.number_of_replicas":1
      },
        "mappings": {
          "_doc": {
            "properties": {
              "title" : { "type" : "text" },
              "company_name" : { "type" : "text" },
              "announcement_id": { "type": "keyword" },
              "meta_title" : { "type" : "text" },
              "meta_keywords" : { "type" : "text" },
              "meta_description" : { "type" : "text" },
              "url" : { "type": "text"  },
              "content" : { "type": "text" },
              "stock_code" : { "type": "keyword" },
              "lang" : { "type": "keyword" },
              "post_date":  {
                "type":   "date",
                "format": "yyyy-MM-dd HH:mm:ss Z||yyyy-MM-dd||epoch_millis"
              }
            }
          }
        }
      }
      '

curl -X PUT "localhost:9200/users/_mappings" -H 'Content-Type: application/json' -d'
{
"settings" : {
  "index.number_of_shards":1,
  "index.number_of_replicas":0
},
  "mappings": {
    "properties": { 
      "title":    { "type": "text"  }, 
      "url":     { "type": "text"  }, 
      "content":      { "type": "text" },
      "stock_code":      { "type": "keyword" },
      "lang":      { "type": "keyword" },
      "post_date":  {
        "type":   "date", 
        "format": "yyyy/MM/dd HH:mm:ss|yyyy/MM/dd|epoch_millis"
      }
    }
  }
}
'

Delete

curl -X DELETE "localhost:9200/*,-announcements,-company_profile"

CRON

# Annuncements to ElascitSearch
0 2 * * * /bin/bash -l -c 'cd /app/bursa/bursaweb/current; RAILS_ENV=production bundle exec rake data_to_elasticsearch:index' >& /dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment