简体   繁体   中英

ElasticSearch Full Text Search

I try to run full text search with regular expression on elastic search java api. My filter is like this:

  FilterBuilder qFilter= FilterBuilders.regexpFilter("_all",
 ". *"+text+". *");

But it matches with only one word not with a phrase. What I mean is, for example:

if there is a string in the soruce like: " one two three four five.. " and when my text string is like these: " two " , " our ", " thr " ... then it works.

But when my realTimeTextIn string is " two three " full text search doesn't work. I can't search one more than one words.

What I'm missing here?

The rest of the codes are something like this:

 FilterBuilder qFilter      = FilterBuilders.regexpFilter("_all", ".*"+q+".*");
  SearchResponse response  = ClientProvider.instance().getClient().prepareSearch(index)
                      .setTypes(type)
                      .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)                            
                      .setPostFilter(qFilter)                  
                      .setFrom(0).setSize(250).setExplain(true)       
                      .execute()
                      .actionGet();

Thanks for helps.

When text string is empty or null,this join method throws exception. You can use regexp filter like this.

FilterBuilder qFilter = FilterBuilders.regexpFilter("_all",(".*"+q+".*").replace(" ", ".*"));

That is an interesting question. I found something like phrase queries and phrase matching: http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/phrase-matching.html http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/_phrase_search.html

In java api we can do this for query (I tested this):

    SearchResponse response = client.prepareSearch(index)
            .setTypes(type)
            .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
            .setFrom(0).setSize(250).setExplain(true).setQuery(QueryBuilders.matchPhraseQuery(field, "one two"))
            .execute()
            .actionGet();

I'm sorry, but I didn't find any solution.

You can try build a script filter (insert plain json to your filter instead of java method) or something called query filter: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-query-filter.html

I hope that it helped you a little.


EDIT: Of course there is a simple solution, but I don't know if it satistfies you.

 FilterBuilder qFilter= FilterBuilders.regexpFilter( "_all",". *"+Joiner.on(".*").join(text.split(" "))+". *"); 

I happened to make a full text search like this using query builder

QueryBuilder queryBuilder = QueryBuilders.multiMatchQuery(query)
              .field("name", 2.0f)
              .field("email")
              .field("title")
              .field("jobDescription", 3.0f)
              .type(MultiMatchQueryBuilder.Type.PHRASE_PREFIX);

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