简体   繁体   中英

NEST Elastic Search - query a term with spaces

I'm having hard time implementing autocomplete in elastic for DisplayName property for the text that have spaces in it, here is the setup of the field:

"DisplayName": {
    "type": "text",
    "fields": {
        "keyword": {
            "type": "keyword",
            "ignore_above": 256,
            "normalizer": "caseinsensitive"
        },
        "prefix": {
            "type": "text",
            "analyzer": "startswith"
        }
    }
},
"id": {
    "type": "keyword"
}

Here is the startwith analyzer definition:

"analysis": {
    "analyzer": {
        "startswith": {
            "char_filter": [
                "html_strip"
            ],
            "filter": [
                "lowercase"
            ],
            "tokenizer": "keyword",
            "type": "custom"
        }
    },
    "normalizer": {
        "caseinsensitive": {
            "filter": [
                "lowercase",
                "asciifolding"
            ],
            "type": "custom"
        }
    }
},
"creation_date": "1565034410554",
"mapping": {
    "total_fields": {
        "limit": "5000"
    }
},
"number_of_shards": "5",
"provided_name": "streetsmart"

In my query builder here is the query that tries to grab the result:

_type:User AND (DisplayName.prefix:Joseph adam* OR UserPrincipalName.prefix:Joseph adam*)"

and the result that I get is all the names that contains Adam which the result should be Joseph Adam Jr , Does anyone know what I should do?

我得到的结果

I am not aware of c# and .net syntax, but adding a working example with index data, search query, and search result in JSON format.

You can also use Match phrase prefix query that :

Returns documents that contain the words of a provided text, in the same order as provided. The last term of the provided text is treated as a prefix, matching any words that begin with that term.

Index Data:

{
    "name": "Adam"
}
{
    "name": "Joseph Adam Sr"
}
{
    "name": "Joseph Adam Jr"
}

Search Query:

    {
    "query": {
        "multi_match": {
            "query": "Joseph Adam",
            "fields": [
                "name"
            ],
            "type": "phrase_prefix"
        }
    }
}

Search Result:

"hits": [
      {
        "_index": "stof_64163994",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.54037446,
        "_source": {
          "name": "Joseph Adam Jr"
        }
      },
      {
        "_index": "stof_64163994",
        "_type": "_doc",
        "_id": "3",
        "_score": 0.54037446,
        "_source": {
          "name": "Joseph Adam Sr"
        }
      }
    ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM