[learn ES series] - 4.ElasticSearch basic interaction basic query and advanced query

Keywords: Programming PHP

Basic query

POST http://127.0.0.1:9200/book/_search

  • 1. Simple query
{
    "query":{
        "match_all":{}
    }
}
  • 2. Condition query
{
    "query":{
        "match":{
            "title":"Entry to mastery"
        }
    },
    "from":1,
    "size":5,
    "sort":{
        "publish_date":{
            "order":"asc"
        }
    }

}
  • 3. Aggregate query
{
    "aggs":{
        "group_by_word_count":{
            "terms":{
                "field":"word_count"
            }
        },
        "group_by_publish_date":{
            "terms":{
                "field":"publish_date"
            }
        }
    }

}
{
    "aggs":{
        "total_word_count":{
            "stats":{
                "field":"word_count"
            }
        }
    }
}
{
    "aggs":{
        "max_word_count":{
            "max":{
                "field":"word_count"
            }
        }
    }
}

Advanced query

1. Sub condition query is also called leaf condition query (specific value of specific field query)

  • 1.1Query Context
    In the process of query, in addition to judging whether the document meets the query criteria, ES will calculate a "score" to identify the matching degree, aiming to judge how well the target document matches the query criteria.
1.1.1 full text query: data of text type
  • --1. Fuzzy matching
{
    "query":{
        "match":{
            "title":"PHP From entry to mastery"
        }
    }
}

Match PHP, from entry to mastery

  • --2. Idiom matching
{
    "query":{
        "match_phrase":{
            "title":"PHP From entry to mastery"
        }
    }
}
  • --3. Fuzzy matching query of multiple fields
{
    "query":{
        "multi_match":{
            "query":"PHP",
            "fields":["title","author"]
        }
    }
}

Query title or author for PHP keywords

  • --4.1 syntax query
{
    "query":{
        "query_string":{
            "query":"(PHP AND Introduction) OR General reform"
        }
    }
}

The text field contains two keywords: PHP and getting started, or the text field contains general reform

  • --4.2 query multiple fields (specify field query)
{
    "query":{
        "query_string":{
            "query":"PHP",
            "fields":["title","author"]
        }
    }
}
  • 1.1.2 field level query: for structured data, such as numbers, dates, etc
  • --1. Specify the exact field query
{
    "query":{
        "term":{
            "author":"General reform"
        }
    }
}
  • --2. Scope query
  • --2.1 digital range
{
    "query":{
        "range":{
            "word_count":{
                "gte":"170000",
                "lte":"200000"
            }
        }
    }
}
  • --2.2 date range
{
    "query":{
        "range":{
            "publish_date":{
                "gte":"2018-01-01",
                "lte":"2019-12-30"
            }
        }
    }
}
{
    "query":{
        "range":{
            "publish_date":{
                "gte":"2019-01-01",
                "lte":"now"
            }
        }
    }
}

1.2Filter Context
In the process of query, only judge whether the document meets the conditions, only yes or no
(query determines yes or no, and also matches the score)

ES will cache Query results, so it is faster than Query

{
    "query":{
        "bool":{
            "filter":{
                "term":{
                    "author":"General reform"
                }

            }
        }
    }
}

2. Composite condition query (a certain logical combination of sub query queries)

Posted by cfemocha on Wed, 04 Dec 2019 10:57:37 -0800