Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Elasticsearch_2

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

1. Query response

1.1 pretty

Http://50.1.1.111:9200/haoke/user/Bct_zXAB_G2CqNSl4VI9 # access with a browser

Http://50.1.1.111:9200/haoke/user/Bct_zXAB_G2CqNSl4VI9?pretty # access with a browser

# if you can see the addition, pretty will return the result in json format

1.2 specify response field

In the response data, if we do not need all the fields, we can specify some required fields to return.

GET http://50.1.1.111:9200/haoke/user/Bct_zXAB_G2CqNSl4VI9?_source=id,name

# only two fields are displayed, pay attention to the following? Number

1.3 only the original data is returned and no metadata is required

# can also filter fields without displaying metadata

1.4 if we only need to determine whether the document exists, rather than query the content of the document

HEAD http://50.1.1.111:9200/haoke/user/Bct_zXAB_G2CqNSl4VI9

# return status code 200 is present.

Starting with # 400, it does not exist.

2.1 batch query

POST http://50.1.1.111:9200/haoke/_mget

{"ids": ["A8t_zXAB_G2CqNSlAVLu", "BMt_zXAB_G2CqNSlolIQ"] # # this is the _ id} of metadata

3. Batch modification

In Elasticsearch, batch insert, modify and delete operations are supported through the api of _ bulk.

_ bulk operation

In Elasticsearch, batch insert, modify and delete operations are supported through the api of _ bulk.

The request format is as follows: (the request format is unusual)

{action: {metadata}}\ n {request body}\ n {action: {metadata}}\ n {request body}\ n {request body}\ n 3.1 insert data in bulk: {"create": {"_ index": "haoke", "_ type": "user", "_ id": 2001} {"id": 2001, "name": "name1", "age": 20, "sex": "male" {"create": {"_ index": "haoke", "_ type": "user" "_ id": 2002} {"id": 2002, "name": "name2", "age": 20, "sex": "male"} {"create": {"_ index": "haoke", "_ type": "user", "_ id": 2003}} {"id": 2003, "name": "name3", "age": 20, "sex": "male"}

3.2 batch deletion

POST http://50.1.1.111:9200/haoke/user/_bulk

{"delete": {"_ index": "haoke", "_ type": "user", "_ id": 2001}} {"delete": {"_ index": "haoke", "_ type": "user", "_ id": 2002}} {"delete": {"_ index": "haoke", "_ type": "user", "_ id": 2003}}

# the data just inserted has been deleted.

Other operations are similar.

How many requests have the highest performance?

The entire batch request needs to be loaded into the memory of the node that accepts our request, so the larger the request, the less memory is available for other requests. There is an optimal bulk request size. Beyond this size, performance will no longer improve and may degrade.

The best size, of course, is not a fixed number. It all depends on your hardware, the size and complexity of your document, and the load of indexing and search.

Fortunately, this best point (sweetspot) is easy to find: try indexing standard documents in batches, and as the size increases, when performance starts to degrade, the size of each batch is too large. The initial number can be between 1000 and 5000 documents, and if your document is very large, you can use smaller batches.

It is usually useful to focus on the physical size of your requested batch. A thousand 1kB documents is very different from a thousand 1MB documents. A good one.

It is best to keep batches between 5-15MB sizes.

4. Pagination

Just as SQL returns an one-page result using the LIMIT keyword, Elasticsearch accepts the from and size parameters:

Size: number of results. Default 10from: number of results that are skipped. Default is 0.

POST http://50.1.1.111:9200/haoke/user/_search?size=1&from=2

# means to skip two and show one, which means that only the third one is displayed

5. Mapping

The index we created earlier and the inserted data are all automatically determined by Elasticsearch. Sometimes we need to specify the field type.

Otherwise, the type of automatic judgment does not match the actual requirements.

The rules for automatic judgment are as follows:

The types supported in Elasticsearch are as follows:

The string type is widely used in older versions of ElasticSearch, and string is no longer supported from ElasticSearch 5.x, and is replaced by text and keyword types. Text type, when a field is to be searched full-text, such as Email content, product description, should be text type. When the text type is set, the field contents are parsed, and the string is divided into a term by the parser before the inverted index is generated. Fields of type text are not used for sorting and are rarely used for aggregation. The keyword type is suitable for indexed structured fields, such as email addresses, hostnames, status codes, and labels. If the fields need to be filtered (such as finding articles in published blogs whose status attribute is published), sorting, aggregating. Fields of type keyword can only be searched by exact values.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report