带有multi_field的Elasticsearch Analyzer导致Java出错

时间:2014-01-28 10:11:49

标签: java elasticsearch analyzer

我想要实现的目标:我的应用程序是前缀搜索并对公司名称进行排序。这样做的目的是根据公司的第一个字母列出公司,即按a-e排序。

错误:我收到错误的部分是按原始multi_field查询和排序(即.addSort(scriptSort("doc['name.raw'].value.toLowerCase()", "string").order(SortOrder.ASC))

我正在使用elasticsearch版本0.19.10

我的原始映射如下所示:

curl -XPOST localhost:9200/job/company/_mapping -d '{
 "company" : {
     "properties" : {
         "name" : {
             "type" : "multi_field",
               "fields" : {
                   "name" : {
                       "type" : "string"
                   },
                   "raw" : {
                       "type" : "string",
                       "index" : "not_analyzed"
                   }
               }
         }
     }
 }
}'

这很好用。我可以在我的查询中访问name.raw

我使用分析仪的新映射如下所示:

curl -XPOST localhost:9200/job/company/ -d '{
  "settings":{
    "index":{
      "analysis":{
        "analyzer":{
          "analyzer_startswith":{
            "tokenizer":"keyword",
            "filter":"lowercase"
          }
        }
      }
    }
  },
  "mappings" : {
     "company" : {
         "properties" : {
             "name" : {
                 "type" : "multi_field",
                   "fields" : {
                       "name" : {
                           "type" : "string"
                       },
                       "tokenized" : {
                           "search_analyzer" : "analyzer_startswith",
                           "index_analyzer" : "analyzer_startswith",
                           "type" : "string"
                       },
                      "raw" : {
                           "type" : "string",
                           "index" : "not_analyzed"
                      }
                   }
             }
         }
     }
  }
}'

映射已成功创建,但现在我无法从查询中访问name.raw。当我尝试时,我收到以下错误:

{
   "error": "SearchPhaseExecutionException[Failed to execute phase [query], total failure; shardFailures {[saZMhHB6STejY-9r37aN_A][jobads][2]: QueryPhaseExecutionException[[job][2]: query[ConstantScore(NotDeleted(cache(_type:company)))],from[0],size[10],sort[<custom:\"_script\": org.elasticsearch.index.field.function.sort.StringFieldsFunctionDataComparator$InnerSource@101bcf3d>]: Query Failed [Failed to execute main query]]; nested: ElasticSearchIllegalArgumentException[No field found for [name.raw] in mapping with types [company]]; }{[saZMhHB6STejY-9r37aN_A][job][1]: QueryPhaseExecutionException[[job][1]: query[ConstantScore(NotDeleted(cache(_type:company)))],from[0],size[10],sort[<custom:\"_script\": org.elasticsearch.index.field.function.sort.StringFieldsFunctionDataComparator$InnerSource@67969fa5>]: Query Failed [Failed to execute main query]]; nested: ElasticSearchIllegalArgumentException[No field found for [name.raw] in mapping with types [company]]; }]",
   "status": 500
}

我想要实现的是前缀搜索并对公司名称进行排序。名称以字母a-e或f-j等开头......我将用Java执行查询。

有人知道如何正确创建具有multi_field的分析器吗? 任何帮助将不胜感激。

* UPDATE * * **

如果有人有兴趣,这似乎有效:

需要在索引上创建设置:

curl -XPOST localhost:9200/job/ -d '{
  "settings":{
    "index":{
      "analysis":{
        "analyzer":{
          "analyzer_startswith":{
            "tokenizer":"keyword",
            "filter":"lowercase"
          }
        }
      }
    }
  }
}'

然后创建映射:

curl -XPOST localhost:9200/job/company/_mapping -d '{
 "company" : {
     "properties" : {
         "name" : {
             "type" : "multi_field",
               "fields" : {
                   "name" : {
                       "type" : "string"
                   },
                  "tokenized" : {
                      "search_analyzer" : "analyzer_startswith",
                      "index_analyzer" : "analyzer_startswith",
                      "type" : "string"
                  },
                   "raw" : {
                       "type" : "string",
                       "index" : "not_analyzed"
                   }
               }
         }
     }
 }
}'

0 个答案:

没有答案