简体   繁体   English

基于MongoDB中文档属性的弹性/最近搜索

[英]Elastic/Nearest search based on document properties in MongoDB

We need to accomplish the nearest search based on document properties in MongoDB. 我们需要基于MongoDB中的文档属性完成最近的搜索。

Let's take an example, there is a Car schema in MongoDB, information will be stored as something similar to: 让我们举个例子,MongoDB中有一个Car模式,信息将存储为类似于以下内容的内容:

{
  Make: "Hyundai",
  Model: "Creta",
  Title: "Hyundai Creta E 1.6 Petrol",
  Description: "Compact SUV",
  Feature: {
    ABS:    true,
    EBD:    true,
    Speakers: 4,
    Display: false
  },
  Specification: {
    Length: "4270 mm",
    Width: "1780 mm",
    Height: "1630 mm",
    Wheelbase:  "2590 mm",
    Doors:  5,
    Seating:    5,
    Displacement: "1591 cc"
  },
  Safety: {
    Airbags: 2,
    SeatBeltWarning: false
  },
  Maintenance: {
    LastService: "21/06/2016",
    WashingDone: true
  }
}

Search needs to be provided based on following criteria: 需要根据以下条件提供搜索:

1. Make
2. Model
3. ABS
4. Seating
5. Displacement
6. Airbags

Now results should contain records where 3 or more of the properties match (exact match), and ordered based on the maximum number of properties that match. 现在,结果应包含3个或更多属性匹配(完全匹配)的记录,并根据匹配的最大属性数进行排序。

What is the best way to implement this with MongoDB? 用MongoDB实现此目的的最佳方法是什么?

You could write something to generate documents for each triplet of fields, and then put them together with $or , producing something like 您可以编写一些内容来为每个三元组字段生成文档,然后将它们与$or放在一起,生成类似

{$or: [
    {Make: "Hyundai", Model: "Creta", Feature.ABS: true},
    {Make: "Hyundai", Model: "Creta", Specification.Seating: 5},
    ...
]}

Sorting probably requires sorting by textScore . 排序可能需要按textScore进行排序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM