Skip to content

Elastic search Query

Blair Learn edited this page Nov 8, 2021 · 50 revisions

1. a-z list expand

When expanding a letter, use this query, substituting:

  • For language, either "en" (English) or "es" (Spanish).
  • For audience, either "Patient" or "HealthProfessional".
  • For dictionary, either "Cancer.gov" for the Dictionary of Cancer Terms or "Genetics" for the Dictionary of Genetics Terms
  • For first_letter, the letter to expand. To expand non-alphabetic first letters, specify "#".
  • For from, the zero-based offset into the list of results for the first one to return.
  • For size, the maximum number of results to return (default is 9999).
  • For _source, a list of field names to be returned.
    • NOTE: Any fields that are enumerations MUST be returned as enumerations are not NULLABLE
curl -XPOST http://SERVER_NAME/glossaryv1/_search -H 'Content-Type: application/x-ndjson'   -d '{
    "query": {
      "bool": {
        "must": 
        [
        {"term": { "language": "en"}},
        {"term": { "audience": "Patient"}},
        {"term": { "dictionary": "Cancer.gov"}},
        {"term": { "first_letter": "m"}}
        ]
    }}
,"sort": ["term_name"]
, "from": 0
, "size": 9999
, "_source": ["term_id", "language", "dictionary", "audience", "term_name", "definition", "pretty_url_name", "first_letter", "pronunciation"]
}'

2. auto suggest

Begin with

Use this query for autosuggest "Begins with", substituting

  • For language, either "en" (English) or "es" (Spanish).
  • For audience, either "Patient" or "HealthProfessional".
  • For dictionary, either "Cancer.gov" for the Dictionary of Cancer Terms or "Genetics" for the Dictionary of Genetics Terms
  • For term_name, the text to be used for the suggestion.
  • For from, the zero-based offset into the list of results for the first one to return.
  • For size, the maximum number of results to return (default is 9999).
  • For _source, a list of field names to be returned. (This will probably be the list shown.)
curl -XPOST http://SERVER_NAME/glossaryv1/_search -H 'Content-Type: application/x-ndjson'   -d '{
  "query": {
    "bool" : {
      "must" : 
        [{"term" : { "language" : "es" }},
        {"term": { "audience": "Patient"}},
        {"term": { "dictionary": "Cancer.gov"}},
        {"prefix" : {"term_name" : "cutáneo"}}
      ]
    }
  }
,"sort": ["term_name"]
, "_source": ["term_id", "term_name"]
, "from": 0
, "size": 10
}'

Contain

Use this query for autosuggest "Contains", substituting

  • For language, either "en" (English) or "es" (Spanish).
  • For audience, either "Patient" or "HealthProfessional".
  • For dictionary, either "Cancer.gov" for the Dictionary of Cancer Terms or "Genetics" for the Dictionary of Genetics Terms
  • For term_name, the text to be used for the suggestion.
  • For from, the zero-based offset into the list of results for the first one to return.
  • For size, the maximum number of results to return (default is 9999).
  • For _source, a list of field names to be returned. (This will probably be the list shown.)
curl -XPOST http://SERVER_NAME/glossaryv1/_search -H 'Content-Type: application/x-ndjson'   -d '{
  "query": {
    "bool" : {
      "must" : 
        [{"term" : { "language" : "es" }},
        {"term" : { "dictionary" : "Cancer.gov" }},
        {"term": { "audience": "Patient"}},
        {"match_phrase":{"term_name._autocomplete":"cutáneo"}}
        ],
         "must_not" :  {"prefix" : {"term_name" : "cutáneo"}}
      }
    }
,"sort": ["term_name"]
, "_source": ["term_id", "term_name"]
, "from": 0
, "size": 10
}'

3. Search

Begin with

Use this query for search "Begins with", substituting

  • For language, either "en" (English) or "es" (Spanish).
  • For audience, either "Patient" or "HealthProfessional".
  • For dictionary, either "Cancer.gov" for the Dictionary of Cancer Terms or "Genetics" for the Dictionary of Genetics Terms
  • For term_name, the text to be searched for.
  • For from, the zero-based offset into the list of results for the first one to return.
  • For size, the maximum number of results to return (default is 9999).
  • For _source, a list of field names to be returned.
curl -XPOST http://SERVER_NAME/glossaryv1/_search -H 'Content-Type: application/x-ndjson'   -d '{
  "query": {
    "bool" : {
      "must" : 
        [{"term" : { "language" : "es" }},
        {"term" : { "dictionary" : "Cancer.gov" }},
        {"term": { "audience": "Patient"}},
        {"prefix" : {"term_name" : "cutáneo"}}
       ]
    }
  }
,"sort": ["term_name"]
, "_source": ["term_id", "language", "dictionary", "audience", "term_name", "definition", "pretty_url_name", "first_letter", "pronunciation"]
, "from": 40
, "size": 20
}'

Contain

Use this query for search "Contains", substituting

  • For language, either "en" (English) or "es" (Spanish).
  • For audience, either "Patient" or "HealthProfessional".
  • For dictionary, either "Cancer.gov" for the Dictionary of Cancer Terms or "Genetics" for the Dictionary of Genetics Terms
  • For term_name, the text to be searched for.
  • For from, the zero-based offset into the list of results for the first one to return.
  • For size, the maximum number of results to return (default is 9999).
  • For _source, a list of field names to be returned.
curl -XPOST http://SERVER_NAME/glossaryv1/_search -H 'Content-Type: application/x-ndjson'   -d '{
  "query": {
    "bool" : {
      "must" : 
        [{"term" : { "language" : "es" }},
        {"term" : { "dictionary" : "Cancer.gov" }},
        {"term": { "audience": "Patient"}},
        {"match":{"term_name._contain":"cutáneo"}}
        ]
    }
  }
,"sort": ["term_name"]
, "_source": ["term_id", "language", "dictionary", "audience", "term_name", "definition", "pretty_url_name", "first_letter", "pronunciation"]
, "from": 40
, "size": 20
}'

4. Get (specific match)

Get a specific term by concatenating the values for

  1. {term_id), the specific term ID to retrieve.
  2. {dictionary}, the name of the desired dictionary.
  3. {language}, the two digit language code.
  4. {audience}, either "Patient" or "healthprofessional".

The values are separated by underscores (_) and all characters are lowercase. Get http request

curl /glossaryv1/_doc/43966_cancer.gov_en_patient

5. Get with fall back

Use this query to get all definitions for a term regardless of target audience and dictionary, substituting

  • For language, either "en" (English) or "es" (Spanish).
  • For term_id, the specific term ID to retrieve.
  • For from, the zero-based offset into the list of results for the first one to return.
  • For size, the maximum number of results to return (default is 9999).
curl -XPOST http://SERVER_NAME/glossaryv1/_search -H 'Content-Type: application/x-ndjson'   -d '{
    "query": {
      "bool": {
        "must": 
        [
         {"term": { "language": "en"}},
         {"term": { "term_id": "44238"}}
        ]
    }}
, "from": 40
, "size": 20
}'

6. getAll

curl -XPOST http://SERVER_NAME/glossaryv1/_search -H 'Content-Type: application/x-ndjson'   -d '{
  "query": {
    "bool" : {
      "must" : 
        [{"term" : { "language" : "en" }},
        {"term" : { "dictionary" : "Cancer.gov" }},
        {"term": { "audience": "Patient"}}
        ]
    }
  }
,"sort": ["term_name"]
, "_source": ["term_name", "definition", "pretty_url_name", "related_resources", "media", "pronunciation"]
, "size": 9999
}'

7. Get Count

Gets the total number of glossary terms matching the criteria, substituting

  1. {dictionary}, the name of the desired dictionary.
  2. {language}, the two digit language code.
  3. {audience}, either "Patient" or "healthprofessional".

This uses _count API of ES instead of _search API

curl -XPOST http://SERVER_NAME/glossaryv1/_count -H 'Content-Type: application/x-ndjson'   -d '{
  "query": {
    "bool" : {
      "must" : 
        [{"term" : { "language" : "es" }},
        {"term" : { "dictionary" : "Cancer.gov" }},
        {"term": { "audience": "Patient"}}
        ]
    }
  }
}'

8 Get by pretty_url_name

curl -XPOST http://SERVER_NAME/glossaryv1/_search -H 'Content-Type: application/x-ndjson'   -d '{
    "query": {
      "bool": {
        "must": 
        [
        {"term": { "language": "en"}},
        {"term": { "audience": "Patient"}},
        {"term": { "dictionary": "Cancer.gov"}},
        {"term": { "pretty_url_name": "stage-iv-lip-and-oral-cavity-cancer"}}
        ]
    }}
,"sort": ["term_name"]
, "from": 0
, "size": 9999
, "_source": ["term_id", "language", "dictionary", "audience", "term_name", "definition", "pretty_url_name", "first_letter", "pronunciation"]
}'

9 Exact match term_name

curl -XPOST http://SERVER_NAME/glossaryv1/_search -H 'Content-Type: application/x-ndjson'   -d '{

 "query": {
      "bool": {
        "must": 
        [
        {"term": { "language": "en"}},
        {"term": { "audience": "Patient"}},
        {"term": { "dictionary": "Cancer.gov"}},
        { "term": { "term_name": "mk-0646"}}
        ]
    }}

}'

Clone this wiki locally