Elasticsearch Index (Multiple field Type field-fields can be retrieved to aggregate)

Source: Internet
Author: User
Tags curl

That's the thing.

Before doing a mongodb search, do a good job to see the elder brother, said, "Yes, you can." Then replace him with a elasticsearch search.

There was a breakdown in my heart. It is MongoDB is used to practicing AH. ES is what ghost.

In the process of learning, when the teacher asked brother, brother always said, this I have not learned ah, you have to learn. So I can only own reader Web documents, it took me a lot of time.

Here is a summary of what I have learned.


First, ES Index and mapping

Before the installation I did not do, the specific deployment can look at the official website (https://www.elastic.co/guide/en/elasticsearch/reference/current/_installation.html).

If you want a PHP query, use the GitHub previous php-es (Https://github.com/nervetattoo/elasticsearch)

The installation commands are as follows

cd/var/www/html
curl-s http://getcomposer.org/installer | php
echo "{"  > Composer.json
echo '        "require": {'                           >> Composer.json
echo '        "Nervetattoo/elasticsearch": ">=2.0" '   >> Composer.json
echo '    } ' >> Composer.json
Echo '} ' >> Composer.json


./composer.phar Install

First of all, analogy relational database:

Relational DB-> Databases-> Tables-> Rows-> Columns elasticsearch-> Indices-> Types-&  Gt Documents-> Fields
Elasticsearch clusters can contain multiple indexes (indices) (databases), each of which can contain multiple types (types) (tables), each containing multiple documents (documents) (rows), and then each document contains multiple fields (Fields) (columns).

To create an index:

<span style= "font-size:10px;" >curl-xput ' http://localhost:9200/ht_email_index/'-d ' {
    "settings": {"
        index": {"
            number_of_shards" : 3,
            "Number_of_replicas": 1
        }
    }
' </span>
When you create an index, specify the number of slices and replicas. , the parameters are in JSON format.

Mapping:

Curl-xput ' Http://localhost:9200/ht_email_index/_mapping/Email '-d ' {
"email": {"
	properties": {
		" Attachments ': {' type ': ' String '},
		' Cc_user ': {' type ': ' String '},
		' date ': {
			' type ': ' String ',
			' index ' : "Not_analyzed"
		},
		"Dir_path": {
			"type": "String"
		},
		"From_user": {
			"type": "String",
			' Fields ': {'
				raw ': {
					' type ': ' String ',
					' index ': ' Not_analyzed '}}}
		,
		' Message ": {
			" type ":" String "
		},
		" subject ": {
			" type ":" String "
		},
		" To_user ": {
			" Type ': "string",
			"fields": {"
				raw": {"
					type": "string",
					"index": "Not_analyzed"
				}
			}
		}
	}
}
}'

Mapping is equivalent to specifying a data type for a field. As you can see from the commands, some fields are set to string, and so on.

Here is a special note: in the "From_user" field, for example, there are two types, which are called "multiple field type fields."

The effect is that some fields sometimes need to be retrieved and sometimes need to be aggregated. Retrieval, the need for the field is analyzed, that is, mapping when the default form, when the field is more than one word, you can take the words to retrieve, this time the search conditions to write the original field name, such as "field" => "From_user." In the aggregate, the field needs to be not_analyzed, at which point the field will be treated as a whole, and the statistics often need to be for the field as a whole; then retrieve (aggregate) The field name of the not_analyzed attribute to be written, such as "field" => From_user.raw ".

This is very important.

Index data:

Curl-xput ' Http://localhost:9200/_river/ht_email_index_ik/_meta '-d ' {' 
type ': ' MongoDB ', 
' MongoDB ': { 
  "DB": "Ht_email", 
  "collection": "Email"
}, 
"index": {
  "name": "Ht_email_index", 
  "type": "Email" ,
  "Indexanalyzer": "Ik",  
  "Searchanalyzer": "Ik"
}

This is the index MONGODB data.

To delete an index:

Curl-xdelete ' Http://localhost:9200/ht_email_index '
curl-xdelete ' http://localhost:9200/_river/ht_email_index_ik/'

If you want to rebuild the index, be sure to delete the original index first.

Tools

Here is a tool for Web pages to view index status--elasticsear-head

http://blog.csdn.net/laigood/article/details/8193758

This blog has a detailed introduction, there is a need to see.


Do the above preparation work can be happy to search.

(I didn't do it myself before I created the index, so I don't guarantee the operability of the method/tutorial)



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.