es - elasticsearch mapping - parameters - position_increment_gap

211 阅读1分钟

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

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

# position_increment_gap
PUT /position_increment_gap_test
{
  "mappings" : {
    "properties" : {
      "names" : {
        "type"   : "text",
        "fields" : {
          "gap" : {
            "type"                   : "text",
            "position_increment_gap" : 3
          },
          "gap0" : {
            "type"                   : "text",
            "position_increment_gap" : 0
          }
        }
      }
    }
  }
}


# 索引
POST /position_increment_gap_test/_doc/1
{
  "names" : ["hello good", "good me"]
}

# 搜索,默认gap为100,所以搜索不到
GET /position_increment_gap_test/_search
{
  "query" : {
    "match_phrase" : {
      "names" : {
        "query" : "good good"
      }
    }
  }
}

# 搜索
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}


# 搜索,将搜索匹配距离设置为101,可以搜索到
GET /position_increment_gap_test/_search
{
  "query" : {
    "match_phrase" : {
      "names" : {
        "query" : "good good",
        "slop"  : 101
      }
    }
  }
}

# 搜索
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.010358453,
    "hits" : [
      {
        "_index" : "position_increment_gap_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.010358453,
        "_source" : {
          "names" : [
            "hello good",
            "good me"
          ]
        }
      }
    ]
  }
}


# 搜索,对于gap为3的距离,设置为4就可搜索到
GET /position_increment_gap_test/_search
{
  "query" : {
    "match_phrase" : {
      "names.gap" : {
        "query" : "good good",
        "slop"  : 4
      }
    }
  }
}

# 搜索
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.21824157,
    "hits" : [
      {
        "_index" : "position_increment_gap_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.21824157,
        "_source" : {
          "names" : [
            "hello good",
            "good me"
          ]
        }
      }
    ]
  }
}


# 搜索,对于gap为0的距离,可以直接搜索到
GET /position_increment_gap_test/_search
{
  "query" : {
    "match_phrase" : {
      "names.gap0" : {
        "query" : "good good"
      }
    }
  }
}

# 搜索
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.5753642,
    "hits" : [
      {
        "_index" : "position_increment_gap_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.5753642,
        "_source" : {
          "names" : [
            "hello good",
            "good me"
          ]
        }
      }
    ]
  }
}