day115-mall business-search service-search DSL test-query part

1. DSL review

It's been two or three months since the last time I knocked on DSL, try again this time

What is the query as follows? The query is a product whose title contains no stock from Huawei and whose brand id is 6 or 8.

Knowledge point: Combine query uses bool, match to search after word segmentation, term uses a whole keyword to search without word segmentation, filter does not calculate any score, and does not care about the sorting problem returned, so

It will be a bit more efficient than must

GET product/_search
{
  "query": {
  "bool": {
    "must": [
      {
        "match": {
          "skuTitle": "华为"
        }
      }
    ],
    "filter": [
    {
      "term": {
        "hasStock": "false"
      }
    },
    {
      "terms": {
        "brandId": [
          "6",
          "10"
        ]
      }
    }
    ]
  }
  }
}

2. The embedded attribute is a conditional query

When we add the following selected part, we find that the query result is empty, why, because the attribute is embedded

When using embedded attributes as a condition

We search like the following, the value of path is the attribute name

The good modification is as follows

GET product/_search
{
  "query": {
  "bool": {
    "must": [
      {
        "match": {
          "skuTitle": "华为"
        }
      }
    ],
    "filter": [
    {
      "term": {
        "hasStock": "false"
      }
    },
    {
      "terms": {
        "brandId": [
          "6",
          "10"
        ]
      }
    },{
        "nested": {
         "path": "attrs",
         "query": {
           "bool": {
             "must": [
               {
                 "term": {
                   "attrs.attrId": {
                     "value": "1"
                   }
                 }
               }
             ]
           }
         }
       }
    }
    ]
  }
  }
}

3. Sort

Add a sort object after the query object, specify the object name and ascending order, you can specify multiple sort objects

4. Price range

5. Pagination

Start from 0, take the first two records

6. Title highlighting

Highlight and package a part of the content, here is to make the font red

You can see that the word Huawei on the right is wrapped up

7. The final DSL statement

Basically, it corresponds to the query conditions in our previous article, so that we can add basic query conditions and basic DSL. The next section will talk about aggregation analysis, which is similar to grouping in sql.

GET product/_search
{
  "query": {
  "bool": {
    "must": [
      {
        "match": {
          "skuTitle": "华为"
        }
      }
    ],
    "filter": [
    {
      "term": {
        "hasStock": "false"
      }
    },
    {
      "terms": {
        "brandId": [
          "6",
          "10"
        ]
      }
    },{
        "nested": {
         "path": "attrs",
         "query": {
           "bool": {
             "must": [
               {
                 "term": {
                   "attrs.attrId": {
                     "value": "1"
                   }
                 }
               }
             ]
           }
         }
       }
    },{
      "range": {
        "skuPrice": {
          "gte": 0,
          "lte": 4000
        }
      }
    }
    ]
  }
  },
  "sort": [
    {
      "skuPrice": {
        "order": "desc"
      }
    }
  ],
  "from": 0,
  "size": 2,
    "highlight": {
    "fields": {"skuTitle": {}},
    "pre_tags": "<b style='color:red'>",
    "post_tags": "</b>"
  }
}

 

Guess you like

Origin blog.csdn.net/JavaCoder_juejue/article/details/115363756