es - elasticsearch mapping - parameters - fields

86 阅读1分钟

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

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

# fields

PUT /fields_param_test
{
  "mappings" : {
    "properties" : {
      "name" : {
        "type" : "text",
        "fields" :  {
          "keyword" : {
            "type" : "keyword"
          },
          "analyze" : {
            "type"     : "text",
            "analyzer" : "english"
          }
        }
      }
    }
  }
}

POST /fields_param_test/_doc/1
{
  "name" : "this is a fox"
}

POST /fields_param_test/_doc/2
{
  "name" : "there are many foxes"
}

POST /fields_param_test/_doc/3
{
  "name" : "hello foxes"
}

POST /fields_param_test/_doc/4
{
  "name" : "my fox"
}


# 搜索 会将搜索结果评分合并
GET /fields_param_test/_search
{
  "query" : {
    "multi_match": {
      "query" : "where are foxes",
      "fields": ["name", "name.analyze"],
      "type"  : "most_fields"
    }
  }
}

# 结果
{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 1.7690088,
    "hits" : [
      {
        "_index" : "fields_param_test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.7690088,
        "_source" : {
          "name" : "there are many foxes"
        }
      },
      {
        "_index" : "fields_param_test",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.90213454,
        "_source" : {
          "name" : "hello foxes"
        }
      },
      {
        "_index" : "fields_param_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.12776,
        "_source" : {
          "name" : "this is a fox"
        }
      },
      {
        "_index" : "fields_param_test",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 0.099543065,
        "_source" : {
          "name" : "my fox"
        }
      }
    ]
  }
}

# 搜索 keyword
GET /fields_param_test/_search
{
  "query" : {
    "term" : {
      "name.keyword" : {
        "value" : "my fox"
      }
    }
  }
}

# 结果
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.2039728,
    "hits" : [
      {
        "_index" : "fields_param_test",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 1.2039728,
        "_source" : {
          "name" : "my fox"
        }
      }
    ]
  }
}