Introduction to the core concept of elasticSearch: simple use of search

Simple use of search

In the previous chapter, we introduced the basic use of the document. If you are interested, you can refer to it
Introduction to the core concept of elasticSearch (III): addition, deletion, modification and query of documents
In this chapter, we will make a simple use of search.

1, Let's delete all the indexes in the above content first

  • View all indexes
curl -X GET "http://172.25.45.150:9200/_cat/indices?v"
  • Delete one by one
curl -X DELETE "http://172.25.45.150:9200/nba"

2, Create a new index and specify mapping

curl -X PUT "http://172.25.45.150:9200/nba" -H 'Content-Type:application/json' -d '
	{
		"mappings":{
            "properties":{
                "name":{
                    "type":"text"
                },
                "team_name":{
                    "type":"text"
                },
                "position":{
                    "type":"keyword"
                },
                "play_year":{
                    "type":"keyword"
                },
                "jerse_no":{
                    "type":"keyword"
                }
            }
		}
	}
'

3, New document (data)

  • doc1
curl -X PUT "http://172.25.45.150:9200/nba/_doc/1" -H 'Content-Type:application/json' -d '
{
    "name":"Harden",
    "team_name":"rocket",
    "position":"point guard",
    "play_year":"10",
    "jerse_no":"13"
}
'
  • doc2
curl -X PUT "http://172.25.45.150:9200/nba/_doc/2" -H 'Content-Type:application/json' -d '
{
    "name":"Curry",
    "team_name":"warrior",
    "position":"Point guard",
    "play_year":"10",
    "jerse_no":"30"
}
'
  • doc3
curl -X PUT "http://172.25.45.150:9200/nba/_doc/2" -H 'Content-Type:application/json' -d '
{
    "name":"James",
    "team_name":"Lakers",
    "position":"Small forward",
    "play_year":"15",
    "jerse_no":"23"
}
'

4, Inquiry

  • term query and full text query

    • Term query: term query will not analyze the query conditions. Only when the term matches the query string exactly can it match the search.
    • Full text query: ElasticSearch engine will give priority to analyzing the query string and split it into multiple word segmentation. As long as the analyzed field contains any or all of the entries, it will match the query conditions and return the document; If any participle is not included, it means that no document matches the query criteria
  • Single term query

    • request
    curl -X POST "http://172.25.45.150:9200/nba/_search" -H 'Content-Type:application/json' -d '
    	{
    		"query":{
    			"term":{
    				"jerse_no":"23"
    			}
    		}
    	}
    '
    
  • Multiple term queries

    • request

      curl -X POST "http://172.25.45.150:9200/nba/_search" -H 'Content-Type:application/json' -d '
      	{
      		"query":{
      			"terms":{
      				"jerse_no":[
                          "23","13"
                      ]
      			}
      		}
      	}
      '
      
    • response

{
"took": 1, / / time consumed
"timed_out": false, / / whether to timeout
"_shards": {/ / fragment information
"total": 1, / / here is a single machine, here is 1
"Successful": 1, / / successful
"skipped": 0,
"failed": 0
},
"Hits": {/ / hits in search results
"total": {
"value": 2, / / total number found
"relation": "eq" //=
},
"max_score": 1.0, / / maximum score. It is used for sorting. The following data are arranged in reverse order
"hits": [
{
"_index": "nba",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"name": "harden",
"team_name": "rocket",
"position": "point guard",
"play_year": "10",
"jerse_no": "13"
}
},
{
"_index": "nba",
"_type": "_doc",
"_id": "3",
"_score": 1.0,
"_source": {
"name": "James",
"team_name": "Lakers",
"position": "small forward",
"play_year": "15",
"jerse_no": "23"
}
}
]
}
}



- full text(match)Full text query

- match_all query

> match_all All data will be queried

```bash
curl -X POST "http://172.25.45.150:9200/nba/_search" -H 'Content-Type:application/json' -d '
{
    "query":{
        "match_all":{}
    },
    "from":0,
    "size":100
}
'
  • match query

    It should be noted that if you use match to query, the type type should be the full-text search type of text separable words.

    curl -X POST "http://172.25.45.150:9200/nba/_search" -H 'Content-Type:application/json' -d '
    {
        "query":{
            "match":{
               "name":"Ku Xiaoli baby" 
            }
        },
        "from":0,
        "size":100
    }
    '
    
  • multi_match multiple queries

    • Modify a piece of data first (add a new title field with id=2)

      curl -X POST "http://172.25.45.150:9200/nba/_update/2" -H 'Content-Type:application/json' -d '
      {
          {
          "doc":{
          "name":"Curry",
          "team_name":"warrior",
          "position":"Point guard",
          "play_year":"10",
          "jerse_no":"30",
          "title":"the best shooter"
      	}
      }
      '
      
    • Using multi_match multi field query

      curl -X POST "http://172.25.45.150:9200/nba/_search" -H 'Content-Type:application/json' -d '
      {
          "query":{
              "multi_match":{
                  "query":"shooter",
                  "fields":["title","name"]
              }
          },
          "from":0,
          "size":100
      }
      '
      
    • response

      {
          "took": 2,
          "timed_out": false,
          "_shards": {
              "total": 1,
              "successful": 1,
              "skipped": 0,
              "failed": 0
          },
          "hits": {
              "total": {
                  "value": 1,
                  "relation": "eq"
              },
              "max_score": 0.2876821,
              "hits": [
                  {
                      "_index": "nba",
                      "_type": "_doc",
                      "_id": "2",
                      "_score": 0.2876821,
                      "_source": {
                          "name": "Curry",
                          "team_name": "warrior",
                          "position": "Point guard",
                          "play_year": "10",
                          "jerse_no": "30",
                          "title": "the best shooter"
                      }
                  }
              ]
          }
      }
      
  • match_phrase query

    Similar to entry query, accurate query

    curl -X POST "http://172.25.45.150:9200/nba/_search" -H 'Content-Type:application/json' -d '
    {
        "query":{
            "match_phrase":{
                "position":"point guard"
            }
        },
        "from":0,
        "size":100
    }
    '
    
  • match_phrase _prefix

    Prefix query is similar to like query in mysql

    curl -X POST "http://172.25.45.150:9200/nba/_search" -H 'Content-Type:application/json' -d '
    {
        "query":{
            "match_phrase_prefix":{
                "title":"the"
            }
        }
    }
    '
    

Keywords: Java Big Data ElasticSearch search engine

Added by abitlikehomer on Sat, 19 Feb 2022 03:28:22 +0200