Есть ли способ написать логический запрос с различными условиями вasticsearch? - PullRequest
0 голосов
/ 21 января 2019

Я настроил эластичный поиск с NodeJS-сервером и мне нужен рабочий логический запрос, который проверяет различные условия поиска. Как это сделать?

Я использую mongoosastic (запрос DSL) с NodeJS и следующий запрос для получения результатов

Функция отображения

async function mapMongoToElastic() {
return new Promise((resolve, reject) => {
console.log("---------Mapping gets created---------");
Product.createMapping(
  {
    mappings: {
      product: {
        properties: {
          ArtNumber: { type: "text" },
          Title: {
            type: "text",
            fields: {
              keyword: {
                type: "keyword"
              }
            }
          },
          DisplayPrice: { type: "double" },
          CF_Deliverytime: { type: "text" },
          Description_Short: { type: "text" },
          MainCat: {
            type: "text",
            fields: {
              keyword: {
                type: "keyword"
              }
            }
          },
          ItemCat: {
            type: "text",
            fields: {
              keyword: {
                type: "keyword"
              }
            }
          },
          Deeplink1: { type: "text" },
          Img_url: { type: "text" },
          CF_img_url2: { type: "text" },
          CF_img_url3: { type: "text" },
          CF_Availability: { type: "text" },
          CF_Productrating: {
            type: "text",
            fields: {
              keyword: {
                type: "keyword"
              }
            }
          },
          CF_Discount: {
            type: "text",
            fields: {
              keyword: {
                type: "keyword"
              }
            }
          },
          Shop: {
            type: "text",
            fields: {
              keyword: {
                type: "keyword"
              }
            }
          }
        }
      }
    }
  },
  function(err, mapping) {
    if (err) {
      console.log("error creating mapping (you can safely ignore this)");
      console.log(err);
      resolve(err);
    } else {
      console.log("X - ElasticSearch Mapping Created");
      resolve(mapping);
    }
   }
   );
  });
 }

Функция запроса

async function asyncSearchWithOutFilter(query, from, to) {
return new Promise((resolve, reject) => {
Product.esSearch(
  {
    from: from,
    size: to,
    query: {
      multi_match: { 
        query: query.suche,
        fields: [ "Title^10", "ItemCat^5" ]
      }
    },
    aggs: {
      mainCats: {
        terms: { field: "MainCat.keyword" }
      },
      itemCats: {
        terms: { field: "ItemCat.keyword" }
      },
      itemShops: {
        terms: {
          field: "Shop.keyword"
        }
      }
    }
  },
  {},
  async (err, results) => {
    if (err) throw err;
    let res = await results;

    /*  console.log("-------------Total Hits---------------");
    console.log(res.hits.total);
    console.log("-----------------------------------------");
    console.log("-------------Shops---------------");
    console.log(res.aggregations.itemShops.buckets);
    console.log("-----------------------------------------");
    console.log("-------------Item-Categories---------------");
    console.log(res.aggregations.itemCats.buckets);
    console.log("-----------------------------------------"); */
    resolve(res);
  }
);
  });
 }

Ожидаемые результаты: - Запрос на «ТВ»

Результаты: Продукты с названием "TV" в - если в категории также есть «ТВ», оцените его выше.

Проблема: Smart-TV-Controller также отображается в списке, если ищется «TV», но не ожидается, если кто-то ищет «TV»

помощь оценена.

1 Ответ

0 голосов
/ 22 января 2019

Похоже, вы пытаетесь получить точное соответствие.У вас уже есть подполе типа keyword для обоих полей Title и ItemCat.Поэтому вместо этого используйте ключевое слово в запросе на совпадение.

query: {
  multi_match: { 
    query: query.suche,
    fields: [ "Title.keyword^10", "ItemCat.keyword^5" ]
  }
}

Если вы не ищете точное совпадение в Title, то другим способом можно установить поля, как показано ниже:

fields: [ "Title^10", "ItemCat.keyword^5" ]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...