ElasticSearch7 添加逗号分词查询

我们往ES中不得已只能写入逗号分割的字符串(比如mysql同步过来的),但是我们又要通过这些字符串去筛选结果,如下自定义的分词器可以解决这个问题。

添加一个逗号分词

PUT info-flow-test4
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_anal": {
          "type": "pattern",
          "pattern": ","
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "id": {
        "type": "keyword"
      },
      "content_id": {
        "type": "long"
      },
      "content_type": {
        "type": "integer"
      },
      "tags": {
        "type": "text",
        "analyzer": "my_anal"
      }
    }
  }
}
添加一个文档
POST /info-flow-test4/_doc
{
  "tags": "1,2,33"
}

POST /info-flow-test4/_search
{
  "query": {
    "match": {
      "tags": "2"
    }
  }
}
返回结果:
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "info-flow-test4",
        "_type" : "_doc",
        "_id" : "MUFNDX8BciPKIrWg9Qc6",
        "_score" : 0.2876821,
        "_source" : {
          "tags" : "1,2,33"
        }
      }
    ]
  }
}

另一片文章是《ElasticSearch7 Pipeline实时计算》是在写入的时候将逗号分隔的字符串写入ES中变为数组,这种方案可以通过painless 脚本做出更加复杂的处理。

你可能感兴趣的:(ElasticSearch7 添加逗号分词查询)