hariprasadraja
5/22/2019 - 5:52 AM

[Query DSL] Elastic Search Query DSL #elasticsearch

[Query DSL] Elastic Search Query DSL #elasticsearch

Find documents that matches a 'FIELD' with an 'VALUE' (match query) **@6.2

GET /_search
{
    "query": {
        "match" : {
            "FEILD" : {
              
            "query": "VALUE",
            
            "operator" : "", // (default: ) (and,or)
          
            // set an analyzer to perform analysis
            "analyzer":"", // (default: search analyzer)
          
            // if true, ignores exceptions caused by data_type mismatches
            "lenient":"", // (default: false)
          
            // allows fuzzy matching based on the type of feild
            "fuzziness":"",  // (default: AUTO:3,6) (0,1,2,AUTO:[low],[high])
            "prefix_length":"",     // prefix lenght for fuzziness
            "max_expansions":"",    // max expansion for fuzziness
          
            // if no document matches, returns all document
            "zero_terms_query":"", // (default: none) (none,all)
          
            "cutoff_frequency":
            }
        }
    }
}

(or)

GET /_search
{
    "query": {
        "match" : {
            "message" : "this is a test"
        }
    }
}

Query by matching documents and Filter it's Results @v6.2

NOTE: Use query clauses in query context for conditions which should affect the score of matching documents (i.e. how well does the document match), and use all other query clauses in filter context.

GET /_search
{
  //  Query Context (“How well does this document match this query clause?”)
  "query": { 
    "bool": { 
      "must": [
        
        // matches the document having title 'Search' and content 'Elasticsearch'
        { "match": { "title":   "Search"        }},  
        { "match": { "content": "Elasticsearch" }}  
      ],
      
      // Filter Context (“Does this document match this query clause?”)
      "filter": [ 
        
        // Filter out the result which has status 'published' and of range greater than or equal to 
        // "2015-01-01"
        { "term":  { "status": "published" }}, 
        { "range": { "publish_date": { "gte": "2015-01-01" }}} 
      ]
    }
  }
}

mathces and returns all the documents in an 'index'
@6.2 @7.1

GET [index]/_search
{
    "query": { // use any one of the match_all
      
        // matches all documents (gives _score as 1.0)
        "match_all": {}
      
       // matches no document
       "match_none":{} 
  
       // imporve _score with 'boost' paramerter
       "match_all": { "boost" : 1.2 }
    }
}

finds the documents that exactly matches the term in order

For a document to be considered a match for the phrase “quick brown fox”, the following must be true:

quick, brown, and fox must all appear in the field. The position of brown must be 1 greater than the position of quick. The position of fox must be 2 greater than the position of quick. If any of these conditions is not met, the document is not considered a match.

GET /_search
{
    "query": {
        "match_phrase" : {
            "message" : {
                "query" : "quick brown fox",
            
             // set an analyzer to perform analysis
            "analyzer":"", // (default: search analyzer)
            }
        }
    }
}

(OR)

GET /_search
{
    "query": {
        "match_phrase" : {
            "message" : "this is a test"
        }
    }
}

finds documents by matching perfix term

It can be used for real time search filtering

It accepts the same parameters as the phrase type. In addition, it also accepts a max_expansions parameter (default 50) that can control to how many suffixes the last term will be expanded. It is highly recommended to set it to an acceptable value to control the execution time of the query.

GET /_search
{
    "query": {
        "match_phrase_prefix" : {
            "message" : "quick brown f"
           
            // contolls how much suffixes the last term need to expand
            "max_expansion":50
        }
    }
}