typesense / gatsby-plugin-typesense

A Gatsby plugin to automatically index content to Typesense post-build
Apache License 2.0
34 stars 11 forks source link

Always getting a 404 - Not found response #12

Closed Richacinas closed 2 years ago

Richacinas commented 2 years ago

Description

I always get a 200 response with this content:

{
  results: [
    { 
      code: 404,
      error: "Not found."
    }
  ]
}

I'm not sure if this error means that there is no coincidences on my search, or that the root resource is not found. I understand that is the server the one answering, so I guess I'm doing my request right.

Steps to reproduce

My Typesense server is started using docker. See this part of my docker-compose.yml:

version: '3.6'

services:
  typesense:
    container_name: typesense
    image: 'typesense/typesense:0.22.0.rcu7'
    entrypoint: sh -c "/opt/typesense-server --data-dir /data --api-key=86c5153b35cf --enable-cors"
    ports:
      - '8108:8108'
    volumes:
      - ./typesense-data/:/data
...

Having this gatsby-config.js (I made it so you can see the important part):

{
      resolve: "gatsby-plugin-typesense",
      options: {
        rootDir: path.resolve(__dirname, '../theme/wordpress/public/es-es'),
        collectionSchema: {
          name: "category",
          fields: [
            {
              name: "category_name",
              type: "string",
            },
            {
              name: "page_path",
              type: "string",
            },
            {
              name: "page_priority_score",
              type: "int32",
            },
          ],
          default_sorting_field: "page_priority_score",
        },
        server: {
          apiKey: "86c5153b35cf",
          nodes: [
            {
              host: "172.20.27.226",
              port: "8108",
              protocol: "http",
            },
          ],
        },
      },
    },
    {
      resolve: `gatsby-plugin-typesense`,
      options: {
        rootDir: path.resolve(__dirname, '../theme/wordpress/public/es-es'),
        collectionSchema: {
          name: "brand",
          fields: [
            {
              name: "brand_name",
              type: "string",
            },
            {
              name: "page_path",
              type: "string",
            },
            {
              name: "page_priority_score",
              type: "int32",
            },
          ],
          default_sorting_field: "page_priority_score",
        },
        server: {
          apiKey: "86c5153b35cf",
          nodes: [
            {
              host: "172.20.27.226",
              port: "8108",
              protocol: "http",
            },
          ],
        },
      },
    },
    {
      resolve: `gatsby-plugin-typesense`,
      options: {
        rootDir: path.resolve(__dirname, '../theme/wordpress/public/es-es'),
        collectionSchema: {
          name: "product",
          fields: [
            {
              name: "product_name",
              type: "string",
            },
            {
              name: "page_path",
              type: "string",
            },
            {
              name: "page_priority_score",
              type: "int32",
            },
          ],
          default_sorting_field: "page_priority_score",
        },
        server: {
          apiKey: "86c5153b35cf",
          nodes: [
            {
              host: "172.20.27.226",
              port: "8108",
              protocol: "http",
            },
          ],
        },
      },
    },

And, considering I have my pages stored in the rootDir folder:

image

Containing some dom elements inside like this one:

<h1 data-typesense-field="category_name" class="whatever">Abrigos</h1>

And this React component:

import { InstantSearch, SearchBox, Hits, Stats } from "react-instantsearch-dom"
import TypesenseInstantSearchAdapter from "typesense-instantsearch-adapter"
...

const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({
  server: {
    apiKey: config.typesenseApiKey,
    nodes: [
      {
        host: "172.20.27.226",
        port: "8108",
        protocol: "http",
      },
    ],
  },
  // The following parameters are directly passed to Typesense's search API endpoint.
  //  So you can pass any parameters supported by the search endpoint below.
  //  queryBy is required.
  collectionSpecificSearchParameters: {
    category: {
      queryBy: "category_name",
    },
    brand: {
      queryBy: "brand_name",
    },
    product: {
      queryBy: "product_name",
    },
  },
})
const searchClient = typesenseInstantsearchAdapter.searchClient

const Hit = ({ hit }) => (
  <p>
    {hit.title} - {hit.description}
  </p>
);

const Search = () => {
  return (
    <InstantSearch searchClient={searchClient} indexName="category" >
      <SearchBox />
      <Stats />
      <Hits hitComponent={Hit} />
    </InstantSearch >
  )
}

I'm getting the response 404 - Not found

Here you can see the network request that is being used:

http://172.20.27.226:8108/multi_search?x-typesense-api-key=86c5153b35cf

{"searches":[{"query_by":"category_name","sort_by":"","highlight_full_fields":"category_name","collection":"category","q":"abrigo","facet_by":"","page":1}]}

Expected Behavior

When I search, for example, "Abrigo", I would expect the server to return some hits.

Actual Behavior

I always get the response I mentioned above.

Metadata

Typsense Version:

I have followed this guide: https://medium.com/swlh/building-a-search-bar-for-your-gatsbyjs-site-with-typesense-3e277dc33942

So I have a typesense server started with docker, which version is typesense/typesense:0.22.0.rcu7.

On the client, I have:

"typesense-instantsearch-adapter": "~2.2.0",
"gatsby-plugin-typesense": "~0.3.0"

OS:

Ubuntu 20.04LTS on WSL (Windows 10)

Richacinas commented 2 years ago

I also have another question:

How does the Typesense server store the elements that I'm setting up using this plugin (data-typesense-field)? I can't imagine how the Typesense server could get to know about these fields/items and then send a response with the hits.

Also, I can't see anything on the Typesense server logs that resembles a response. It is as if the response is being sent from somewhere else:

image

jasonbosco commented 2 years ago

Closing in favor of #13. I've added my response there.

Richacinas commented 2 years ago

Sorry.. I don't know how I managed to open 2 issues!