(14) ElasticSearch basic query (Chinese inquiries)

1, prepare data

PUT /lib4
{
    "settings":{
        "number_of_shards":3,
        "number_of_replicas":0
    },
    "mappings":{
        "user":{
            "properties":{
                "name":{
                    "type":"text",
                    "analyzer":"ik_max_word"
                },
                "address":{
                    "type":"text",
                    "analyzer":"ik_max_word"
                },
                "age":{
                    "type":"integer"
                },
                "interests":{
                    "type":"text",
                    "analyzer":"ik_max_word"
                },
                "birthday":{
                    "type":"date"
                }
            }
        }
    }
}
PUT / Iib4 / the User / 1 
{ 
    " name " : " Zhao six " ,
     " address " : " Heilongjiang Province, Tieling " ,
     " Age " : 50 ,
     " Birthday " : " 1970-12-12 " ,
     " Interests " : " I like to drink, exercise, Laughed " 
} 

PUT / Iib4 / the User / 2 
{ 
    " name " : "Zhao Ming " ,
    " Address " : " Beijing Haidian District Qinghe " ,
     " Age " : 20 ,
     " Birthday " : " 1998-10-12 " ,
     " Interests " : " I like to drink, exercise, singing " 
} 

PUT / Iib4 / the User / 3 
{ 
    " name " : " lisi " ,
     " address " : " Beijing Haidian District Qinghe " ,
    "age": 23 ,
     " Birthday " : " 1998-10-12 " ,
     " Interests " : " I like to drink, exercise, singing " 
} 

PUT / Iib4 / the User / 4 
{ 
    " name " : " Wang Wu " ,
     " address " : " Haidian District, Beijing Qinghe " ,
     " Age " : 26 ,
     " Birthday " : "1995-10-12",
    " Interests " : " like programming, music, travel " 
} 

PUT / Iib4 / the User / 5 
{ 
    " name " : " Joe Smith " ,
     " address " : " Beijing Haidian District Qinghe " ,
     " Age " : 29 ,
     " Birthday " : " 1988-10-12 " ,
     " Interests " : " like photography, music, dancing " 
}

2, check out, Zhao Ming and Zhao six documents with the name of Zhao word

GET /lib4/user/_search/
{
    "query":{
        "term":{"name":""}
    }
}
GET /lib4/user/_search/
{
    "query":{
        "match":{"name":""}
    }
}

3, check out the documentation interests contain alcohol or singing

GET /lib4/user/_search/
{
    "query":{
        "terms":{
            "interests":["喝酒","唱歌"]
        }
    }
}

4, check out a document containing alcohol or interests in singing, from the start at index 0, just check out two

GET /lib4/user/_search/
{
    "from":0,
    "size":2,
    "query":{
        "terms":{
            "interests":["喝酒","唱歌"]
        }
    }
}

5, check out a document containing alcohol or interests in singing and displays the version number

GET /lib4/user/_search/
{
    "version":true,
    "query":{
        "terms":{
            "interests":["喝酒","唱歌"]
        }
    }
}

6, check out the name contains documents that contain six or Zhao, Zhao Ming and Zhao will check out Six, because word will match.

GET /lib4/user/_search/
{
    "query":{
        "match":{
            "name":"赵六"
        }
    }
}

7, check out the age of 20 is the document showed that Zhao Ming, digital type, regardless of the word.

GET /lib4/user/_search/
{
    "query":{
        "match":{
            "age":20
        }
    }
}

8, check out all the documents

GET /lib4/user/_search/
{
    "query":{
        "match_all":{}
    }
}

9, multi-field queries multi_match, field name or interests contained singing will check out

GET /lib4/user/_search/
{
    "query":{
        "multi_match":{
            "query":"唱歌",
            "fields":["interests","name"]
        }
    }
}

10, matching phrase queries match_phrase, keyword as a phrase that must match exactly, as follows, must contain "exercise, said the comic" This phrase can be found

GET / Iib4 / the User / _search / 
{ 
    " Query " : {
         " match_phrase " : {
             " Interests " : " exercise, said the comic " 
        } 
    } 
}

11, the specified field _source returned as the result returned name and address only

GET /lib4/user/_search/
{
    "_source":["address","name"],
    "query":{
        "match":{
            "interests":"唱歌"
        }
    }
}

12, prefix matching the query match_phrase_prefix, query name, in order to "Zhao" at the beginning

GET /lib4/user/_search/
{
    "query":{
        "match_phrase_prefix":{
            "name":{"query":""}
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/javasl/p/11405395.html