Data preparation

Single field Conditional Query (TERM)

// Query by name

1.SQL Example SELECT * from user where name="wxx"

2.Es query GET index3/_search {"query": {
    "term": {
      "name": {
        "value": "wxx"}}}}Copy the code

Single-field Conditional Query (match)

// Query by name

1.SQL Example SELECT * from user where address="Fuzhou"

2.Es query GET index3/_search {"query": {
    "match": {
      "address": {
        "value": "Fuzhou"}}}}Copy the code

Note: The difference between term and match is that one does not support word segmentation, one supports word segmentation, and the other does not support word segmentation. Match matches as long as it can match word segmentation (for example, Fuzhou, Jiangxi, will be divided into words of Fuzhou, Jiangxi).

Single-field condition Batch query

// Query by name

1.SQL example SELECT *from user where name in ("wxx"."zhangxianbo")2. Es query GET index3/_search{
  "query": {
    "terms": {
      "name": [
        "zhangxianbo"."wxx"]}}}Copy the code

Single field condition range query

// Query by name

1.SQL Example SELECT * from user where age between10 and 26

2.Es query GET index3/_search {"query": {
    "range": {
      "age": {
        "gte": 10."lte": 26}}}}Copy the code

Multi-field union query

// Query by name and age or

1.SQL Example SELECT * from user where name="wxx" or age =26

2.Es query GET index3/_search {"query": {
    "bool": {
      "should": [{"term": {
            "name": {
              "value": "wxx"}}}, {"term": {
            "age": {
              "value": "26"}}}]}}Copy the code

Multi-field intersection query

// Query by name and age

1.SQL Example SELECT * from user where name="liujing" and age =18

2.Es query GET index3/_search {"query": {
    "bool": {
      "must": [{"term": {
            "name": {
              "value": "liujing"}}}, {"term": {
            "age": {
              "value": "18"}}}]}}Copy the code

Complex query with multi-field union

// Meet a certain gender, or meet a certain name and age

1.SQL example SELECT *from user where (sex=wman) or (name=wangjian and age=26)2. Es query GET index3/_search{
  "query": {
    "bool": {
      "should": [{"term": {
            "sex": {
              "value": "wman"}}}, {"bool": {
            "must": [{"term": {
                  "name": {
                    "value": "wangjian"}}}, {"term": {
                  "age": {
                    "value": "26"}}}]}}}Copy the code

Complex query with multi-field intersection

// Meet a gender, and meet a name or age

1.SQL example SELECT *from user where (sex=man) and (name=wangjian or name=zhangxianbo or age=18)2. Es query GET index3/_search{
  "query": {
    "bool": {
      "must": [{"term": {
            "sex": {
              "value": "man"}}}, {"bool": {
            "should": [{"term": {
                  "name": {
                    "value": "wangjian"}}}, {"term": {
                  "name": {
                    "value": "zhangxianbo"}}}, {"term": {
                  "age": {
                    "value": "18"}}}]}}}Copy the code

Query precision control

// Query user likes fruit, match at least three
POST index3/_search
{
  "query": {
    "match": {
      "like_fruits": {
        "query": "apple banana pear cherry"
        , "minimum_should_match": 3  // Percentage available}}}}// Use should must,must_not
POST index3/_search
{
  "query": {
    "bool": {
      "should": [{"match": {
            "like_fruits": "apple"}}, {"match": {
            "like_fruits": "banana"}}, {"match": {
            "like_fruits": "pear"}}, {"match": {
            "like_fruits": "cherry"}}]."minimum_should_match": 3}}}// Use Boost to score participating indicators, the higher the weight, the higher the total score
POST index3/_search
{
  "query": {
    "bool": {
      "should": [{"match": {
            "like_fruits": {
              "query": "apple"}}}, {"match": {
            "like_fruits": {
              "query": "banana"
              , "boost": 5}}}, {"match": {
            "like_fruits": {
              "query": "pear"}}}, {"match": {
            "like_fruits": {
              "query": "cherry"}}}],"minimum_should_match": 3}}} ** Note: Obviously, match accuracy must only consider the segmentation case, so use match, as a search engine, this is unique to es **Copy the code

conclusion

1. Single-field query is simple, batch query is equivalent to in, range query is equivalent to between and 2. Should,must, must_NOT can be used for or and 3 4. Bool should,must not be nested, bool should,must 5. Precise search control is unique to ES and is also a search engine feature