未分析弹性搜索索引

时间:2014-07-11 13:19:50

标签: indexing elasticsearch

我是弹性搜索的新手,我在使用分析仪时遇到了困难。

我正在创建这样的索引(要复制我的问题,您可以直接在控制台中复制和粘贴以下代码。)

请阅读脚本中有关我的问题和问题的评论。

#!/bin/bash
# fails if the index doesn't exist but that's OK
curl -XDELETE 'http://localhost:9200/movies/'

# creating the index that will allow type wrapper, and generate _id automatically from the path

curl -XPOST http://localhost:9200/movies -d '{
    "settings" : {
        "number_of_shards" : 1,
        "mapping.allow_type_wrapper" : true,
        "analysis": {
            "analyzer": {
                "en_std": {
                    "type":"standard",
                    "stopwords": "_english_"
                }
            }
        }

    },
    "mappings" : {
        "movie" : {
            "_id" : {
                "path" : "movie.id"
            }
        }
    }
}'

# inserting some data
curl -XPOST http://localhost:9200/movies/movie -d '{
    "movie" : {
        "id" : 101,
        "title" : "Bat Man",
        "starring" : {
            "firstname" : "Christian",
            "lastname" : "Bale"
        }
    }
}'

#trying to get by ID ... \m/ works!!!
curl -XGET http://localhost:9200/movies/movie/101


# tryign to search using query_string ... \m/ works
curl -XPOST http://localhost:9200/movies/movie/_search -d '{
    "query" : {
        "query_string" : {
            "query" : "bat"
        }
    }
}'

# when i try to search in a paricular field it fails. returns 0 hits
curl -XPOST http://localhost:9200/movies/_search -d '{
    "query" : {
        "query_string" : {
            "query" : "bat",
             "fields" : ["movie.title"]

        }
    }
}'


#I thought the analyzer was the problem, so i checked. 
curl 'http://localhost:9200/movies/movie/_search?pretty=true' -d '{
   "query" : {
        "query_string" : {
            "query" : "bat"

        }
    },
    "script_fields": {
        "terms" : {
            "script": "doc[field].values",
            "params": {
                "field": "movie.title"
            }
        }

    }
}'

# The field wasn't analyzed.
# the follwoing is the result
#{
#  "took" : 1,
#  "timed_out" : false,
#  "_shards" : {
#    "total" : 1,
#    "successful" : 1,
#    "failed" : 0
#  },
#  "hits" : {
#    "total" : 1,
#    "max_score" : 0.13424811,
#    "hits" : [ {
#      "_index" : "movies",
#      "_type" : "movie",
#      "_id" : "101",
#      "_score" : 0.13424811,
#      "fields" : {
#        "terms" : [ "Bat Man" ]
#      }
#    } ]
#  }
#}


# So i even tried the term as such... Nope didn't work :(  0 hits.
curl -XPOST http://localhost:9200/movies/_search -d '{
    "query" : {
        "query_string" : {
            "query" : "Bat Man",
             "fields" : ["movie.title"]

        }
    }
}'

有谁可以指出我做错了什么?

1 个答案:

答案 0 :(得分:0)

您应该在插入文档后立即插入sleep 1命令,一切都会正常工作。

Elasticsearch近乎实时地提供搜索(read this)。每次索引文档时,Lucene索引都不会立即更新(根据Elasticsearch刷新)。刷新索引的频率可在索引级别进行配置。您还可以通过向每个HTTP请求传递查询参数refresh=true来强制刷新索引,这将使ES更新索引。但是你可能会因为这种情况而开始受到影响,这取决于你的要求。

还有一个Refresh API