“This is the 10th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

What is the difference between term and match?

  • match
  • matchPhrase
  • queryString
  • Term, accurately match the field that has not been segmented. The field to be segmented needs some exact match (keyword is not segmented, text is segmented)

Term and match queries are cross-matched with the keyword and text write types

A query Write type explain
term keyword Neither of them can be participles, so they have to be exactly the same to make a match

Equivalent to mysql=
term text Term will not be segmented, but text will be segmented. As long as the value of term meets part of the value stored in text, the match will be successful.hello->hello world
match keyword Match does word segmentation, but keyword does not. Only one of the match participles matches the keyword exactly. For example,hello world->hello
match text Both participles, as long as one of the participles matches each other, for examplehello world->hello kangkang

What’s the difference between Match and matchPhrase?

Match splits the query and returns as long as one of the terms is satisfied

MatchPhrase splits the queried words and returns the queried data only when all the split words match (and are adjacent to each other)

For example, I was looking up the word “mine,” and you got me a bunch of “mine,” which doesn’t work. It must be matched with “I”, “of” can also be matched, and the two participles are adjacent.

The difference between term and terms

The same query condition allows multiple values to be matched, that is, relationships that result in

For example, I like both red and blue

Es implementation call

{
    "query": {"term": {"name":"coderymy"}}} {"query": {"terms": {"color": ["red"."blue"]
        }
    }
}

select * from person where color in ("red"."blue")
Copy the code
QueryBuilders.boolQuery().must(QueryBuilders.termsQuery("coupon.coupon_type".new int[] {1.9}))
Copy the code

Nested query

Usage scenario: The required query condition is in a list object in index, or not in the current index object but in the first level. In short, the index cannot directly obtain query conditions for processing

Keyword: nested

Official Case (adaptation) :

GET /my_index/blogpost/_search
{
  "query": {
    "bool": {
      "must": [{"match": {
            "title": "eggs"}}, {"nested": {
            "path": "coupon"."query": {
              "bool": {
                "must": [{"term": {
                      "coupon.couponId": 123}}, {"term": {
                      "coupon.couponType": 2}}]}}}}}}}Copy the code
BoolQueryBuilder builder=QueryBuilders.boolQuery();
builder.must(QueryBuilders.boolQuery().must(QueryBuilders.termQuery("coupon.coupon_type".2)));
builder.must(QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("coupon.coupon_id".123)));
builder.must(QueryBuilders.nestedQuery("coupon",builder,ScoreMode.Total));
Copy the code

Analysis:

1. In the query, nested and match(term) are at the same level, and their parameters include Path and QueryBuilder 2. The QueryBUilder is a normal builder, that is, the JSON information taken out is a complete ES query condition 3.Copy the code

Multi-condition query

Key word: bool

{
    "query": {"bool": {"must": [{"term": {"age":"22"
                }
            },{
                "term": {"sex":"girl"}}]}}}Copy the code