0

I'm having some trouble getting the right query for elastic search. When searching on 2 fields it's giving unexpected results.

Let's say given the documents:

[
  { name: 'Infernoble', code: 'MP21' },
  { name: 'Infernoble', code: 'AMDE' },
  { name: 'A Cell Device', code: 'FOTB' }
]

I'm using searchkit's MultiMatchQuery on those 2 fields which sends the following query to ElasticSearch:

{
    "bool": {
        "should": [
            {
                "multi_match": {
                    "query": "<my query term>",
                    "fields": [
                        "name",
                        "code"
                    ],
                    "type": "best_fields",
                    "operator": "and"
                }
            },
            {
                "multi_match": {
                    "query": "<my query term>",
                    "fields": [
                        "name",
                        "code"
                    ],
                    "type": "cross_fields"
                }
            },
            {
                "multi_match": {
                    "query": "<my query term>",
                    "fields": [
                        "name",
                        "code"
                    ],
                    "type": "phrase"
                }
            },
            {
                "multi_match": {
                    "query": "<my query term>",
                    "fields": [
                        "name",
                        "code"
                    ],
                    "type": "phrase_prefix"
                }
            }
        ]
    }
}

Currently, if I search for "Infernoble" I correctly get the first 2 documents.

When I search "Infernoble AMDE" I would like it to bring the second document to the front (given that it has name:'Infernoble' and code: 'AMDE'),

Instead "Infernoble AMDE" gives the same results at the top as just "Infernoble".

Additionally, "Infernoble A" brings the 3rd document to the front (since it starts with "A"), ignoring that documents exist with name "Inferno".

The code property is always just a short acronym that could be added to the end to narrow the search by code.

How can I get this to work?

parliament
  • 21,544
  • 38
  • 148
  • 238

1 Answers1

0

You need to use the nested field type rather than the object.

Musab Dogan
  • 1,811
  • 1
  • 6
  • 8
  • Thanks but Im not following, could you elaborate please? I'm not using object or array types. `name` and `code` are simply 2 text fields. The array I provided is illustrating 3 documents in my index, not the structure of a single document. I don't see how nesting is relevant. – parliament Mar 10 '23 at 09:38
  • Hmm, if you are not using an array you need to index the data not as nested to get the result you want. Please check the examples in nested field type – Musab Dogan Mar 10 '23 at 09:48