天天看点

es - elasticsearch mapping - parameters - 1

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

问 :es mapping的parameters都有哪些?

答 :

es - elasticsearch mapping - parameters - 1

问 :analyzer有什么特点?

答 :

es - elasticsearch mapping - parameters - 1

问 :analyzer如何使用?

答 :

# analyzer
# 自定义分析器
PUT /analyzer_test
{
  "settings" : {
    "analysis" : {
      "analyzer" : {
        "my_analyzer" : {
          "type"      : "custom",
          "tokenizer" : "standard",
          "filter"    : ["lowercase"]
        },
        "my_stop_analyzer" : {
          "type"      : "custom",
          "tokenizer" : "standard",
          "filter"    : ["lowercase", "english_stop"]
        }
      },
      "filter" : {
          "english_stop" : {
            "type"      : "stop",
            "stopwords" : "_english_"
          }
        }
     }
  },
  "mappings" : {
    "properties" : {
      "name1" : {
        "type"    : "text",
        "analyzer": "my_analyzer"
      },
      "name2" : {
        "type"            : "text",
        "analyzer"        : "my_analyzer",
        "search_analyzer" : "my_stop_analyzer"
      },
      "name3" : {
        "type"                  : "text",
        "analyzer"              : "my_analyzer",
        "search_analyzer"       : "my_stop_analyzer",
        "search_quote_analyzer" : "my_analyzer"
      }
    }
  }
}

# 索引
POST /analyzer_test/_doc/1
{
  "name1" : "this is my hello good",
  "name2" : "this is my hello good",
  "name3" : "this is my hello good"
}

# 索引
POST /analyzer_test/_doc/2
{
  "name1" : "a hello good",
  "name2" : "a hello good",
  "name3" : "a hello good"
}

# 搜索 - 短语 - 不去掉停用词
GET /analyzer_test/_search
{
  "query" : {
    "match_phrase" : {
      "name1" : "a hello good"
    }
  }
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.178298,
    "hits" : [
      {
        "_index" : "analyzer_test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.178298,
        "_source" : {
          "name1" : "a hello good",
          "name2" : "a hello good",
          "name3" : "a hello good"
        }
      }
    ]
  }
}

# 搜索 - 短语 - 去掉停用词
GET /analyzer_test/_search
{
  "query" : {
    "match_phrase" : {
      "name2" : "a hello good"
    }
  }
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.40618476,
    "hits" : [
      {
        "_index" : "analyzer_test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.40618476,
        "_source" : {
          "name1" : "a hello good",
          "name2" : "a hello good",
          "name3" : "a hello good"
        }
      },
      {
        "_index" : "analyzer_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.33081025,
        "_source" : {
          "name1" : "this is my hello good",
          "name2" : "this is my hello good",
          "name3" : "this is my hello good"
        }
      }
    ]
  }
}


# 搜索 - 短语 - search_quote_analyzer - 不去掉短语中的停用词
GET /analyzer_test/_search
{
  "query" : {
    "match_phrase" : {
      "name3" : "a hello good"
    }
  }
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.178298,
    "hits" : [
      {
        "_index" : "analyzer_test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.178298,
        "_source" : {
          "name1" : "a hello good",
          "name2" : "a hello good",
          "name3" : "a hello good"
        }
      }
    ]
  }
}

# 搜索 - 非短语
GET /analyzer_test/_search
{
  "query" : {
    "match" : {
      "name3" : "a hello good"
    }
  }
}

# 结果
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.40618476,
    "hits" : [
      {
        "_index" : "analyzer_test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.40618476,
        "_source" : {
          "name1" : "a hello good",
          "name2" : "a hello good",
          "name3" : "a hello good"
        }
      },
      {
        "_index" : "analyzer_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.33081025,
        "_source" : {
          "name1" : "this is my hello good",
          "name2" : "this is my hello good",
          "name3" : "this is my hello good"
        }
      }
    ]
  }
}

           

继续阅读