Indexing same field twice is not working


(Steve Bissett) #1

I have the following Ruby code to define my index:

 self.client.indices.create index: self.index,
                               body: {
                                   settings: {
                                       analysis: {
                                           filter: {
                                               ngram: {
                                                   type: 'nGram',
                                                   min_gram: 2,
                                                   max_gram: 25
                                               }
                                           },
                                           analyzer: {
                                               ngram: {
                                                   tokenizer: 'whitespace',
                                                   filter: ['lowercase', 'stop', 'ngram'],
                                                   type: 'custom'
                                               },
                                               ngram_search: {
                                                   tokenizer: 'whitespace',
                                                   filter: ['lowercase', 'stop'],
                                                   type: 'custom'
                                               }
                                           }
                                       }
                                   },
                                   mappings: {
                                       search_variable: {
                                           properties: {
                                               "name" => {
                                                   "type" => "string",
                                                   "index" => "not_analyzed"
                                               },
                                               .... <some items removed here for brevity>,
                                               "node_id" => {
                                                   "type" => "integer",
                                                   "index" => "not_analyzed"
                                               },
                                               "search_text" => {
                                                   "type" => "multi_field",
                                                   "fields" => {
                                                       "search_text" => {"type" => "string",
                                                                         "analyzer" => "ngram",
                                                                         "search_analyzer" => "ngram_search"},
                                                       "whole_words" => {"type" => "string",
                                                                         "analyzer" => "ngram_search"}
                                                   }


                                               }
                                           }
                                       }
                                   }
                               }

This code is acknowledged and created successfully.
I can confirm this with a get request to localhost:9200/<index_name> which shows the settings.

I would then have expected to be able to search search_text and search_text.whole_words, but it returns no hits for search_text.whole_words.

Here is my search query:

search_query = {
    index: index,
    body: {
        _source: {
            exclude: ["search_text", "survey_id", "ancestor_ids", "node_type"]
        },
        query: {
            :bool => {
                :filter => {
                    :term => {"survey_id" => 12}
                },
                :must => {
                    :match => {
                        #"search_text" => {"query" => "ford", "operator" => "and"},
                        "search_text.whole_words" => {"query" => "ford", "operator" => "and"}
                    },
                },
                :must_not => {
                    :match => {
                        "search_text" => {"query" => "", "operator" => "or"}}
                }
            }
        }
    }
}

It returns no results and I can't understand why.
Basically, at the end, my requirement is that I want to be able to prioritise whole words.


(Steve Bissett) #2

This also shows what I'm trying to do in my searches, but it doesn't seem to work:


(Mark Walkom) #3

Once you have indexed data in, can you grab one of the docs to make sure that field has been populated?


(system) #4