• elasticsearch mapping demo


    curl -XPUT localhost:9200/local -d '{
        "settings" : {
            "analysis" : {
                "analyzer" : {
                    "stem" : {
                        "tokenizer" : "standard",
                        "filter" : ["standard", "lowercase", "stop", "porter_stem"]
                    }
                }
            }
        },
        "mappings" : {
            "article" : {
                "dynamic" : true,
                "properties" : {
                    "title" : {
                        "type" : "string",
                        "analyzer" : "stem"
                    }
                }
            }
        }
    }'
     
    # Sample Analysis 
    curl -XGET localhost:9200/local/_analyze?analyzer=stem -d '{Fight for your life}'
    curl -XGET localhost:9200/local/_analyze?analyzer=stem -d '{Bruno fights Tyson tomorrow}'
     
    # Index Data
    curl -XPUT localhost:9200/local/article/1 -d'{"title": "Fight for your life"}'
    curl -XPUT localhost:9200/local/article/2 -d'{"title": "Fighting for your life"}'
    curl -XPUT localhost:9200/local/article/3 -d'{"title": "My dad fought a dog"}'
    curl -XPUT localhost:9200/local/article/4 -d'{"title": "Bruno fights Tyson tomorrow"}'
     
    # search on the title field, which is stemmed on index and search
    curl -XGET localhost:9200/local/_search?q=title:fight
     
    # searching on _all will not do anystemming, unless also configured on the mapping to be stemmed...
    curl -XGET localhost:9200/local/_search?q=fight
    

      

    curl -XPUT http://localhost:9200/test_index/ -d '
    {
        "index": {
            "analysis": {
                "analyzer": {
                    "index_analyzer": {
                        "tokenizer": "nGram",
                        "filter": ["lowercase", "snowball"]
                    },
                    "search_analyzer": {
                        "tokenizer": "nGram",
                        "filter": ["lowercase", "snowball"]
                    }
                },
                "filter": {
                    "snowball": {
                        "type": "snowball",
                        "language": "English"
                    }
                }
            }
        }
    }'
     
    curl -XPUT 'http://localhost:9200/test_index/item/_mapping' -d '
    {
        "item": {
          "properties": {
            "title": {
              "type": "string",
              "boost": 2.0,
              "index": "analyzed",
              "store": "yes",
              "term_vector" : "with_positions_offsets"
            },
            "description": {
              "type": "string",
              "boost": 1.0,
              "index": "analyzed",
              "store": "yes",
              "term_vector" : "with_positions_offsets"
            },
            "link": {
              "type": "string"
            }
          }
        }
    }'
    

      

    # curl -XDELETE http://localhost:9200/test-index
     
    # "analyzer"."default" => default name for index and search
    # "tokenizer" : "standard" => splits words at punctuation characters
    # http://www.elasticsearch.org/guide/reference/index-modules/analysis/
     
    curl -XPUT http://localhost:9200/test-index/ -d '
    {
        "index": {
            "analysis": {
                "analyzer": {
                    "default": {
                        "tokenizer": "standard",
                        "filter": ["lowercase", "snowball"]
                    }
                }
            }
        }
    }'
     
     
    # http://www.elasticsearch.org/guide/reference/api/search/highlighting.html
    # "store": "yes" => enable highlighting
    # "term_vector" : "with_positions_offsets" => for performance
     
    curl -XPUT http://localhost:9200/test-index/test-item/_mapping -d '
    {
        "test-item": {
            "properties": {
                "name": {
                    "type": "string",
                    "store": "yes",
                    "term_vector": "with_positions_offsets"
                },
                "description": {
                    "type": "string",
                    "store": "yes",
                    "term_vector": "with_positions_offsets"
                },
                "field-without-highlighting": {
                    "type": "string"
                }
            }
        }
    }'
     
     
    curl -XPUT http://localhost:9200/test-index/test-item/1 -d '
    {
        "name": "Example One",
        "description": "Create a test item for to the index.",
        "field-without-highlighting": "test"
    }'
     
     
    # "highlight" => wrap search result in <em> tags (define own tags with pre_tags/post_tags)
    # "number_of_fragments": 0 => don"t split field in multiple fragments
    # http://www.elasticsearch.org/guide/reference/api/search/highlighting.html
     
    curl -XGET http://localhost:9200/test-index/test-item/_search?pretty=true -d '
    {
        "highlight": {
            "fields": {
                "name": {
                    "number_of_fragments": 0
                },
                "description": {
                    "number_of_fragments": 0
                }
            }
        },
        "query": {
            "query_string": {
                "fields": ["name", "description"],
                "query": "test"
            }
        }
    }'
     
    => 
     
    {
        "took": 4,
        "timed_out": false,
        "_shards": {
            "total": 5,
            "successful": 5,
            "failed": 0
        },
        "hits": {
            "total": 1,
            "max_score": 0.029424578,
            "hits": [{
                "_index": "test-index",
                "_type": "test-item",
                "_id": "1",
                "_score": 0.029424578,
                "_source": {
                    "name": "Example One",
                    "description": "Create a test item for to the index.",
                    "field-without-highlighting": "test"
                },
                "highlight": {
                    "description": ["Create a <em>test</em> item for to the index."]
                }
            }]
        }
    }
     
    curl -XGET http://localhost:9200/test-index/test-item/_search?pretty=true -d '
    {
        "highlight": {
            "fields": {
                "name": {
                    "number_of_fragments": 0
                },
                "description": {
                    "number_of_fragments": 0
                }
            }
        },
        "query": {
            "query_string": {
                "fields": ["name", "description"],
                "query": "created EXAMPLES"
            }
        }
    }'
     
    => 
     
    {
        "took": 6,
        "timed_out": false,
        "_shards": {
            "total": 5,
            "successful": 5,
            "failed": 0
        },
        "hits": {
            "total": 1,
            "max_score": 0.06241896,
            "hits": [{
                "_index": "test-index",
                "_type": "test-item",
                "_id": "1",
                "_score": 0.06241896,
                "_source": {
                    "name": "Example One",
                    "description": "Create a test item for to the index.",
                    "field-without-highlighting": "test"
                },
                "highlight": {
                    "description": ["<em>Create</em> a test item for to the index."],
                    "name": ["<em>Example</em> One"]
                }
            }]
        }
    }
    

      

  • 相关阅读:
    由@Convert注解引出的jackson对枚举的反序列化规则
    List.contains()与自动拆箱
    Utf-8+Bom编码导致的读取数据部分异常问题
    ResouceUtils.getFile()取不到Jar中资源文件源码小结
    Java自动装箱中的缓存原理
    Javaconfig形式配置Dubbo多注册中心
    logback多环境配置
    Spring @Scheduled @Async联合实现调度任务(2017.11.28更新)
    Nginx的Access日志记录的时机
    Mysql索引引起的死锁
  • 原文地址:https://www.cnblogs.com/huangfox/p/3629314.html
Copyright © 2020-2023  润新知