Quantcast
Channel: 钻戒 and 仁豆米
Viewing all articles
Browse latest Browse all 290

Elasticsearch数据的增删改查

$
0
0

我们前面其实已经提到了数据的增加和查询,这里就详细说一下。

一、Elasticsearch数据的增加和修改是一样的。

curl -XPUT "http://localhost:9200/movies/movie/1" -d'  
{
    "title": "The Godfather",
    "director": "Francis Ford Coppola",
    "year": 1972
}'

以上等于往index: movies、type: movie 、id: 1提交了一条数据。

往elasticsearch提交数据的时候,index/type/id是三要素。

二、删除

根据id直接删除

curl -XDELETE "http://localhost:9200/movies/movie/1" -d''  

三、查询 查询比较复杂:

如果知道记录的id,可以直接显示数据:

curl -XGET "http://localhost:9200/movies/movie/1" -d''  

泛查询,查询所有的index和type和所有字段,来匹配某字串:

curl -XPOST "http://localhost:9200/_search" -d'  
{
    "query": {
        "query_string": {
            "query": "God"
        }
    }
}'

只搜索title字段,其他字段不搜:

curl -XPOST "http://localhost:9200/_search?pretty=on" -d'  
{
    "query": {
        "query_string": {
            "query": "Godfather",
            "fields": ["title"]
        }
    }
}'

对查询结果再次过滤:

curl -XPOST "http://localhost:9200/_search" -d'  
{
    "query": {
        "filtered": {
            "query": {
                "query_string": {
                    "query": "Ford"
                }
            },
            "filter": {
                "term": { "year": 1972 }
            }
        }
    }
}'

如果只做过滤,不做查询:

curl -XPOST "http://localhost:9200/_search" -d'  
{
    "query": {
        "filtered": {
            "query": {
                "match_all": {
                }
            },
            "filter": {
                "term": { "year": 1972 }
            }
        }
    }
}'

只做过滤,不做查询的另一种写法:

curl -XPOST "http://localhost:9200/_search" -d'  
{
    "query": {
        "constant_score": {
            "filter": {
                "term": { "year": 1972 }
            }
        }
    }
}'

来个复杂的,如果我们要查 "director"是"Francis Ford Coppola",这个,我们来发一下查询:

curl -XPOST "http://localhost:9200/_search" -d'  
{
    "query": {
        "constant_score": {
            "filter": {
                "term": { "director": "Francis Ford Coppola" }
            }
        }
    }
}'

结果是查不到的,为毛呢?因为elasticsearch是有分词和分析的,它把"Francis Ford Coppola"给拆分了,所以如果你单查Francis、Ford、Coppola都可以查到,你放一起反而查不到了。

如何能让它查到呢?

简单,重建mapping,我们给director弄多一个虚字段,不让他拆即可

原始的mapping,director是个字符串:

# curl http://localhost:9200/movies/movie/_mapping?pretty=on
{
  "movies" : {
    "mappings" : {
      "movie" : {
        "properties" : {
          "director" : {
            "type" : "string"
          },
          "title" : {
            "type" : "string"
          },
          "year" : {
            "type" : "long"
          }
        }
      }
    }
  }
}

修改mapping,增加original虚字段,且对字段不做分析

curl -XPUT "http://localhost:9200/movies/movie/_mapping" -d'  
{
   "movie": {
      "properties": {
         "director": {
            "type": "multi_field",
            "fields": {
                "director": {"type": "string"},
                "original": {"type" : "string", "index" : "not_analyzed"}
            }
         }
      }
   }
}'

修改过后:

# curl http://localhost:9200/movies/movie/_mapping?pretty=on
{
  "movies" : {
    "mappings" : {
      "movie" : {
        "properties" : {
          "director" : {
            "type" : "string",
            "fields" : {
              "original" : {
                "type" : "string",
                "index" : "not_analyzed"
              }
            }
          },
          "title" : {
            "type" : "string"
          },
          "year" : {
            "type" : "long"
          }
        }
      }
    }
  }
}

这样就多出了个虚字段,这个字段的内容是不分拆的。查询它的时候改查这个字段,就可以查到了:

curl -XPOST "http://localhost:9200/_search" -d'  
{
    "query": {
        "constant_score": {
            "filter": {
                "term": { "director.original": "Francis Ford Coppola" }
            }
        }
    }
}'

注意,更改mapping后,你得重提提交一下数据,否则index不会生效。查到的还是0,重新提交数据后,查到的结果就是1了。


Viewing all articles
Browse latest Browse all 290

Trending Articles