<返回更多

Elasticsearch DSL语法

2022-09-21  今日头条  51编程
加入收藏
  1. 全局搜索
GET /shops_index,goods_index/_search  //多索引搜索用逗号隔开或者 /_all/_search、/_search
{
  "query": {
    "multi_match": {
      "query": "口腔",  // 如果查询字段 *口腔* 则fileds 必须["goods_title.keyword^10","goods_subtitle.keyword^7"]  ^1表示权重
      "fields": ["goods_title", "title"]
    }    
  }
  "_source": ["goods_title","title"] //查询字段
}
  1. match_all
{
    "query":{
        "match_all":{} //查询所有
    }
}
  1. 分页查询
{
  "query": {
      "match_all":{} //查询所有
  },
  "from":0,
  "size":10
}
  1. match_phrase

查询同一个字段中几个词,可以跳过其他词,slop表示可以跳过的最大词数

{
  "query": {
     "match_phrase": {
          "description": "He is",
          "slop":100
    }
  }
}
  1. term 单个条件查询

(不会分词,精确匹配)一句话解释: elasticsearch 里默认的IK分词器是会将每一个中文都进行了分词的切割,所以你直接想查一整个词,或者一整句话是无返回结果的。查询不是中文的字符串能查不出来

{
  "query": {

      "term": {
        "category_id": "10052", // 能查出数据
        //"goods_title" : "体检", 查不出来因为中文分词了,除非就一个汉字
      }

  }
}
  1. terms 多个关键字查询
"query": {
    "terms": {
        "channel_id": [102,101]
    }
}
  1. match 条件查询

会分词,全文检索

{
  "query": {
    "match": {
      "goods_title": "体检"
    }
  }
}

{
  "query": {
    "match": {
      "goods_title": {
        "query": "体检",
        "operator": "and" // or 表示只要有一个分词就可以查询出来,and 表示必须包含分出来的所有字段
      }
    }
  }
}
  1. multi_match 多个字段检索
{
  "query": {
    "multi_match": {
      "query": "口腔",
      "fields": ["goods_title","goods_subtitle"]
    }
  }
}
  1. exits 检索字段存在
{
  "query": {
    "exists": {
      "field": "goods_title"
    }
  }
}
  1. bool 检索

must :多个条件全部要满足;should:或者的意思,满足一个即可;must_not:除了满足所有条件剩下的数据

{
  "query": {
    "bool": {
      "must": [
          {
              "multi_match":{
                  "query":"口腔",
                  "fields":["goods_title","goods_subtitle"]
              }
          },
          {
              "term":{
                  "age":19              
              }          
          },
          {
              "terms": {
                "brand_name.keyword": ["白敬宇"] //类似in查询
              }
          }
      ],
      "must_not":[
          {
              "terms": {
                 "goods_id": [1,2,3]
}
  }      
      ]
    }
  }
}
  1. post_filter 对结果过滤
{
  "query": {
    "match": {
      "goods_title": "口腔"
    }
  },
  "post_filter": {
    "range": {
      "selling_price": {
        "gte": 1000,
        "lte": 1500
      }
    }
  }
}
  1. sort 排序
{
  "query": {
    "match": {
      "goods_title": "口腔"
    }
  },
  "post_filter": {
    "range": {
      "selling_price": {
        "gte": 1000,
        "lte": 2000
      }
    }
  },
  "sort": [
    {
      "selling_price": {
        "order": "asc"
      }
    }
  ], 
  "_source": ["selling_price"]
}
  1. 修改setting

注意:修改setting需要先关闭所以,然后再开启索引

$es->indices()->close(['index' => $index]);
$params_setting = [
    'index' => $index,
    'body'  => [
        'settings' => [
            'analysis' => [
                'analyzer' => [
                    'ik_pinyin_analyzer' => [
                        'type'      => 'custom',
                        'tokenizer' => 'ik_max_word',
                        'filter'    => ["my_pinyin", "word_delimiter"],
                    ]
                ],
                'filter'   => [
                    "my_pinyin" => [
                        "type"         => "pinyin",
                        "first letter" => "prefix",
                        "padding_char" => " "
                    ]
                ]
            ]
        ]
    ]
];
$es->indices()->putSettings($params_setting);
$es->indices()->open(['index' => $index]);
  1. 修改mApping
PUT fit_goods_index/_mapping
{
  "properties":{
       "search_title":{
         "type":"text"
       }
   }
}


// php代码
$params = [
    'index' => $index,
    'body'  => [
        '_source'    => [
            'enabled' => true
        ],
        'properties' => [
            'location'   => [
                'type'             => 'geo_point',
                'ignore_malformed' => true
            ],
            'completion' => [
                'type'     => 'completion',
                'analyzer' => 'ik_pinyin_analyzer',
                'fields'   => [
                    "key" => [
                        "type" => "keyword"
                    ],
                ],
                "contexts" => [
                    [
                        "type" => "category",
                        "name" => "plan_id"
                    ]
                ]
            ],
        ]
    ]
];
$es->indices()->putMapping($params);
  1. 范围搜索
GET /goods_index/_search
{
  "query":{
    "bool": 
      {
        "must":[
          {
            "query_string":{
              "query":"双重好礼",
              "fields": ["goods_title^2"]
            }
          }  
        ]
      }
  },
  "_source": ["goods_title","goods_subtitle","selling_price"],
 "post_filter": {
    "range": {
      "selling_price": {
        "gte": 0,
        "lte": 108
      }
    }
  }
}
  1. in 查询
GET /goods_index/_search
{
  "query":{
    "bool": 
      {
        "must":[
          {
            "query_string":{
              "query":"双重好礼",
              "fields": ["goods_title"]
            }
          },
          {
            "terms": {
                "specs_type": ["more"]
            }
          }
        ]
      }
  }
}

NOT IN 查询

GET /goods_index/_search
{
  "query":{
    "bool": 
      {
        "must":[
          {
            "query_string":{
              "query":"双重好礼",
              "fields": ["goods_title"]
            }
          }
        ],
        "must_not":[
            {
              "terms": 
                {
                    "specs_type": ["more"]
                }
            }
        ]
      }
  }
} 
声明:本站部分内容来自互联网,如有版权侵犯或其他问题请与我们联系,我们将立即删除或处理。
▍相关推荐
更多资讯 >>>