S
S
Sergey Shevchenko2021-02-20 03:26:57
elasticsearch
Sergey Shevchenko, 2021-02-20 03:26:57

I can not make a request, I already broke my head. What am I doing wrong?

Hello. A bit of background...
There are courses Courses have a list of tags.
I make an index:

'mappings' => [

    'properties' => [

        'id' => [

            'type' => 'integer',

        ],

        'name' => [

            'type' => 'text',

            'analyzer' => 'course',

        ],

        'format' => [

            'type' => 'keyword',

            'null_value' => '',

        ],

        'tags' => [

            'type' => 'nested',

            'properties' => [

                'id' => [

                    'type' => 'integer',

                ],

                'name' => [

                    'type' => 'text',

                    'analyzer' => 'tags',

                ],

            ],

        ],

    ],

]


I'm trying to make a request to get courses by a list of identifiers (the course ID is the same as the document ID) and then they need to find the search word in the course name or in the tag name. In sql I would do this:
...
WHERE id IN(...) and (name like '...' or tags.name like '...');


Here is what I got from the last results of trying to compose this request on elastic (it is not correct)
'query' => [

    'bool' => [

        'must' => [

            ['terms' => ['id' => $options['course_ids']]],

        ],

        'should' => [

            ['match' => [

                'name' => ['query' => $searchable],

            ]],


            ['nested' => [

                'path',

                'query' => [

                    'bool' => [

                        'must' => [

                            ['match' => ['tags.name' => ['query' => $searchable]]],

                        ],

                    ]

                ],

            ]],

        ],

    ],

],



Please tell me where did I go wrong? How to bring it "to mind"?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Shevchenko, 2021-03-03
@lancer_serega

I decided myself. Here is the answer if anyone is interested.

{
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "id": [
                 111,
                 222,
                 333
            ]
          }
        },
        {
          "bool": {
            "should": [
              {
                "match": {
                  "name": {
                    "query": "курс"
                  }
                }
              },
              {
                "nested": {
                  "path": "tags",
                  "query": {
                    "match": {
                      "tags.name": {
                        "query": "курс"
                      }
                    }
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

S
siri0s, 2021-03-19
@siri0s

Nested is optional here, because search in it on one field.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question