WANG LH , Research & Development

Elasticsearch备忘

2021.05.31 22:05

rest client

https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.5/_usage.html

https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.5/java-rest-high-supported-apis.html

es document

https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html

创建新索引的技巧

重建索引

和创建普通索引一样创建新索引。这里值得一提的时,当数据量很大的时候,需要设置刷新时间间隔,在此期间写入的数据不能搜到,从而提高重建速度:refresh_intervals = -1, number_of_replicas = 0。实践告诉我,大概会提高100% ~ 400%的提升。

取消任务
PUT _tasks/{taskID}/cancel

脚本排序

  private void buildSalesSort(String salesSortField, SortOrder sortOrder, SearchSourceBuilder searchSourceBuilder) {
    Map<String, Object> params = Maps.newHashMap();
    Date now = DateTime.now().toDate();
    String dt = TimeUtil.formatYYYMMDD(now);
    params.put("dt", dt);
    String sortScript = String.format("doc['sales.dt'].value != params.dt ? 0:doc['%s'].value", salesSortField);
    Script script = new Script(ScriptType.INLINE, "painless", sortScript, params);

    ScriptSortBuilder order = SortBuilders.scriptSort(script, ScriptSortBuilder.ScriptSortType.NUMBER).order(sortOrder);
    searchSourceBuilder.sort(order);
  }

对象赋值

GET _tasks/JiH8qXXjR6KDqQeXanOi3w:387022337


POST dev_product/product/_update_by_query?refresh&slices=5&scroll_size=1000&wait_for_completion=false
{
  "script": {
    "inline": "ctx._source.sales=params.sales",
    "params": {
      "sales": {
        "d1": {
          "num": 0,
          "dt": 20190521
        },
        "d7": {
          "num": 0,
          "dt": 20190521
        },
        "d30": {
          "num": 0,
          "dt": 20190521
        }
      }
    }
  }
}

重建索引过程

POST _reindex?wait_for_completion=false
{
  "source": {
    "index": "dev_product"
  },
  "dest": {
    "index": "dev_product_v0"
  }
}

索引重命名,会将全部数据copy到新的索引

POST _reindex?wait_for_completion=false
{
  "source": {
    "index": "dev_product"
  },
  "dest": {
    "index": "dev_product_v0"
  }
}

指定wait_for_completion=false,返回task id,可以根据task id获取task进度状态

get _tasks/JiH8qXXjR6KDqQeXanOi3w:223102303

修改别名:

POST /_aliases
{
    "actions": [
        { "add":    { "index": "dev_product_v1", "alias": "dev_product" }}
    ]
}

添加索引字段

PUT dev_product/_mapping/product
{
  "properties": {
    "store_kind":{
      "type": "byte"
    }
  }
}

es通过script添加数组元素

// 添加
{"script":{"inline":"ctx._source.tags.add(params.tag)","params":{"tag":"3"}}}'
// 删除
{"script":{"inline":"ctx._source.tags.remove(ctx._source.tags.indexOf(params.tag))","params":{"tag":"3"}}}
// 通过查询更新数组
{"query":{"term":{"tags":"1"}},"script":{"inline":"ctx._source.tags.remove(ctx._source.tags.indexOf(params.tag))","params":{"tag":"1"}}}'

空数组判断添加元素

"source":"if(ctx._source.tag == null) {ctx._source.tag=new ArrayList();ctx._source.tag.add('n');} ctx._source.tag.add(\"look\");",

if (ctx._source.businesses_reviewed == null) {
    ctx._source.businesses_reviewed = new ArrayList();
} 
ctx._source.businesses_reviewed.add(params.businessid_name);

查询id带有斜杠类似/sku/yoox.cn/35292801MQ,需要转义

GET dev_product/product/%2Fsku%2Fyoox.cn%2F35292801MQ

es一个字段可以用来全文索引,也可以用来过滤聚合。
es mapping

PUT my_index
{
  "mappings": {
    "_doc": {
      "properties": {
        "city": {
          "type": "text",
          "fields": {
            "raw": { 
              "type":  "keyword"
            }
          }
        }
      }
    }
  }
}

// 搜索过程如下
GET my_index/_search
{
  "query": {
    "match": {
      "city": "york" 
    }
  },
  "sort": {
    "city.raw": "asc" 
  },
  "aggs": {
    "Cities": {
      "terms": {
        "field": "city.raw" 
      }
    }
  }
}

Rescore排序

GET /online_product/product/_search
{
  "query": {
    "multi_match": {
      "query": "酒面膜",
      "fields": [
        "sku_id",
        "brand_name_en^1.0",
        "brand_name_zh^1.0",
        "brand_search_words^1.0",
        "categories^1.0",
        "colors^1.0",
        "ids^1.0",
        "name^1.0",
        "store_name^1.0"
      ]
    }
  },
  "rescore": [
    {
      "window_size": 1000,
      "query": {
        "score_mode": "total",
        "rescore_query": {
          "function_score": {
            "script_score": {
              "script": {
                "source": "Math.min(Math.log(1 +  5 * doc['product_score'].value), 7)"
              }
            }
          }
        },
        "query_weight": 1,
        "rescore_query_weight": 1
      }
    },
    {
      "window_size": 1000,
      "query": {
        "score_mode": "total",
        "rescore_query": {
          "function_score": {
            "script_score": {
              "script": {
                "source": "if (doc['store_kind'].value == 1) {return 1} else{return 0}"
              }
            }
          }
        },
        "query_weight": 1,
        "rescore_query_weight": 1
      }
    }
  ],
  "_source": {
    "includes": [
      "product_score",
      "goodslist_ref_num",
      "sku_id",
      "brand_name_en",
      "brand_name_zh",
      "brand_search_words",
      "categories",
      "colors",
      "ids",
      "name",
      "store_name"
    ]
  },
  "from": 0,
  "size": 10,
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    }
  ]
}