ruby-on-rails - 弹性-QueryingParsingException-查询不支持-使用过滤器按关联字段搜索时

标签 ruby-on-rails ruby elasticsearch jbuilder

我有一个模型person,其中有很多addresses(关联/表名称person_addresses)
我正在尝试使用户能够通过其地址找到一个人。

当我尝试按地址搜索时,我得到:QueryingParsingException - query does not support [addresses.address]
例如:/api/v1/search/people?q=aaron&address=scarborough
查询:

[ 
   { 
      "query":{ 
         "match":{ 
            "name":"aaron"
         }
      },
      "sort":[ 
         { 
            "id":{ 
               "order":"desc"
            }
         }
      ],
      "filter":{ 
         "bool":{ 
            "must":{ 
               "nested":{ 
                  "path":"addresses",
                  "query":{ 
                     "terms":{ 
                        "addresses.address":"scarborough"
                     }
                  }
               }
            }
         }
      },
      "aggregations":{ 
         "addresses":{ 
            "nested":{ 
               "path":"addresses"
            },
            "aggregations":{ 
               "nested_items":{ 
                  "terms":{ 
                     "field":"addresses.address",
                     "order":{ 
                        "_count":"desc"
                     }
                  }
               }
            }
         }
      }
   }
]

映射:
  mapping do
    indexes :first_name, type: 'multi_field', fields: {
      first_name: { type: 'string', index: 'analyzed' },
      na_first_name: { type: 'string', index: 'not_analyzed' }
    }
    indexes :middle_name, type: 'multi_field', fields: {
      middle_name: { type: 'string', index: 'analyzed' },
      na_middle_name: { type: 'string', index: 'not_analyzed' }
    }
    indexes :last_name, type: 'multi_field', fields: {
      last_name: { type: 'string', index: 'analyzed' },
      na_last_name: { type: 'string', index: 'not_analyzed' }
    }
    indexes :addresses, type: :nested do
      indexes :location, type: :geo_point, index: :not_analyzed
      indexes :country, type: :string, index: :not_analyzed
      indexes :state, type: :string, index: :not_analyzed
      indexes :address, type: :string
      indexes :county, type: :string, index: :not_analyzed
      indexes :city, type: :string, index: :not_analyzed
      indexes :zip, type: :string, index: :not_analyzed
      indexes :state_id, type: :long
      indexes :county_id, type: :long
      indexes :city_id, type: :long
    end
  end

我将此记录编入索引:
 {
        "_index": "pl_people",
        "_type": "person",
        "_id": "813106",
        "_score": null,
        "sort": [
            -9223372036854775808
        ],
        "resource": "Person",
        "parameterized": "813106-aaron-mcguire",
        "name": "Aaron McGuire",
        "phone": "813-689-6889",
        "date_of_birth": "1991-03-01",
        "first_name": "Aaron",
        "last_name": "McGuire",
        "addresses": [
            {
                "id": 1,
                "parameterized": "1",
                "address": "123 Scarborough road",
                "zip": "L5A2A9",
                "city": "Ontario",
                "country": "USA",
                "state": "California",
                "location": null,
                "state_id": null,
                "county_id": null,
                "city_id": null
            }
        ],
        "projects": [],
    }

index.json.jbuilder:
# frozen_string_literal: true

json.query do
  if @term.present?
    json.set!(:match, @field => @term)
  else
    json.set!(:match_all, {})
  end
end

json.sort do
  json.child! do
    json.set!(:id, order: 'desc')
  end
end

if [@address].any?(&:present?)
  json.filter do
    json.partial!('people/index/filters')
  end
end

json.aggregations do
  json.addresses do
    json.nested do
      json.set!(:path, 'addresses')
    end
    json.aggregations do
      json.nested_items do
        json.terms do
          json.set!(:field, 'addresses.address')
          json.set!(:order, _count: 'desc')
        end
      end
    end
  end
end

_filters.json.jbuilder:
# frozen_string_literal: true

json.bool do
  json.must do
    if (value = @address).present?
      json.nested do
        json.path('addresses')
        json.query do
          json.set!(:terms, 'addresses.address' => value)
        end
      end
    end
  end
end

index.rb(人员搜索)
# frozen_string_literal: true

module Searching
  module People
    class Index < ::Searching::ApplicationSearching
      self.valid_params = {
        q: { type: :string, on_blank: :remove },
      }

      delegate :aggregations, to: :results

      def initialize(params = {})
        super(params)
      end

      protected

      def klass
        Person
      end

      def search
        @search ||= ClientSearch.new(
          index: [klass].map(&:index_name).join(','),
          body: body,
          size: limit,
          from: offset,
        )
      end

      def body
        query_field = params[:field].present? ? params[:field] : :name
        Searching::TemplateHandler.(
          'people/index',
          term: Searching::Util.sanitize_string(params[:q]),
          field: query_field,
          address: params[:address],
        )
      end

    end
  end
end
GET /pl_people
{ 
   "pl_people":{ 
      "aliases":{ 

      },
      "mappings":{ 
         "person":{ 
            "properties":{ 
               "ac_name":{ 
                  "type":"string",
                  "analyzer":"autocomplete"
               },
               "addresses":{ 
                  "type":"nested",
                  "properties":{ 
                     "address":{ 
                        "type":"string"
                     },
                     "city":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     },
                     "city_id":{ 
                        "type":"long"
                     },
                     "country":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     },
                     "county":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     },
                     "county_id":{ 
                        "type":"long"
                     },
                     "id":{ 
                        "type":"long"
                     },
                     "location":{ 
                        "type":"geo_point"
                     },
                     "parameterized":{ 
                        "type":"string"
                     },
                     "state":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     },
                     "state_id":{ 
                        "type":"long"
                     },
                     "zip":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     }
                  }
               },
               "author":{ 
                  "type":"string",
                  "index":"not_analyzed"
               },
               "body":{ 
                  "type":"string",
                  "analyzer":"remove_html",
                  "fields":{ 
                     "ns_body":{ 
                        "type":"string",
                        "analyzer":"remove_html_stopwords"
                     }
                  }
               },
               "charities":{ 
                  "type":"nested",
                  "properties":{ 
                     "email":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     },
                     "id":{ 
                        "type":"long"
                     }
                  }
               },
               "community":{ 
                  "properties":{ 
                     "name":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     },
                     "parameterized":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     },
                     "slug":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     }
                  }
               },
               "created_at":{ 
                  "type":"date",
                  "format":"dateOptionalTime"
               },
               "date_of_birth":{ 
                  "type":"date",
                  "format":"dateOptionalTime"
               },
               "description":{ 
                  "type":"string"
               },
               "employments":{ 
                  "type":"nested",
                  "properties":{ 
                     "email":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     },
                     "employment_status":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     },
                     "foia_contact":{ 
                        "type":"boolean"
                     },
                     "id":{ 
                        "type":"long"
                     },
                     "phone":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     },
                     "phone_extension":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     }
                  }
               },
               "first_name":{ 
                  "type":"string",
                  "fields":{ 
                     "na_first_name":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     }
                  }
               },
               "last_name":{ 
                  "type":"string",
                  "fields":{ 
                     "na_last_name":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     }
                  }
               },
               "market":{ 
                  "properties":{ 
                     "name":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     },
                     "parameterized":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     },
                     "slug":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     }
                  }
               },
               "middle_name":{ 
                  "type":"string",
                  "fields":{ 
                     "na_middle_name":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     }
                  }
               },
               "most_recent_organization":{ 
                  "properties":{ 
                     "description":{ 
                        "type":"string"
                     },
                     "id":{ 
                        "type":"long"
                     },
                     "name":{ 
                        "type":"string"
                     },
                     "parameterized":{ 
                        "type":"string"
                     },
                     "phone":{ 
                        "type":"string"
                     }
                  }
               },
               "name":{ 
                  "type":"string",
                  "fields":{ 
                     "na_name":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     },
                     "ngram_name":{ 
                        "type":"string",
                        "analyzer":"my_start"
                     },
                     "ns_name":{ 
                        "type":"string",
                        "analyzer":"no_stopwords"
                     }
                  }
               },
               "organizations":{ 
                  "properties":{ 
                     "name":{ 
                        "type":"string"
                     },
                     "parameterized":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     }
                  }
               },
               "package":{ 
                  "properties":{ 
                     "name":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     },
                     "parameterized":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     },
                     "slug":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     }
                  }
               },
               "parameterized":{ 
                  "type":"string",
                  "index":"not_analyzed"
               },
               "phone":{ 
                  "type":"string"
               },
               "photo":{ 
                  "properties":{ 
                     "large":{ 
                        "type":"string"
                     },
                     "medium":{ 
                        "type":"string"
                     },
                     "teaser":{ 
                        "type":"string"
                     },
                     "thumb":{ 
                        "type":"string"
                     },
                     "url":{ 
                        "type":"string"
                     }
                  }
               },
               "projects":{ 
                  "properties":{ 
                     "id":{ 
                        "type":"long"
                     },
                     "name":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     },
                     "parameterized":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     },
                     "slug":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     }
                  }
               },
               "public_offices":{ 
                  "type":"nested",
                  "properties":{ 
                     "email":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     },
                     "employment_status":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     },
                     "id":{ 
                        "type":"long"
                     }
                  }
               },
               "published":{ 
                  "type":"string",
                  "index":"not_analyzed"
               },
               "region":{ 
                  "properties":{ 
                     "name":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     },
                     "parameterized":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     },
                     "slug":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     }
                  }
               },
               "resource":{ 
                  "type":"string"
               },
               "short_description":{ 
                  "type":"string"
               },
               "show_path":{ 
                  "type":"string"
               },
               "time":{ 
                  "type":"date",
                  "format":"dateOptionalTime"
               },
               "updated_at":{ 
                  "type":"date",
                  "format":"dateOptionalTime"
               }
            }
         }
      },
      "settings":{ 
         "index":{ 
            "creation_date":"1581684793139",
            "analysis":{ 
               "filter":{ 
                  "english_stop":{ 
                     "type":"stop",
                     "stopwords":[ 
                        "a",
                        "an",
                        "and",
                        "are",
                        "as",
                        "at",
                        "be",
                        "but",
                        "by",
                        "for",
                        "if",
                        "in",
                        "into",
                        "is",
                        "it",
                        "no",
                        "not",
                        "of",
                        "on",
                        "or",
                        "such",
                        "that",
                        "the",
                        "their",
                        "then",
                        "there",
                        "these",
                        "they",
                        "this",
                        "to",
                        "was",
                        "with"
                     ]
                  },
                  "my_edge":{ 
                     "min_gram":"2",
                     "side":"front",
                     "type":"edgeNGram",
                     "max_gram":"18"
                  }
               },
               "analyzer":{ 
                  "no_stopwords":{ 
                     "type":"stop",
                     "stopwords":"_english_"
                  },
                  "comma":{ 
                     "pattern":",",
                     "type":"pattern"
                  },
                  "default":{ 
                     "filter":[ 
                        "standard",
                        "lowercase",
                        "english_stop",
                        "word_delimiter"
                     ],
                     "tokenizer":"standard"
                  },
                  "remove_html_stopwords":{ 
                     "filter":"stop",
                     "char_filter":"html_strip",
                     "type":"custom",
                     "stopwords":"_english_",
                     "tokenizer":"lowercase"
                  },
                  "autocomplete":{ 
                     "filter":[ 
                        "lowercase",
                        "apostrophe"
                     ],
                     "tokenizer":"keyword"
                  },
                  "my_start":{ 
                     "filter":[ 
                        "asciifolding",
                        "lowercase",
                        "my_edge"
                     ],
                     "tokenizer":"whitespace"
                  },
                  "remove_html":{ 
                     "filter":"lowercase",
                     "char_filter":"html_strip",
                     "type":"custom",
                     "tokenizer":"standard"
                  }
               }
            },
            "number_of_shards":"5",
            "number_of_replicas":"1",
            "version":{ 
               "created":"1070699"
            },
            "uuid":"LZrnPjxiTwyNf-Ggo3bXtw"
         }
      },
      "warmers":{ 

      }
   }
}

我没有遵循的任何命名约定吗?
我尝试将filter.json和index.json上的person_address更改为address,但仍然没有成功。

最佳答案

所以我重新安装了es 1.7并做了一些尝试,简化了您的数据。

错误出在过滤器的“术语”中,您必须改用术语/匹配。如果要添加多个条件,则似乎需要构建一个 bool(boolean) 查询,并在每个条件上加一个术语。

而且我不明白为什么你在这里使用post_filter(顶级过滤器= es> 2.X中的post_filter),你能解释一下吗?

如果要复制,这就是我使用HEAD所做的事情(我简化了映射,仅保留了相关字段)。

希望对您有所帮助。

http://localhost:9200/pl_people/(POST)

{ 
      "mappings":{ 
         "person":{ 
            "properties":{ 
               "ac_name":{ 
                  "type":"string"
               },
               "addresses":{ 
                  "type":"nested",
                  "properties":{ 
                     "address":{ 
                        "type":"string"
                     },
                     "city":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     },
                     "city_id":{ 
                        "type":"long"
                     },
                     "country":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     },
                     "county":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     },
                     "county_id":{ 
                        "type":"long"
                     },
                     "id":{ 
                        "type":"long"
                     },
                     "location":{ 
                        "type":"geo_point"
                     },
                     "parameterized":{ 
                        "type":"string"
                     },
                     "state":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     },
                     "state_id":{ 
                        "type":"long"
                     },
                     "zip":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     }
                  }
               },
               "date_of_birth":{ 
                  "type":"date",
                  "format":"dateOptionalTime"
               },
               "first_name":{ 
                  "type":"string",
                  "fields":{ 
                     "na_first_name":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     }
                  }
               },
               "last_name":{ 
                  "type":"string",
                  "fields":{ 
                     "na_last_name":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     }
                  }
               },
               "name":{ 
                  "type":"string",
                  "fields":{ 
                     "na_name":{ 
                        "type":"string",
                        "index":"not_analyzed"
                     },
                     "ngram_name":{ 
                        "type":"string"
                     },
                     "ns_name":{ 
                        "type":"string"
                     }
                  }
               },
               "parameterized":{ 
                  "type":"string",
                  "index":"not_analyzed"
               },
               "phone":{ 
                  "type":"string"
               },
               "resource":{ 
                  "type":"string"
               }
            }
         }
      }
}

http://localhost:9200/pl_people/person/813106(POST)
{
        "ac_name": "Aaron McGuire",
        "resource": "Person",
        "parameterized": "813106-aaron-mcguire",
        "name": "Aaron McGuire",
        "phone": "813-689-6889",
        "date_of_birth": "1991-03-01",
        "first_name": "Aaron",
        "last_name": "McGuire",
        "addresses": [
            {
                "id": 1,
                "parameterized": "1",
                "address": "123 Scarborough road",
                "zip": "L5A2A9",
                "city": "Ontario",
                "country": "USA",
                "state": "California"
            }
        ]
    }

http://localhost:9200/pl_people/person/_search/(发布)
{
  "query": {
    "match": {
      "name": "aaron"
    }
  },
  "sort": [
    {
      "id": {
        "order": "desc"
      }
    }
  ],
  "filter": {
    "bool": {
      "must": {
        "nested": {
          "path": "addresses",
          "query": {
            "term": {
              "addresses.address": "scarborough"
            }
          }
        }
      }
    }
  },
  "aggregations": {
    "addresses": {
      "nested": {
        "path": "addresses"
      },
      "aggregations": {
        "nested_items": {
          "terms": {
            "field": "addresses.address",
            "order": {
              "_count": "desc"
            }
          }
        }
      }
    }
  }
}

关于ruby-on-rails - 弹性-QueryingParsingException-查询不支持-使用过滤器按关联字段搜索时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60227548/

相关文章:

elasticsearch - Logstash在 Elasticsearch 中插入记录时跳过记录

ruby-on-rails - Heroku 应用程序似乎从错误的 Git 存储库/分支中提取

ruby-on-rails - Rails 应用程序中的代理 Websocket 请求

ruby-on-rails - 事件管理员 : Customize only new form

ruby-on-rails - Paypal IPN : Invalid byte sequence in UTF-8

java - 使用Java,如何在Elasticsearch中将匹配查询的默认运算符更改为AND?

ruby-on-rails - Rails Geocoder near 方法将结果限制为 100

ruby-on-rails - 测试之间的 Rails 清理数据库不起作用

ruby-on-rails - ajax 检查电子邮件是否已经存在 Ruby on Rails

elasticsearch - 设置 ElasticSearch 以仅在每个客户的数据中进行搜索的最佳方式