“This is the seventh day of my Participation in the August More Text Challenge. Check out the details:August is more challenging”

This section introduces ElasticSearch core Syntax. This section introduces ElasticSearch Core Syntax.

I. Usage scenarios of DIS_max

When searching, match as many fields as possible for the search conditions. Whichever field matches with the highest degree of relevance will be ranked in the front.

The document data is as follows:

PUT /zoe_article/_doc/1
{   
  "name": "zhangsan"."remark":"developer"
}

PUT /zoe_article/_doc/2
{   
  "name": "zhangsan"."remark":"java developer"
}

PUT /zoe_article/_doc/3
{   
  "name": "lisi"."remark":"java developer"
}

PUT /zoe_article/_doc/4
{   
  "name": "lisi"."remark":"python developer"
}
Copy the code

When you enter Java Developer Zhangsan, the search results are as follows:

GET /zoe_article/_search
{
  "query": {
    "dis_max": {
      "queries": [{"match": {
            "name": "zhangsan"}}, {"match": {
            "remark": "java developer"}}}Copy the code

2. Cross fields

The literal translation is to search through multiple fields.

Java in the search criteria must match in the name or remark field, and developer must match in the name or remark field.

GET /zoe_article/_search
{
  "query": {
    "multi_match": {
      "query": "java developer"."fields": [
        "name"."remark"]."type": "cross_fields"."operator": "and"}}}Copy the code

Use scenarios of copy_TO

Copy_to: Copies multiple fields into a single field to achieve a multi-field combination.

If you type “computer” in the search box and click search, which field is the product attribute to be matched? If _all doesn’t work and some fields don’t match, can you specify which fields to match? Copy_to does that.

If the copy_to syntax is used, you need to manually specify the mapping policy when defining the index.

Copy_to grammar:

PUT /user_address/_mapping
{
  "properties": {
    "provice": {
      "type": "text"."analyzer": "standard"."copy_to": "address"
    },
    "city": {
      "type": "text"."analyzer": "standard"."copy_to": "address"
    },
    "street": {
      "type": "text"."analyzer": "standard"."copy_to": "address"
    },
    "address": {
      "type": "text"."analyzer": "standard"}}}Copy the code

Use scenario of Match Phrase

Match Phrase search means that search conditions are not divided into words, which means that search conditions are inseparable.

GET /zoe_article/_search
{
  "query": {
    "match_phras": {
      "remark": "java developer"}}}Copy the code

Use scenarios of Prefix Search

Prefix search, usually for fields of type keyword, that is, fields that are not word segmented.

Perform a prefix search on the title field.

GET /zoe_article/_search
{
  "query": {
    "prefix": {
      "title.keyword": {
        "value": "Gold Mining community."}}}}Copy the code

Search for recommended usage scenarios

GET /zoe_article/_search
{
  "query": {
    "match_phrase_prefix": {
      "title": {
        "query": "Gold Mining community."."slop": 10."max_expansions": 10}}}}Copy the code

The max_expansions are used to specify the maximum number of terms (words) that the prefix can match, beyond which it will no longer match.

Seven,

There are a lot of syntax for ElasticSearch. There are a lot of syntax for ElasticSearch for different scenarios.

Welcome to follow the wechat official account (MarkZoe) to learn from each other and communicate with each other.