text elasticsearch analyzer示例 - 测试查询

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text elasticsearch analyzer示例 - 测试查询相关的知识,希望对你有一定的参考价值。

#  Index
---------------------------------------------------------------------

curl -XPUT http://localhost:9200/pictures/ -d '
{
    "settings": {
        "analysis": {
            "analyzer": {
                "index_analyzer": {
                    "tokenizer": "standard",
                    "filter": ["standard", "my_delimiter", "lowercase", "stop", "asciifolding", "porter_stem"]
                },
                "search_analyzer": {
                    "tokenizer": "standard",
                    "filter": ["standard", "lowercase", "stop", "asciifolding", "porter_stem"]
                }
            },
            "filter": {
                "my_delimiter": {
                    "type": "word_delimiter",
				"generate_word_parts": true,                
			        "catenate_words": true,
			        "catenate_numbers": true,
			        "catenate_all": true,
			        "split_on_case_change": true,
			        "preserve_original": true,
			        "split_on_numerics": true,
			        "stem_english_possessive": true
                }
            }
        }
    }
}'


_____________________________________________________________________

#  Mapping
---------------------------------------------------------------------

curl -XPUT 'http://localhost:9200/pictures/picture/_mapping' -d '
{
    "picture": {
      "_all" : {"enabled" : true, "index_analyzer": "index_analyzer", "search_analyzer": "search_analyzer"},
      "properties": {
        "id": {
          "type": "string",
          "index": "not_analyzed"
        },
        "title": {
          "type": "string",
          "boost": 7.0,
          "index": "analyzed",
          "index_analyzer": "index_analyzer",
          "search_analyzer": "search_analyzer",
          "store": "yes"
        },
        "description": {
          "type": "string",
          "boost": 1.0,
          "index": "analyzed",
          "index_analyzer": "index_analyzer",
          "search_analyzer": "search_analyzer"
        },
        "featured": {
          "type": "boolean",
          "boost": 10.0,
          "index": "not_analyzed"
        },
        "categories": {
          "type": "string",
          "boost": 2.0,
          "index_name": "category",
          "index": "analyzed",
          "index_analyzer": "index_analyzer",
          "search_analyzer": "search_analyzer",
          "store": "yes"
        },
        "tags": {
          "type": "string",
          "boost": 4.0,
          "index_name": "tag",
          "index": "analyzed",
          "index_analyzer": "index_analyzer",
          "search_analyzer": "search_analyzer",
          "store": "yes"
        },
        "created_at": {
          "type": "double",
          "index": "not_analyzed"
        }
      }
    }
}'

_____________________________________________________________________


# Some data
---------------------------------------------------------------------

{
    "picture" {
        "id" : "4df1ad2df10a854a9d000026"
        "title" : "Birds on the beach",
	"description" : "",
        "featured" : false,
        "categories" : ["Landscapes","Nature"],
        "tags" : ["beach","landscape","ocean","sand","sky","birds"],
	"created_at" : 1307684141.682636
    }
}

{
    "picture" {
        "id" : "4defd951f10a8524b8000005"
        "title" : "Rock on the beach",
	"description" : "",
        "featured" : false,
        "categories" : ["Landscapes","Nature"],
        "tags" : ["beach","landscape","ocean","rock","sand","seaside","sky"],
	"created_at" : 11307564214.04741
    }
}

{
    "picture" {
        "id" : "4deff7fcf10a8527e4000012"
        "title" : "Rock in the ocean",
	"description" : "",
        "featured" : false,
        "categories" : ["Landscapes","Nature"],
        "tags" : ["beach","landscape","ocean","rock","sand","seaside","sky"],
	"created_at" : 1307572220.6159518
    }
}


{
    "picture" {
        "id" : "4defe065f10a8524b8000040"
        "title" : "Beautiful sunset over the ocean",
	"description" : "",
        "featured" : false,
        "categories" : ["Landscapes","Nature"],
        "tags" : ["landscape","ocean","sky","sunset"],
	"created_at" : 1307564214.04741
    }
}


{
    "picture" {
        "id" : "4df1acaef10a854a9d000020"
        "title" : "Purple sunset",
	"description" : "",
        "featured" : false,
        "categories" : ["Landscapes","Nature"],
        "tags" : ["landscape","ocean","sunset"],
	"created_at" : 1307684014.390545
    }
}


{
    "picture" {
        "id" : "4df1b7fcf10a854a9d000077"
        "title" : "",
	"description" : "",
        "featured" : false,
        "categories" : ["Landscapes","Nature"],
        "tags": ["ocean"],
	"created_at" : 1307686908.699473
    }
}


{
    "picture" {
        "id" : "4defe0ecf10a8524b8000047"
        "title" : "Victoria's Secret photoshoot",
	"description" : "",
        "featured" : false,
        "categories" : ["Fashion", "Girls"],
        "tags": ["girl", "photoshoot", "supermodel", "Victoria's Secret"],
	"created_at" : 1307564214.04741
    }
}

_____________________________________________________________________

#  Queries for word delemiter
---------------------------------------------------------------------

1. OK
http://localhost:9200/pictures/_search?q=victoria's
{ "hits" : {"total":1 ... "_id":"4defe0ecf10a8524b8000047" ... } }

2. OK
http://localhost:9200/pictures/_search?q=tags:victorias
{ "hits" : {"total":1 ... "_id":"4defe0ecf10a8524b8000047" ... } }

3. OK
http://localhost:9200/pictures/_search?q=victorias
{ "hits" : {"total":1 ... "_id":"4defe0ecf10a8524b8000047" ... } }

4. OK
http://localhost:9200/pictures/_search?q=victoria
{ "hits" : {"total":1 ... "_id":"4defe0ecf10a8524b8000047" ... } }

_____________________________________________________________________

#  Queries for stemming
---------------------------------------------------------------------

1. OK
http://localhost:9200/pictures/_search?q=birds
{ "hits" : {"total":1 ... "_id":"4df1ad2df10a854a9d000026" ... } }

2. OK
http://localhost:9200/pictures/_search?q=tags:bird
{ "hits" : {"total":1 ... "_id":"4df1ad2df10a854a9d000026" ... } }

3. OK
http://localhost:9200/pictures/_search?q=bird
{ "hits" : {"total":1 ... "_id":"4df1ad2df10a854a9d000026" ... } }

_____________________________________________________________________


#  Queries for "More Like This"
---------------------------------------------------------------------

1. OK
http://localhost:9200/pictures/picture/4defe065f10a8524b8000040/_mlt?min_term_freq=1&min_doc_freq=1
{ "hits" : {"total":5 ...}}

2. OK
http://localhost:9200/pictures/picture/4df1b7fcf10a854a9d000077/_mlt?min_term_freq=1&min_doc_freq=1
{ "hits" : {"total":5 ...}}

...

5. OK
http://localhost:9200/pictures/picture/4deff7fcf10a8527e4000012/_mlt?mlt_fields=categories&min_term_freq=1&min_doc_freq=1
{ "hits" : {"total":5 ...}}

_____________________________________________________________________

以上是关于text elasticsearch analyzer示例 - 测试查询的主要内容,如果未能解决你的问题,请参考以下文章

Elasticsearch关于 Analyzers 的一切,第2部分

ElasticSearch 常用设置

Elasticsearches 7 Failed to parse value [analyzed] as only [true] or [false] are allowed

elasticsearch Suggester实现智能提示

Elasticsearch 之 Hello World

elasticsearch5.6.8中文分词器