es - elasticsearch mapping - field data type - 15

192 阅读1分钟

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。

问 :text有什么特点?
答 :
在这里插入图片描述
问 :text如何使用?
答 :

# text
PUT /text_test
{
  "mappings" : {
    "properties" : {
      "my_text" : {
        "type"   : "text",
        "fields" : {
          "keyword" : {"type" : "keyword"}
        }
      }
    }
  }
}

# 索引
POST /text_test/_doc
{
  "my_text" : "James Gosling"
}

# 搜索
GET /text_test/_search
{
  "query" : {
    "match" : {
      "my_text" : "James"
    }
  }
}

# 结果
{
  "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" : "text_test",
        "_type" : "_doc",
        "_id" : "zJTiOHgBHtodrUzexIJ0",
        "_score" : 0.2876821,
        "_source" : {
          "my_text" : "James Gosling"
        }
      }
    ]
  }
}

# term 搜索
GET /text_test/_search
{
  "query" : {
    "term" : {
      "my_text.keyword": {
        "value" : "James Gosling"
      }
    }
  }
}

# 结果
{
  "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" : "text_test",
        "_type" : "_doc",
        "_id" : "zJTiOHgBHtodrUzexIJ0",
        "_score" : 0.2876821,
        "_source" : {
          "my_text" : "James Gosling"
        }
      }
    ]
  }
}

# fielddata
PUT /text_test_2
{
  "mappings" : {
    "properties" : {
      "my_text" : {
        "type"      : "text",
        "fielddata" : true,
        "fielddata_frequency_filter" : {
          "min"              : 0.01,
          "max"              : 1.0,
          "min_segment_size" : 2
        }
      }
    }
  }
}

# 索引
POST /text_test_2/_doc
{
  "my_text" : "James Gosling"
}

POST /text_test_2/_doc
{
  "my_text" : "James Li"
}

POST /text_test_2/_doc
{
  "my_text" : "James"
}

# 聚合搜索
GET /text_test_2/_search?size=0
{
  "query"  : {
    "match" : {
      "my_text" : "James"
    }
  }, 
  "aggs" : {
    "my_count" : {
      "cardinality" : {
        "field" : "my_text"
      }
    }
  }
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "my_count" : {
      "value" : 3
    }
  }
}