文章 十一月 29, 2017

elasticsearch简单配置

文章字数 4.4k 阅读约需 4 mins. 阅读次数 0

全新基于elasticsearch的搜索API
使用Laravel

ssh://git@127.0.0.1:9922/apitmt2.git

1.INDEX创建

curl -xput 'http://localhost:9200/tmtpost'

2.创建users document的mapping

curl -xput 'http://localhost:9200/tmtpost/users/_mapping'

'{
  "users": {
    "properties": {
      "name": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_max_word",
        "include_in_all": "true",
        "boost": 8
      },
      "bio": {
        "type": "text",
        "analyzer": "ik_smart",
        "search_analyzer": "ik_smart",
        "include_in_all": "true",
        "boost": 8
      }
    }
  }
}'

3.创建posts document的mapping

curl -xput 'http://localhost:9200/tmtpost/posts/_mapping'

'{
  "users": {
    "properties": {
      "title": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_max_word",
        "include_in_all": "true",
        "boost": 8
      },
      "summary": {
        "type": "text",
        "analyzer": "ik_smart",
        "search_analyzer": "ik_smart",
        "include_in_all": "true",
        "boost": 8
      }
    }
  }
}'

4.导入数据

curl -xput 'http://localhost:9200/tmtpost/users/1'

'{
  "name": "阿Jo",
  "bio": "他是一个好人"
}'

curl -xput 'http://localhost:9200/tmtpost/users/2'

'{
  "name": "王老王",
  "bio": "他是一个坏人"
}'

5.导入数据

curl -xput 'http://localhost:9200/tmtpost/posts/1'

'{
  "title": "阿Jo",
  "summary": "他是一个好人"
}'

6.测试搜索

curl -xpost 'http://localhost:9200/tmtpost/users/_search'  or  curl -xpost 'http://localhost:9200/tmtpost/_search'

'{
  "query": {
    "match": {
      "bio": "一个"
    }
  },
  "highlight": {
    "pre_tags": [
      "<tag1>",
      "<tag2>"
    ],
    "post_tags": [
      "</tag1>",
      "</tag2>"
    ],
    "fields": {
      "bio": {}
    }
  }
}'

{
   "query": {
        "multi_match": {
            "query":  "一个",
            "type":   "most_fields",
            "fields": [ "title", "name", "bio", "summary" ]
        }
    }
}

7.查询结果

{
    "took": 13,
    "timed_out": false,
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 2.025282,
        "hits": [{
                "_index": "tmtpost",
                "_type": "users",
                "_id": "1",
                "_score": 2.025282,
                "_source": {
                    "name": "阿Jo",
                    "bio": "他是一个好人"
                }
            },
            {
                "_index": "tmtpost",
                "_type": "posts",
                "_id": "1",
                "_score": 2.025282,
                "_source": {
                    "title": "阿Jo",
                    "summary": "他是一个好人"
                }
            }
        ]
    }
}

上一篇:
下一篇:
0%