question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot reproduce a POST search query

See original GitHub issue

Hi,

I have searched for this into the issues before opening this one, but I cannot see a similar case. I want to perform a search query.

  • Index: clients
  • doc type: client
  • Path: /clients/client/_search
  • Method: POST
  • Body:
{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "del*",
            "fields": [
              "firstName",
              "lastName"
            ]
          }
        },
        {
          "match": {
            "merchantId": "44UudEcyFMbpLOGzG2HWpfngJ8o2"
          }
        }
      ]
    }
  }
}

Doing this with curl:

curl -X POST "https://xyz/clients/client/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "ale*",
            "fields": [
              "firstName",
              "lastName"
            ]
          }
        },
        {
          "match": {
            "merchantId": "44UudEcyFMbpLOGzG2HWpfngJ8o2"
          }
        }
      ]
    }
  }
}
'

Gives me 2 hits (correct!).

Same with POSTMAN (as attached) screen shot 2018-09-21 at 16 28 42.

Now, the problem.

Here’s my code:

return client.search({
                index: 'clients',
                type: 'client',
                body: {
                    query: {
                        bool: {
                            must: [
                                {
                                    query_string: {
                                        query: "ale*",
                                        fields: ["firstName", "lastName"]
                                    }
                                },
                                {
                                    match: {
                                        'merchantId': merchantId // Defined and logged <-
                                    }
                                }
                            ]
                        }
                    }
                }
            }
        );

The result is empty, the log gives me this:

  starting request {
    "method": "POST",
    "path": "/clients/client/_search",
    "body": {
      "query": {
        "bool": {
          "must": [
            {
              "query_string": {
                "query": "ale*",
                "fields": [
                  "firstName",
                  "lastName"
                ]
              }
            },
            {
              "match": {
                "merchantId": "44UudEcyFMbpLOGzG2HWpfngJ8o2"
              }
            }
          ]
        }
      }
    },
    "query": { }
  }


info: Elasticsearch DEBUG: 2018-09-21T14:16:07Z
  Request complete

info: ->! { took: 1,
  timed_out: false,
  _shards: { total: 1, successful: 1, skipped: 0, failed: 0 },
  hits: { total: 0, max_score: null, hits: [] } }

What am I missing?

Thanks!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
lorenzosfarracommented, Sep 28, 2018

I am not creating the data, the data is there from days. Anyway, “brutally” executing your code works just fine, so definitely is something I have to figure out in my custom code. Thanks again for your support ( I close by myself 😃 )!

0reactions
delvedorcommented, Sep 27, 2018

I created this simple example where I get back all the data.

'use strict'

const sleep = require('then-sleep')
const elasticsearch = require('elasticsearch')
const client = new elasticsearch.Client({
  host: 'localhost:9200',
  api: '6.2'
})

client.bulk({
  body: [
    { index: { _index: 'clients', _type: 'client' } },
    { 'firstName': 'Alessandro', 'lastName': 'Del Piero', 'phoneNumber': '+399287654098', 'merchantId': '44UudEcyFMbpLOGzG2HWpfngJ8o2' },
    { index: { _index: 'clients', _type: 'client' } },
    { 'firstName': 'Alessandro', 'lastName': 'Costacurta', 'phoneNumber': '+394943943', 'merchantId': '44UudEcyFMbpLOGzG2HWpfngJ8o2' },
    { index: { _index: 'clients', _type: 'client' } },
    { 'firstName': 'Alessandro', 'lastName': 'Nesta', 'phoneNumber': '+3949304903', 'merchantId': '90339203812831293' }
  ]
}, async (err, res) => {
  if (err) throw err

  await sleep(2000)

  const result = await client.search({
    index: 'clients',
    type: 'client',
    body: {
      query: {
        bool: {
          must: [
            {
              query_string: {
                query: 'ale*',
                fields: ['firstName', 'lastName']
              }
            },
            {
              match: {
                'merchantId': '44UudEcyFMbpLOGzG2HWpfngJ8o2'
              }
            }
          ]
        }
      }
    }
  })
  console.log(JSON.stringify(result, null, 2))

  // use a custom elasticsearch instance for run this test!
  // this command will delete every index!
  await client.indices.delete({ index: '*', ignore: 404 })
}

I was asking if you are running the request just after the indexing operation because Elasticsearch is a near real-time engine. This means that the indexed data cannot be available instantaneously, but there is a time span where is not available. By default, that time span is 1 second, so if you run two requests against your express server:

  1. index data
  2. search data

And there is less than one second between the requests you will not find the data.

That’s why there is a sleep function in my code, and if you remove it, or you change the timeout at 200ms, you will not find anything.

If you want to know more, or how to change the index refresh time, I suggest you read this fantastic document 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

POST or GET in a search form? - Stack Overflow
The essence of the problem, is that a GET has no request body, and for that reason it cannot handle more complex request....
Read more >
POST search engine - Vivaldi Forum
I have tried this like 8 times now, in Stable, Snapshot and in an internal build, and I cannot reproduce it. For me,...
Read more >
Influencing your title links in search results - Google Developers
A title link is the title of a search result on Google Search and other properties (for example, Google News) that links to...
Read more >
Google Chrome Privacy Whitepaper
Chrome prefetching - can be initiated by Chrome itself whenever it detects a search query typed in the omnibox, a likely beginning of...
Read more >
Why would one employ a POST-based search engine on their ...
However, I'm working with an internal site search engine that receives and returns SERP requests POST-style. Why would it be designed to do...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found