0

这是我的轮胎设置:

def self.search(params)
    tire.search(load: true, page: params[:page], per_page: 9) do 
      query do
        boolean do
          must { string params[:query], default_operator: "AND" } if params[:query].present?
          must { range :published, lte: Time.zone.now }
          must { term :post_type, params[:post_type] } if params[:post_type].present? 
        end
      end
      sort { by :created_at, "desc" } if params[:query].blank?
      facet "posttypes" do
        terms :post_type
      end
    end
  end   

    settings :analysis => {
      :filter  => {
        :ngram_filter => {
          :type => "nGram",
          :min_gram => 2,
          :max_gram => 12
        }
      },
      :analyzer => {
        :index_ngram_analyzer => {
          :type  => "custom",
          :tokenizer  => "standard",
          :filter  => ["lowercase", "ngram_filter"]
        },
        :search_ngram_analyzer => {
          :type  => "custom",
          :tokenizer  => "standard",
          :filter  => ["standard", "lowercase", "ngram_filter"]
        }
      }
    } do
    mapping do
      indexes :_id, index: :not_analyzed
      indexes :tags, :analyzer => 'keyword'
      indexes :published, type: 'date'
      indexes :created_at, type: 'date'
      [:title, :description, :post_type].each do |attribute|
        indexes attribute, :type => 'string', :index_analyzer => 'index_ngram_analyzer', :search_analyzer => 'search_ngram_analyzer'
      end
    end
  end

  def to_indexed_json
    {
      _id: _id,
      title: title,
      description: description,
      tags: tags,
      post_type: post_type,
      published: published,
      created_at: created_at
    }.to_json
  end

如果我只写一个冒号我得到这个错误:

500 : {"error":"SearchPhaseExecutionException[Failed to execute phase [query], total failure; shardFailures {[r7kpljHrRbOPzjngca91Uw][posts][3]: SearchParseException[[posts][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\"query_string\":{\"query\":\":\"}},\"filter\":{\"range\":{\"published\":{\"lte\":\"2013-03-09T12:51:43Z\"}}},\"size\":9}]]]; nested: QueryParsingException[[posts] Failed to parse query [:]]; nested: ParseException[Cannot parse ':': Encountered \" \":\" \": \"\" at line 1, column 0.\nWas expecting one of:\n    <NOT> ...\n    \"+\" ...\n    \"-\" ...\n    <BAREOPER> ...\n    \"(\" ...\n    \"*\" ...\n    <QUOTED> ...\n    <TERM> ...\n    <PREFIXTERM> ...\n    <WILDTERM> ...\n    \"[\" ...\n    \"{\" ...\n    <NUMBER> ...\n    <TERM> ...\n    \"*\" ...\n    ]; nested: ParseException[Encountered \" \":\" \": \"\" at line 1, column 0.\nWas expecting one of:\n    <NOT> ...\n    \"+\" ...\n    \"-\" ...\n    <BAREOPER> ...\n    \"(\" ...\n    \"*\" ...\n    <QUOTED> ...\n    <TERM> ...\n    <PREFIXTERM> ...\n    <WILDTERM> ...\n    \"[\" ...\n    \"{\" ...\n    <NUMBER> ...\n    <TERM> ...\n    \"*\" ...\n    ]; }{[r7kpljHrRbOPzjngca91Uw][posts][2]: SearchParseException[[posts][2]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\"query_string\":{\"query\":\":\"}},\"filter\":{\"range\":{\"published\":{\"lte\":\"2013-03-09T12:51:43Z\"}}},\"size\":9}]]]; nested: QueryParsingException[[posts] Failed to parse query [:]]; nested: ParseException[Cannot parse ':': Encountered \" \":\" \": \"\" at line 1, column 0.\nWas expecting one of:\n    <NOT> ...\n    \"+\" ...\n    \"-\" ...\n    <BAREOPER> ...\n    \"(\" ...\n    \"*\" ...\n    <QUOTED> ...\n    <TERM> ...\n    <PREFIXTERM> ...\n    <WILDTERM> ...\n    \"[\" ...\n    \"{\" ...\n    <NUMBER> ...\n    <TERM> ...\n    \"*\" ...\n    ]; nested: ParseException[Encountered \" \":\" \": \"\" at line 1, column 0.\nWas expecting one of:\n    <NOT> ...\n    \"+\" ...\n    \"-\" ...\n    <BAREOPER> ...\n    \"(\" ...\n    \"*\" ...\n    <QUOTED> ...\n    <TERM> ...\n    <PREFIXTERM> ...\n    <WILDTERM> ...\n    \"[\" ...\n    \"{\" ...\n    <NUMBER> ...\n    <TERM> ...\n    \"*\" ...\n    ]; }{[r7kpljHrRbOPzjngca91Uw][posts][4]: SearchParseException[[posts][4]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\"query_string\":{\"query\":\":\"}},\"filter\":{\"range\":{\"published\":{\"lte\":\"2013-03-09T12:51:43Z\"}}},\"size\":9}]]]; nested: QueryParsingException[[posts] Failed to parse query [:]]; nested: ParseException[Cannot parse ':': Encountered \" \":\" \": \"\" at line 1, column 0.\nWas expecting one of:\n    <NOT> ...\n    \"+\" ...\n    \"-\" ...\n    <BAREOPER> ...\n    \"(\" ...\n    \"*\" ...\n    <QUOTED> ...\n    <TERM> ...\n    <PREFIXTERM> ...\n    <WILDTERM> ...\n    \"[\" ...\n    \"{\" ...\n    <NUMBER> ...\n    <TERM> ...\n    \"*\" ...\n    ]; nested: ParseException[Encountered \" \":\" \": \"\" at line 1, column 0.\nWas expecting one of:\n    <NOT> ...\n    \"+\" ...\n    \"-\" ...\n    <BAREOPER> ...\n    \"(\" ...\n    \"*\" ...\n    <QUOTED> ...\n    <TERM> ...\n    <PREFIXTERM> ...\n    <WILDTERM> ...\n    \"[\" ...\n    \"{\" ...\n    <NUMBER> ...\n    <TERM> ...\n    \"*\" ...\n    ]; }]","status":500}

如何解决这个问题?

我正在使用带有 mongoid 3.xx 的 mongodb

谢谢!

4

3 回答 3

1

lucene 查询中的特殊字符需要转义

尝试查询\:而不是:.

于 2013-03-10T13:56:13.410 回答
1

这些特殊字符需要转义:

+ - && || ! ( ) { } [ ] ^ " ~ * ? : \

所以你需要\:而不是:

于 2014-01-27T17:50:19.790 回答
0

尝试将其转换为等效的十六进制以转义字符

于 2013-03-10T13:41:16.973 回答